How to handle the positions in Gallery android? - android

How to handle the positions in Gallery.I need to change the background of image at perticular position.Iam able to change the background image of selected image successfully within the onItemClick method.But i need to change the old selected background image also as non-selected one.
public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {
//previouse selected image
//here sometimes it is changing background of another image(which is not a old selected image)
if(mSelectedItemIndex!=-1 && mSelectedItemIndex!=position){
// Object v =mGallery.getItemAtPosition(mSelectedItemIndex);
// View v =imageAdapter.getDropDownView(mSelectedItemIndex,null,null);
// View v =mGallery.getChildAt(mSelectedItemIndex);
View v = (View)view.getChildAt(mSelectedItemIndex);
ImageView imgView = (ImageView) v.findViewById(R.id.adsimage);
// ImageView imgView = (ImageView)v;
// ImageView imgView = (ImageView)arg0.findViewById(R.id.adsimage);
imgView.setBackgroundResource(R.drawable.gallery_unselected_default);
//current selected image
//always this code is working fine.
ImageView imgView = (ImageView)view.findViewById(R.id.adsimage);
imgView.setBackgroundResource(R.drawable.gallery_selected_focused);
}
I used getItemBackground theme in base adapter but no use.Please give me how to persist image positions if gallery is scrolling form left to right and right to left.If i select one by one it is working fine.But when i select alternate image.It is not working.

Create a member variable eg View lastSelected in your activity to store the view when it has been selected/clicked. When a new item is selected/clicked use lastSelected to change the background and then update lastSelected with the newly selected view.
Hope that helps.

Related

change background resource of an image in the gridview in android

here am trying to change the background resource for the image, am success in that, but the problem is, when i clicking on all the items in the gridview every item has changes in the background resource, my question is if i click on item which is 0 position it has to change the background resource image, and again if i click on the item which is in 1 position it has to change the background resource and as well as a image on 0 position has to set normal image like without background resource comes to normal state.
public void onItemClick(AdapterView<?> arg0, View vv, int arg2, long arg3) {
// TODO Auto-generated method stub
vv.setBackgroundResource(android.R.drawable.btn_default);
}
Provide me to set only one selected item has to change the image.
Thanks in advance.
You need modify your adapter to set background for checked items. For Example:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// creating view
if(item.isChecked()){
veiw.setBackgroundResource(android.R.drawable.btn_default);
}
return view;
}
You actually need to modify the Adapter and for that i will refer you to this question here
How to set an image as background image on a click?
Check the accepted answer and do it as described and you are good to go

Customizing change Text and Background Color of row Item programmatically in Android

I have already fill adapter into Listview and it works fine.
My problem is how to change Text and Background color in specific row Item (using TextView) when selected.
#Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
int visiblePosition = listView1.getFirstVisiblePosition();
View view = (View)listView1.getChildAt(position);
.....
});
But it doesn't work as well.
By the way, please show me how to prevent loading data when scrolling in Listview (using custom adapter) or how to fill adapter into all row when first run.
it doesn't work because the children of a listview are being re-used each time you scroll.
you should either update the data and call notifyDataSetChanged , or update the data and use something like:
int visiblePosition = mListView.getFirstVisiblePosition();
View view = mListView.getChildAt(position - visiblePosition);
if(view!=null)
{
// update the view to have a different background and other stuff...
}
notes about the second way:
on some adapterViews the second way might not work as expected.
i'm not sure what will happen when changing the size of the view .

How to get another item then click on item in gridview?

I want to know how to get different item from selected item in gridview. I have gridview game of matching pictures. When i click on one image then that will flip and then when click on second image first will flip back. How to perform this animation? I want to get imageview of first selected image.
You can do that by saving the last view in a variable
View lastView = null;
// .....
gv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view,
int position, long arg3) {
if(lastView != null) {
// do something with the last view
}
// the rest of your code
lastView = view;
}
});
Not tested but should work.

Show dialog inside gridview

I've implemented a gridview of images that are picked from sdcard.Now i want every image to be shown in dialog on click.How to do it within Image Adapter?Any help will be appreciated.Thanks in advance.
You wouldn't do that in your adapter... Do it inside your click listener (from wherever you call and set the gridview adapter).
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
ImageView iv = (ImageView) v.findViewById(R.id.imageviewid); // get the resource id
Drawable image = iv.getDrawable(); // get the image
CreateYourDialog(image); // pass the image to a method to create your dialog
}
});
If your GridView is only the imageview for each cell, then you can skip the findViewById line of code and simply use Drawable image = v.getDrawable();.

Custom ListView adapter, strange ImageView behavior

I have a custom ListView Adapter, with which I'm creating rows for a list. My problem is though, that it doesn't seem to be distinguishing the ImageViews, from each other. It seems to be randomly picking ImageViews to chuck into place as I scroll up and down. The text information (omitted from this snippet) does not break. It works as one would expect.
Here is the relevant method of my Adapter:
public View getView( int position, View convertView, ViewGroup parent )
{
View v = convertView;
if( v == null )
{
LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate( R.layout.generic_row, null );
}
// find the image
ImageView favImage = (ImageView)v.findViewById( R.id.toggle_favorite );
// when clicked...
favImage.setOnClickListener( new OnClickListener() {
#Override
public void onClick( View v )
{
// make the gray star a yellow one
int newImage = R.drawable.ic_star_yellow_embossed;
((ImageView)v).setImageBitmap(BitmapFactory.decodeResource(getContext().getResources(), newImage));
}
});
return v;
}
That behavior appears because the ListView recycles the row views as you scroll the list up and down, and because of this you get rows that were acted on by the user(the image was changed) in position were the image should be unmodified. To avoid this you'll have to somehow hold the status of the ImageView for every row in the list and use this status to set up the correct image in the getView() method. Because you didn't say how exactly did you implement your adapter I will show you a simple example.
First of all you should store your the statuses of the ImageView. I used an ArrayList<Boolean> as a member of the custom adapter, if the position(corresponding to the row's position in the list) in this list is false then the image is the default one, otherwise if it is true then the user clicked it and we should put the new image:
private ArrayList<Boolean> imageStatus = new ArrayList<Boolean>();
In your custom adapter constructor initialize this list. For example if you put in your adapter a list of something then you should make your imageStatus as big as that list and filled with false(the default/start status):
//... initialize the imageStatus, objects is the list on which your adapter is based
for (int i = 0; i < objects.size(); i++) {
imageStatus.add(false);
}
Then in your getView() method:
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.adapters_adapter_with_images, null);
}
// find the image
ImageView favImage = (ImageView) v
.findViewById(R.id.toggle_favorite);
// Set the image bitmap. If the imageStatus flag for this position is TRUE then we
// show the new image because it was previously clicked by the user
if (imageStatus.get(position)) {
int newImage = R.drawable.ic_star_yellow_embossed;
favImage.setImageBitmap(BitmapFactory.decodeResource(
getContext().getResources(), newImage));
} else {
// If the imageStatus is FALSE then we explicitly set the image
// back to the default because we could be dealing with a
// recycled ImageView that has the new image set(there is no need to set a default drawable in the xml layout)
int newImage = R.drawable.basket_empty; //the default image
favImage.setImageBitmap(BitmapFactory.decodeResource(
getContext().getResources(), newImage));
}
// when clicked... we get the real position of the row and set to TRUE
// that position in the imageStatus flags list. We also call notifyDataSetChanged
//on the adapter object to let it know that something has changed and to update!
favImage.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Integer realPosition = (Integer) v.getTag(); //get the position from the view's tag
imageStatus.set(realPosition, true); //this position has been clicked be the user
adapter.notifyDataSetChanged(); //notify the adapter
}
});
// set the position to the favImage as a tag, we later retrieve it
// in the onClick method
favImage.setTag(new Integer(position));
return v;
}
This should work well if you don't plan to dynamically modify the list(remove/add rows), otherwise you'll have to take care of also modifying that list of imageStatus to reflect the changes. You didn't say what was your row data, another approach(and the right one if you plan to do something if the user clicks that image(besides changing it)) is to incorporate the status of the image in the row's data model. Regarding this here are some tutorials:
Android ListView Advanced Interactive
or Commonsware-Android Excerpt (Interactive rows)
you need to define the default image right after finding its reference:
// find the image
ImageView favImage = (ImageView)v.findViewById( R.id.toggle_favorite );
//setting to default
favImage.setImageResource(R.drawable.default_image);
// when clicked...
favImage.setOnClickListener....
you need to do this because once the image is changed, and you scroll the ListView , it reappears because ListView recycles the item views. So you need to define it in getView to use a default image when the list is scrolled

Categories

Resources