change background resource of an image in the gridview in android - 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

Related

Get access to particular item's layout in list view

In my android app my Main Activity extends List Activity. I store there elements called Items, their layout is defined by itemLayout xml file and I use custom adapter (called ItemAdapter) to transfer data to List View in Main Activity. In itemLayout there's an ImageView and my aim is to change its image when user clicks on the particular item in list view. In my opinion the easiest way to achieve that is to get access to particular item's (the one that was clicked) layout, there find the ImageView and call method setImageBitmap. How can I "find" this layout of clicked item? I tried many things in method:
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
}
but nothing worked. Do I have to add anything to my ItemAdapter? Or is it possible to do in onListItemClick(…) but I can't find out how?
You're thinking about it in slightly the wrong way. Adapter's are a way to map data to views. So if you want to change how a particular view looks for a given position, you need to change it's correlating data so that the rendered View then changes. Attempting to modify a view directly kinda goes against how adapters are meant to be used.
So in your case, when a user clicks on an item. Find that item in your adapter via the position number. Then update the item and ensure notifydataset is called. Meanwhile, your adapter's getView() will then handle displaying the appropriate image.
As I understood you need to modify clicked item layout, right? Use argument from onListItemClicked:
v.findViewById(<your_img_view_id>)
For better performance use view holder.
onListItemClick is fired when you press on an element of the ListView. If you want to retrieve the element in the dataset, you can simply invoke
l.getItemAtPosition(position)
the returned value has to be casted to the specific object
Yes It is possible in your custom adapter class in the getView() mtheod u can change imageview bitmap by clicking on it
see this code
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View rowView = convertView;
rowView = inflater.inflate(R.layout.playlist_item, null);
final ImageView im = (ImageView) rowView.findViewById(R.id.favorite);
im.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Do your stuff here
}
});
return rowView;
}

Android ListActivity using SimpleAdapter - Highlight selected item

I´m using SimpleAdapter in my ListActivity and I want when user select an item highlight this item. I tried extends SimpleAdapter and override getView() method:
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
if (position == mItemIndex) {
convertView.setSelected(true);
convertView.setPressed(true);
convertView.setBackgroundColor(Color.parseColor("#FF9912"));
}
return view;
}
but this solution don´t work properly. It set background color to more then one list row.
Can sameone help me?
That's because the convertView is being reused and you do not update the selected state for both cases (selected / not selected). You need to call setSelected(false) when it is not the item you want selected and reset the background color. Also, the call to setPressed is not needed.
You also should checkout the ColorStateList which will allow you to define the colors for various states. Then you could just use the built in support for single item selection.
So instead of (e.g.) opening a new Activity you want to highlight the selected list item by changing its colors if the user clicks on it?
In your ListActivity you need to override onListItemClick()
#Override
protected void onListItemClick(ListView list, View view, int position, long id) {
super.onListItemClick(list, view, position, id);
TextView tv = (TextView)view;
tv.setTextColor(Color.RED);
tv.setBackgroundColor(Color.BLUE);
}
This changes the color of the selected entry to red (text) and blue (background).
Thats the first step. But the first clicked entry stays this way, even if you click another one. So you need to change that (somehow).

How to set background color for all elements of ListView on OnItemClick event?

I have a ListView and posibility to select one element (single choice).
How can I set background color for all elements of ListView (maybe which are visible at least) when some item was selected?
adapter = new ArrayAdapter<Orderline>(activity, simple_list_item_single_choice, orderlines) {
#Override
public View getView(final int position, View convertView, final ViewGroup parent) {
...
convertView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
convertView.setBackgroundColor(BLACK);
// so here currently selected element is set to BLACK, but also other elements have to be set to WHITE
}
});
return convertView;
}
}
I don't have much code for you because I'm not at my workstation right now, but I'm thinking you can just set the background of the selected item to black by via your onItemClick as you've already suggested. Cool.
To change the color of the other(unselected) views when a particular view is selected, I'm guessing you can call your Adapter's getCount() and loop through that list, make a call to getChildAt(i) of your ListView. This returns a View which you can call setBackgroundColor(Color) on. Hope this helps
You can do
parent.setBackgroundColor(BLACK);
this.notifyDataSetChanged();
to set your list view background.
I would use a Drawable selector.
Check this link for a pretty good example:
http://www.charlesharley.com/2012/programming/custom-drawable-states-in-android/
Basically you create the XML drawable which contains a mapping (which the system uses automatically) to what you would like displayed when certain click events occur.
You can assign the background drawable to any view, so you could do it in your XML for your adapter, or in Java code. Which in your case might be something like this:
convertView.setBackgroundDrawable(R.drawable.MySelector);
parent.setBackgroundColor(while_color);
v.setBackgroundColor(black_color)

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 .

Android ListView itemClick Issue

Can someone explain this issue to me ?
I have a listview that holds more rows than the screen can show, so scrolling.
If I click on one item, I replace an icon that is part of each row. That all works.
The issue I have is that when I click on lets say the first item, I change the icon for that first row. When I now scroll down I see that the first row outside the visible viewport also changed the icon.
Why is that happening and how can I avoid this issue ?
Thanks in advance,
Mozzak
Just to make sure, you are using a class that implements ListAdapter or extends some other sort of adapter right?
When using an adapter, you will have to keep in mind that the views in the ListView are recycled to save memory. Because of this, you will need to store the state in a separate variable.
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
{
LayoutInflater inflator = (LayoutInflater)parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflator.inflate(R.layout.listitem, null);
}
// Retreive my image that may or may not change
ImageView myIcon = (ImageView) convertView.findViewById(R.id.iconView);
// Checking my stored boolean for this position to see if I need to use icon2 or icon
if (myItem[position].needsIconChanged)
{
// I have set my boolean, so use icon2
myIcon.setImageResource(R.drawable.icon2);
}
else
{
// I have not set my boolean, or set it to false so set it to icon
myIcon.setImageResource(R.drawable.icon);
}
return convertView;
}
You will also have to remember to set that boolean in your onItemCLick
public void onItemClick(AdapterView<?> myAdapter, View myView, int position, long arg3) {
// Retreive your item and set a boolean or icon state (depending on what you do)
myAdapter.getItemAtPosition(position).needsIconChanged = true;
}

Categories

Resources