I am working on an android application. I have created a listview by using
setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arrayname));
getListView().setTextFilterEnabled(true);
Now i want to change the selected item's color. I could change the background of the selected item by placing
listviewobject.getChildAt(position).setBackgroundColor(Color.BLACK);
in onListItemClick()
this code is changing the background color but if I select any other list item then also the previously clicked list item's color is red.So I change the previously clicked listitem's color by
l.getChildAt(prevpos).setBackgroundColor(Color.BLACK);
now the problem is if i change the background of previously clicked listitems color to black.Then i can't see the text on that particular listitem.I i click again then only i can see the text on that item.So its look weired.please help me friends
After going through lots of posts and blogs i found this solution it works for me...
declare row variable global
public View row;
your_list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v,
int position, long id) {
if (row != null) {
row.setBackgroundResource(R.color.orange);
}
row = v;
v.setBackgroundResource(R.color.transparent_green);
)};
Well using this in the properties of your list view mught help:
android:listSelector="#drawable/list_selector"
It will automatically set the selected drawable as background of the selected item. Hope this works for you and drawable may be your own choice's color.
Explanation: Add an image strip to your drawables folder/s and set that image in listSelector attribute of the your list view. Now you will navigate through your list view, the list view's background will be of the color of the image strip you set instead of android's native color. Hope you get it now...:-)
I this This happening because you have put text color as black and your setting the background color also black that's why you can't see the difference. for setting the background color you can use the following line.
view.setBackgroundColor(Color.YELLOW);
use different color then text color.
By default the background is black. If you have customized your listview then when you scroll it would turn black. So when you define your list view set background cache color to the color you need as below:
yourlistView.setCacheColorHint(Color.WHITE);
What you can do is instead of using .setBackgroundColor() with a Color value, create a Color State List and assign it with .setBackgroundResource(). That way, you can define the various states that your list item can become, depending on the item's current state.
Related
I have an Activity with a ListView, I set the background image to the view programatically.
When I scroll down my list, the background image turns to white ( because my theme is Theme.Light.NoTitleBar).
How can I make it scroll with the blue background intact?
If the above point works, how can I change the text color of ListView to white instead of black?
Normal ListView
Scrolling ListView
Pressing ListView item
Use the attribute android:cacheColorHint="#000000" in ListView Tag
Regarding make TextView's color black or white, you can refer here to make a custom TextView for your ListView row, The extra work you have to do is just add another attribut inside TextView tag like this
android:textColor="#FFF" //or #FFFFFF for white
add a attribute on the ListView Tag
android:cacheColorHint="#00000000"// setting as a transparent color
This is due because of an optimization. To remove this, just set the cacheColorHint of your listView to transparent, like this: android:cacheColorHint="#00000000"
Full post here: http://android-developers.blogspot.fr/2009/01/why-is-my-list-black-android.html
You can Wrap that ListView inside on LinearLayout having same background and then remove ListView's Background that should work fine. :)
Please check this answer. I have got the same issue and it is fixed by putting view = null in adapter side.
I have to show list view as like below image
Here I have the view in the xml as like this
<View
android:id="#id/margin"
android:background="#6DAAE9"
android:layout_width="20.0dip"
android:layout_height="fill_parent" />
I Have to change this background (#6DAAE9) dynamically when list is loaded and I have to fix these colors to corresponding item even the list items are increased.
Can any body help me. Thanks in advance
Basiclly you need to edit your getView method.
you can use
convertView.setBackgroundColor(Color.parseColor("#6DAAE9"));
With your appropriate logic, meaning if you want it to be random set random color or hold an array with the order, etc.
See this listview I added list item as per odd or even row and you can do it checking position in getView() method.And you should put imageview for that and set color rather that set color to row.
if(position==0){
//here set color for imageview which position is 0
}else if(position==1){
//here set color for imageview which position is 1
}
....
I am using the this code to have section/headers in list.
http://w2davids.wordpress.com/android-sectioned-headers-in-listviews/
I am setting the color with 50% alpha to list header and my window background color is transparent. So while scrolling header color becomes dark. Any idea how to overcome this.
and i have also set android:cacheColorHint="#00000000".
I had the same problem. I set the background color of the header view to the same background colour as the list's parent and it worked.
So I had a LinearLayout with a background colour of 'grey'.
The list is contained within this layout.
The list had android:cacheColorHint="#00000000" set in the XML and no background colour set.
The header view is loaded using LayoutInflater, inside my activity and set before the call to the list's setListAdapter.
The parent View (or main View) of the list header was also a LinearLayout with a background colour of 'grey'.
Hope this works for you.
I'm trying to restore the background Color of a View.
I have several selectable Views. When the user clicks one of those Views, the following code is executed and the View becomes Yellow:
View newSelection, previousSelection;
...
if(previousSelection != null) {
previousSelection.setBackgroundColor(Color.BLACK); // problem here
}
newSelection.setBackgroundColor(Color.YELLOW);
However, I want to reset the color of the previously selected View. However, I do not know which color it was (I'm setting it to Color.BLACK in the above code). I was not able to find a getBackgroundColor or similar method in the View class. If I had it, I could save the previous color and just put it back when the new View is selected.
use View.getBackground(), it returns the current 'Drawable' background of the view which can then be used in View.setBackgroundDrawable()
View theView;
Drawable originalBackground;
...
originalBackground = theView.getBackground();
theView.setBackgroundColor(Color.YELLOW);
...
theView.setBackgroundDrawable(originalBackground);
I'm not sure exactly what you are trying to accomplish but perhaps a ColorStateList would come in handy here.
You can try setting the previous color as a tag of the view.
For example
View newSelection, previousSelection;
newSelection.setTag(Color.Green);
previousSelection.setTag(Color.Black);
if(previousSelection != null) {
previousSelection.setBackgroundColor((int)previousSelection.getTag());
}
newSelection.setBackgroundColor(Color.YELLOW);
I haven't tried the code if there is an error but the flow on how to implement is there.
I've a border set around a drawable using LinearLayout (bg: rounded rectangle).
The drawable and the border is used as a tab view. I'd like to change the border color of the tab when it is selected.
How can I do this?
Color state list doesn't seem to work as the view being selected is not the shape (i.e border) but the tab. Drawable state list doesn't seem to work either as I'm trying to swivel between views, not drawables. Moreover, I can't find any "onSelectedListener" of the sort...
You need to make an OnClickListener to handle clicks, and get it to change the appropriate background:
private OnClickListener tabClick(Context mContext){
#Override
onClick(View v){
v.setBackgroundResource(R.drawable.active);
}
}
myTab.setOnClickListener(tabClick);
The solution is to call setOnTabChangeListener in the TabHost and then change everything manually.