Full image adapter error - java

I have a custom base image adapter in which i use for a grid view.. i was trying to use it for a full image view but i get an error while creating the my custom adapter object...passing the parameters....
By the way sorry for any mistakes ,this is my first question:
This is for gallery to preview full
Fullimage java:Where the error is present
public class FullImage extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_full_image);
Intent i= getIntent();
int position = i.getExtras().getInt("id");
MyPicsAdapter adapter = new MyPicsAdapter(this);//ERROR is here!!!
ImageView imageView = (ImageView) findViewById(R.id.fullimage);
imageView.setImageResource(adapter.item_image[position]);
}
}
Custom Adapter:
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class MyPicsAdapter extends BaseAdapter{
public Context context;
public final int item_image[];
public final String item_text[];
public MyPicsAdapter(Context context, int item_image[], String[] item_text)
{
this.context = context;
this.item_image = item_image;
this.item_text = item_text;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = new View(context);
// get layout from custom_gridview.xml
gridView = inflater.inflate(R.layout.custom_gridview, null);
// set value into imageview
ImageView image = (ImageView) gridView.findViewById(R.id.item_image);
image.setImageResource(item_image[position]);
// set value into textview
TextView text = (TextView) gridView.findViewById(R.id.item_text);
text.setText(item_text[position]);
} else {
gridView = (View) convertView;
}
return gridView;
}
#Override
public int getCount() {
return item_text.length;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
}
Class where the images are:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;
public class Smitpic extends AppCompatActivity {
GridView grid;
String text[] = {
"Colorful countryside on the way to Smit",
"A local bus at the Village square",
"Cattle grazing at the village ground",
"Pine trees lined the road leading to the Wooden house",
"Traditional wooden house made without any metal nails",
"Intricate carvings on the door of the wooden house",
"A cottage at Smit",
"Villagers returning home after washing clothes at the river",
"A river idlying flowing near Smit",
"Colorful field of grasses and pine trees"
};
int image[] = {R.drawable.smit1,R.drawable.smit2,
R.drawable.smit3,R.drawable.smit4,R.drawable.smit5,
R.drawable.smit6,R.drawable.smit7,R.drawable.smit8,
R.drawable.smit9,R.drawable.smit10
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_smitpic);
grid = (GridView)findViewById(R.id.simpleGrid);
grid.setAdapter(new MyPicsAdapter(this,image,text));
// grid.setAdapter(new ImageAdapter(this,image));
grid.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(),text[position],Toast.LENGTH_LONG).show();
Intent a= new Intent(getApplicationContext(),FullImage.class);
a.putExtra("id",position);
startActivity(a);
}
});
}
}

You can pass only the image as follows,
grid.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(),text[position],Toast.LENGTH_LONG).show();
Intent a = new Intent(getApplicationContext(),FullImage.class);
a.putExtra("id", image[position]);
startActivity(a);
}
});
Inside onCreate of FullImage Activity get the image and display it in the ImageView,
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_full_image);
int image = getIntent().getExtras().getInt("id");
ImageView imageView = (ImageView) findViewById(R.id.fullimage);
imageView.setImageResource(image);
}

Related

OnCreateView method in Activity class

fragment reminder image - current problem
This current class that I am using called Fragment Reminder is used to display a list of the medications that have been entered. However this class before was a Fragment and I decided to change it to an Activity and the method which displays the list called onCreateView is unused now. When I click on that button to run this class the screen goes dark for some reason. Now that this class is an Activity the onCreateView cannot be used since that is for a Fragment. What should I change that to so I can create the list.
FragmentReminder:
package com.example.junai.mrtest2;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import static android.view.View.GONE;
public class FragmentReminder extends Activity implements View.OnClickListener{
private ArrayList al;
private List list=new ArrayList();
private ArrayAdapter<String> adapter;
ListView lv;
TextView tv;
FloatingActionButton fab;
public void receiveData(ArrayList al)
{
this.al=al;
list.add(al.get(0));
}
//data for customlist
private String desc[] = {};
// #Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
this.setTitle("Medicine Reminder");
View v=inflater.inflate(R.layout.fragment_reminder, container, false);
fab=(FloatingActionButton)v.findViewById(R.id.floatingActionButton);
lv=(ListView)v.findViewById(R.id.rem_lv);
tv=(TextView) v.findViewById(R.id.reminder_tv);
fab.setOnClickListener(this);
DatabaseHandler db=new DatabaseHandler(this);
list=db.getAllReminders();
if(list.size()==0)
{
lv.setVisibility(GONE);
return v;
}
tv.setVisibility(GONE);
adapter = new CustomList(this,list,desc);
lv.setAdapter(adapter);
//***Customised list view add***********************************************************************
/* CustomList customList = new CustomList(getActivity(),list, desc);
lv.setAdapter(customList);*/
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//Toast.makeText(getActivity(),"You Clicked "+list.get(i),Toast.LENGTH_SHORT).show();
//addReminderInCalendar();
Intent in=new Intent(FragmentReminder.this,MedRemInfo.class);
in.putExtra("id",list.get(i).toString());
startActivity(in);
}
});
//***************************************************************************
return v;
}
#Override
public void onClick(View v) {
Intent in=new Intent(this,AddReminder.class);
startActivity(in);
}
}
fragment_reminder.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.junai.mrtest2.FragmentReminder">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp"
android:id="#+id/reminder_tv"
android:textSize="20dp"
android:text="No Reminders to show, Add a reminder.."
android:gravity="center"
/>
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:src="#drawable/ic_add_black_24dp"
android:tint="#ffffff"
android:layout_gravity="bottom|center"
android:id="#+id/floatingActionButton" />
<ListView
android:layout_width="match_parent"
android:id="#+id/rem_lv"
android:layout_height="match_parent"
/>
</LinearLayout>
MainActivity:
package com.example.junai.mrtest2;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
/**
* Created by junai on 20/03/2018.
*/
public class MainActivity extends Activity {
private Button btn_addReminder, btn_reminderinfo;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_addReminder = (Button) findViewById(R.id.btn_addReminder);
btn_addReminder.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, AddReminder.class));
}
});
btn_reminderinfo = (Button) findViewById(R.id.btn_reminderinfo);
btn_reminderinfo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, FragmentReminder.class));
}
});
}
}
CustomList:
package com.example.junai.mrtest2;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
import java.util.Random;
public class CustomList extends ArrayAdapter<String> {
private List names;
private String[] desc;
private Activity context;
public CustomList(Activity context, List names, String[] desc) {
super(context, R.layout.list_medinfo, names);
this.context = context;
this.names = names;
this.desc = desc;
}
String color_hex[]={"#ff4000","#0000ff","#003EFF","#5C246E","#8B668B","#CD2990","#D41A1F","#FBDB0C","#FF6600"};
#Override
public View getView(int position, View convertView, ViewGroup parent) {
//********************************************************************
ViewHolder holder;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
// If holder not exist then locate all view from UI file.
if (convertView == null) {
// inflate UI from XML file
convertView = inflater.inflate(R.layout.list_medinfo, parent, false);
// get all UI view
holder = new ViewHolder(convertView);
// set tag for holder
convertView.setTag(holder);
} else {
// if holder created, get tag from view
holder = (ViewHolder) convertView.getTag();
}
holder.textView.setText(getItem(position));
String firstLetter = null;
//get first letter of each String item
Log.d("String",getItem(position));
String str=getItem(position);
firstLetter=String.valueOf(str.charAt(0)).toUpperCase();
// firstLetter.toUpperCase();
ColorGenerator generator = ColorGenerator.MATERIAL; // or use DEFAULT
// generate random color
//int color = generator.getColor(getItem(position));
int color = generator.getRandomColor();
int pos= new Random().nextInt(color_hex.length);
color = Color.parseColor(color_hex[pos]);
Log.d("Color",""+pos);
TextDrawable drawable = TextDrawable.builder()
.buildRound(firstLetter, color); // radius in px
holder.imageView.setImageDrawable(drawable);
return convertView;
//***********************************************************************
/* LayoutInflater inflater = context.getLayoutInflater();
View listViewItem = inflater.inflate(R.layout.list_medinfo, null, true);
TextView textViewName = (TextView) listViewItem.findViewById(R.id.medname);
TextView textViewDesc = (TextView) listViewItem.findViewById(R.id.textViewDesc);
ImageView image = (ImageView) listViewItem.findViewById(R.id.imageView);
textViewName.setText(names.get(position).toString());
textViewDesc.setText(desc[position]);
image.setImageResource(imageid[position]);
return listViewItem;*/
}
private class ViewHolder {
private ImageView imageView;
private TextView textView;
public ViewHolder(View v) {
imageView = (ImageView) v.findViewById(R.id.medimage);
textView = (TextView) v.findViewById(R.id.medname);
Typeface typeface=Typeface.createFromAsset(context.getAssets(), "fonts/georgia.ttf");
textView.setTypeface(typeface);
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP,20);
}
}
}
I've refactored your code:
public class FragmentReminder extends Activity implements View.OnClickListener {
private ArrayList al;
private List list = new ArrayList();
private ArrayAdapter<String> adapter;
ListView lv;
TextView tv;
FloatingActionButton fab;
public void receiveData(ArrayList al) {
this.al = al;
list.add(al.get(0));
}
//data for customlist
private String desc[] = {};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_reminder);
fab = (FloatingActionButton) findViewById(R.id.floatingActionButton);
lv = (ListView) findViewById(R.id.rem_lv);
tv = (TextView) findViewById(R.id.reminder_tv);
fab.setOnClickListener(this);
DatabaseHandler db = new DatabaseHandler(this);
list = db.getAllReminders();
if (list.size() == 0) {
lv.setVisibility(GONE);
}
tv.setVisibility(GONE);
adapter = new CustomList(this, list, desc);
lv.setAdapter(adapter);
//***Customised list view add***********************************************************************
/* CustomList customList = new CustomList(getActivity(),list, desc);
lv.setAdapter(customList);*/
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//Toast.makeText(getActivity(),"You Clicked "+list.get(i),Toast.LENGTH_SHORT).show();
//addReminderInCalendar();
Intent in = new Intent(FragmentReminder.this, MedRemInfo.class);
in.putExtra("id", list.get(i).toString());
startActivity(in);
}
});
}
#Override
public void onClick(View v) {
Intent in = new Intent(this, AddReminder.class);
startActivity(in);
}
}
You need only to move the code to onCreate() instead of onCreateView() and also you don't need to access the recently inflated view V to do findViewByID, all you have to do is setContentView() beforehand.

Recycler view images aren't clickable, to open up a new page

I'm trying to make each image within the RecyclerView clickable to open up the photo, along with its Title and a description, but all of this info is coming through Flickr, I've added in code that I thought should be suitable but it doesn't seem to be working, as none of the images are clicking, any guidance as to how to fix this is greatly appreciated.
Would a simple findByViewId() method in the details class suffice or is there more than I seem to be missing? Like I said, any help is appreciated.
Thank you!
ImageListAdapter Class
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.android.volley.toolbox.NetworkImageView;
import java.util.ArrayList;
/**
* Created by Adam on 22/12/2017.
*/
public class ImageListAdapter
extends RecyclerView.Adapter<ImageListAdapter.ViewHolder> {
private NetworkImageView imageThumbnail;
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView imageTitle;
public NetworkImageView mainImage;
public ViewHolder(View itemView) {
super(itemView);
imageTitle = (TextView) itemView.findViewById(R.id.imageTitle);
mainImage = (NetworkImageView)
itemView.findViewById(R.id.mainImage);
}
private View.OnClickListener itemClicked = new View.OnClickListener() {
int position = ViewHolder.this.getLayoutPosition();
#Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(),
DetailsActivity.class);
intent.putExtra("PHOTO_POSITION", position);
imageThumbnail.setOnClickListener(itemClicked);
itemView.getContext().startActivity(intent);
}
};
}
//Data Source
public ArrayList<ImageInfo> imageList = new ArrayList<ImageInfo>();
// context
private Context context;
public ImageListAdapter(Context context) {
this.context = context;
}
#Override
public int getItemCount() {
return imageList.size();
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//Create the view for the cell in the list
View v = LayoutInflater.from(context).inflate(R.layout.cell_image_card, parent, false);
ImageListAdapter.ViewHolder vh = new ImageListAdapter.ViewHolder(v);
return vh;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
ImageInfo imageInfo = imageList.get(position);
holder.imageTitle.setText(imageInfo.title);
holder.mainImage.setImageUrl(imageInfo.url_m,
NetworkMgr.getInstance(context).imageLoader);
// holder.mainImage.setImageResource(imageInfo.imageResource);
}
}
DetailsActivity Class
public class DetailsActivity extends AppCompatActivity {
public TextView imageTitle;
public NetworkImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
Intent intent = getIntent();
photoPosition = intent.getIntExtra("PHOTO_POSITION", 0);
titleView.setText(photo.title);
imageView = NetworkMgr.getInstance(this).imageList.get(photoPosition);
}
}
you are just forgetting to set the onClick listener on the mainImage view
Step 1: set the click listener on mainImage
public ViewHolder(View itemView) {
super(itemView);
final Context mContext = itemView.getContext();
final int position = getAdapterPosition();
imageTitle = (TextView) itemView.findViewById(R.id.imageTitle);
mainImage = (NetworkImageView)
itemView.findViewById(R.id.mainImage);
mainImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(mContext, DetailsActivity.class);
intent.putExtra("PHOTO_POSITION", position);
mContext.startActivity(intent);
}
});
}
Step 2: get the position like this in details activity
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
Intent intent = getIntent();
int position = intent.getIntExtra("PHOTO_POSITION", 0);
ImageInfo imageInfo = NetworkMgr.getInstance(this).imageList.get(position);
//use image info to get the image and title details.
}

How to add different toast messages for each imageView which displayed using GridView in android

activity_main
<GridView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="8"
tools:ignore="MissingConstraints" />
ImageAdapter.java
package com.example.android.whattheemoticon;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
private Integer[] mThumbIds = {
R.drawable.smileys, R.drawable.animals,
R.drawable.food, R.drawable.activities,
R.drawable.symbols, R.drawable.objects,
R.drawable.travel, R.drawable.flags,
};
}
MainActivity.java
package com.example.android.whattheemoticon;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//display images using gridview.
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
//onClickListeners for each clickable imageViews.
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
//create toast for view when clicked.
Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
}
}
I'm trying to add toast message for each images which are displayed using GridView , i'm ended up adding the code for toast to work for each grid . And i have completed adding and displaying the images in GridView. Please help me out for how to display the right toast message for each image while clicked by an user.
#Rajashekar. Here is an example.
public class MainActivity extends AppCompatActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//display images using gridview.
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
final ArrayList<String> message = new ArrayList<>();
message.add("Message for image 0");
message.add("Message for image 1");
message.add("Message for image 2");
//onClickListeners for each clickable imageViews.
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
//create toast for view when clicked.
String mess = message.get(position);
Toast.makeText(MainActivity.this, "" + mess, Toast.LENGTH_SHORT).show();
}
});
}
}
Declare an array like this :
String arr[ImageCount] = {"Message for first image", "Message for second image"......};
Then use a function to print Toast Message like this :
public void showToast(int pos){
for(int i = 0; i<=pos; i++){
if(i == pos){
Toast.makeText(Context, arr[i], Toast.LENGTH_SHORT).show();
break;
}
}
}
Now call the function :
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
showToast(position);
}

Android prompting to save image or set wallpaper from gridview

I would like the user to get the option once the chosen image is clicked to set the image as their wallpaper or to save it to their SD card.
This is my first time doing this so I need some guidance. I have looked at other questions similar to this one but everyone uses different methods to the one I have done to set up displaying the images.
Thanks in advance, here's the code:
AdapterView for Displayimagesin:
package com.question;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class AdapterViewADV extends BaseAdapter {
private Context mContext;
public Integer[] mThumbIds = {
R.drawable.Image1,
R.drawable.Image2,
R.drawable.Image3,
R.drawable.Image4,
R.drawable.Image5,
R.drawable.Image6
};
public AdapterViewADV(Context c){
mContext = c;
}
#Override
public int getCount() {
return mThumbIds.length;
}
#Override
public Object getItem(int position) {
return mThumbIds[position];
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = null;
if(convertView == null){
imageView = new ImageView(mContext);imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(NO_SELECTION, NO_SELECTION));
convertView = imageView;
}else{
imageView = (ImageView)convertView;
}
imageView.setImageResource(mThumbIds[position]);
return convertView;
}
}
Class displaying images in:
package com.question;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.Toast;
public class Displayimagesin extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_battlefield4);
GridView gridView = (GridView) findViewById(R.id.grid_view);
// Instance of ImageAdapter Class
gridView.setAdapter(new AdapterViewADV(this));
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Toast.makeText(Displayimagesin.this, "Wallpaper set",
Toast.LENGTH_SHORT).show();
}
});
}
}
set wallpaper by calling WallpaperManager .
get a reference to your image to Bitmap.
something like this
WallpaperManager wm=WallpaperManager.getInstance(this);
wm.setBitmap(bitmap);
and in manifest file add permission
android.permission.SET_WALLPAPER
hope it helps.
Try to use WallpaperManager to set wallpaper
WallpaperManager myWallpaperManager=WallpaperManager.getInstance(getApplicationContext());
myWallpaperManager.setResource(mThumbIds[curruntPosition]);

OutOfMemoryException while scroll many time

I got a problem in gridview scroll. I have make one custom gridview and insert widget in raw file and inflate them through view. I got proper result and proper data and everything is gone well but when I scroll gridview 3-4 times up down speedy it raises an OutOfMemoryException.
This app contain list of installed app list and icon
Here is my custome adapter's code:
package com.AppFavorits;
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.AppFavorits.ImageLoad.ImageLoader;
public class GridViewAdapter extends BaseAdapter {
private Context activit;
LayoutInflater inflator;
public RelativeLayout imgvGridItem;
public TextView txtGridItemlabel;
public CheckBox chkbxGridItem;
ArrayList<PInfo> lstpinfo = new ArrayList<PInfo>();
public ImageLoader imageLoader;
public GridViewAdapter(Context m1, ArrayList<PInfo> lstpinfo) {
activit = m1;
inflator = LayoutInflater.from(m1);
this.lstpinfo = lstpinfo;
imageLoader=new ImageLoader(m1.getApplicationContext());
}
public static class ViewHolder {
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder view;
// LayoutInflater inflator = activit.getLayoutInflater();
view = new ViewHolder();
convertView = inflator.inflate(R.layout.gridviewrow, null);
imgvGridItem = (RelativeLayout) convertView
.findViewById(R.id.rlGreidItemicon);
txtGridItemlabel = (TextView) convertView
.findViewById(R.id.txtGridItemlabel);
chkbxGridItem = (CheckBox) convertView
.findViewWithTag(R.id.chkbxGridItem);
if ((lstpinfo.get(position).appname.toString()) != null){
Drawable d = lstpinfo.get(position).icon;
//new ImageLoad().execute();
imgvGridItem.addView(getimageviewimage(lstpinfo.get(position).icon));
txtGridItemlabel.setText(lstpinfo.get(position).appname.toString());
// convertView.setTag(view);
}
return convertView;
}
#Override
public int getCount() {
return lstpinfo.size();
}
#Override
public Object getItem(int position) {
return lstpinfo.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public RelativeLayout getimageviewimage(Drawable d) {
RelativeLayout seprator = new RelativeLayout(activit);
seprator.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
seprator.setGravity(Gravity.LEFT | Gravity.CENTER);
ImageView imgmap = new ImageView(activit);
imgmap.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
// imgmap.setBackgroundResource(R.drawable.a13);
Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
imageLoader.DisplayImage(bitmap, imgmap);
//imgmap.setImageBitmap(bitmap);
seprator.setPadding(5, 5, 15, 5);
seprator.addView(imgmap);
return seprator;
}
private class ImageLoad extends AsyncTask<Void, Void, Void> {
// private final ProgressDialog dialog = new ProgressDialog(tranning.this);
protected void onPreExecute() {
// this.dialog.setMessage("Please Wait...");
// this.dialog.show();
// put your code which preload with processDialog
}
#Override
protected Void doInBackground(Void... arg0) {
// put your code here
return null;
}
#Override
protected void onPostExecute(final Void unused) {
/*if (this.dialog.isShowing()) {
this.dialog.dismiss();
} */
}
}
}
I think there are something wrong with getview.
In getView() you are not reusing convertView, but always inflating new one with inflator. This requires more memory and makes your code slower. Also, check that you are not leaking images with imageLoader.
out of memory usually means that there is not enough memory to load the file. try closing everything else (or atleast a few other apps) and trying again.
Download code From Following link and use gridview instead of listview
Lazy Loading Example

Categories

Resources