Change background color of listview - android

I am using ListView inside a ViewFlipper as follows:
<ViewFlipper
android:id="#+Category/viewFlipper"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:entries="#array/options"
android:cacheColorHint="#FFFFFF"
android:background="#FFFFFF"/>
</ViewFlipper>
I want to change the background color of the ListView.
I have only 5 items in the options array and they do not fill the screen. So, the remaining part of the listView appears in the default grey color. I want to change this default color. I read about cacheColorHint attribute in questions posted by others related to this. But it did not work out. Can anyone please help me with this?
Thanks in advance

You should use layout_height="wrap_content" (and layout_width="wrap_content" also if need) for your ListView and change the "default grey color" like you said by set the background for ViewFlipper.

Set a selector as the background of your ListView:
ListView mylw = findViewById(R.id.mylistView);
//Do not use reserved android or java identifiers as your id or variable!
mylw.setBackgroundResource(R.drawable.thexmlbelow);
Drawable:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false" android:drawable="Background Color" />
<item android:drawable="Background Color when pressed" />
</selector>

Related

ListView items' borders change color (like temporally highlighted or flicker) when clicked?

Each item in the ListView consists of a CardView that has a TextView inside.
This is not a bug this is just the normal way android listview shows that the item was clicked (like highlighting it till you release touch). (u have seen it right?)
But i dont want this functionality as I handle the clicks manually. How to remove this?
To remove selector color try this(add transparent list selector color):
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:listSelector="#android:color/transparent"
/>
To change it, try below:
First create a selector XML (listitem_selector.xml) file in your drawable folder,
<!-- pressed state-->
<item android:drawable="#drawable/button_selected_state" android:state_pressed="true"/>
<!-- focused state-->
<item android:drawable="#drawable/button_selected_state" android:state_focused="true"/>
<!-- default state-->
<item android:drawable="#drawable/button_normal_state"/>
Then in your Layout XML, Find your listview and add
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:listSelector="#drawable/listitem_selector"
/>
Or you can add selector for your row items.
For that, you need to set background drawable of the row layout as
android:background="#drawable/listitem_selector"
and you need to set your listview's list selector like that:
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:listSelector="#android:color/transparent"
/>

How to make ListView not respond (change background) on touch?

I have a ListView which displays some inert items, so this should not change their backgrounds when a user touches the list view or item. This turns out hard to do:
What i have tried so far:
Java:
listView.setLongClickable(false);
listView.setClickable(false);
listView.setFocusable(false);
This has no effect what so ever. If I touch listview the background changes.
In the xml I tried;
<ListView
android:id="#android:id/list"
android:choiceMode="none"
android:clickable="true"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:listSelector="#null" />
Setting listSelector to #null is something I saw elsewehere on stack overflow. Does not work.
I also tried setting each item's background in code to transparent.
convertView.setBackgroundResource(R.color.transparent);
Nothing. On every touch there is always a change in background to blue.
How to make my listview not change item background on touch?
Try This, It works For Me
<ListView
android:listSelector="#android:color/transparent"
android:cacheColorHint="#android:color/transparent"
/>
You can set view.setEnabled(false); in your Adapter getView() - it will remove background effects.
You are probably using a layout for your list items that has a background which has a different selected state.
See what layout you use for the list items and change that to not include that background. If you are using the default (android.R.layout.simple_list_item) layout, you can create and use your own layout.
for this you need to make a custom background for the listview like
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="false"
android:drawable="#android:color/white" />
<item
android:state_pressed="true"
android:state_enabled="true"
android:drawable="#android:color/white" />
<item
android:state_focused="true"
android:state_enabled="true"
android:drawable="#android:color/white" />
<item
android:state_enabled="true"
android:drawable="#android:color/white" />
</selector>
place this xml file with name listbg.xml inside your drawable folder
<ListView
android:id="#+id/listt"
android:choiceMode="none"
android:clickable="true"
android:background="#drawable/listbg"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:listSelector="#null" />

Listview dividers color with HoloEveryWhere

I'm using HoloEveryWhere to get support of the Holo themes on android 2.x and I want to change the default color of my ListView dividers.
I did this :
<ListView
android:id="#+id/listRecherche"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_width="wrap_content"
android:divider="#e5e5e5"
android:dividerHeight="1dp"
android:layout_height="wrap_content" >
</ListView>
It works well on android 4.x but in 2.x, what I get is no more dividers and instead a #e5e5e5 background on the whole ListView.
I've thought about a height problem since I know that changing the dividers color resets the dividers height. This is why I've set heights at the end... but no effect.
Use a drawable instead of an RGB color Just put a file named divider.xml in res/drawable/ so you can access it as R.drawable.divider; if you can access it that way, then you can use android:divider="#drawable/divider" in the XML for ListView.
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="schemas.android.com/apk/res/android">
<gradient
android:startColor="#ffcdcdcd"
android:endColor="#ffcdcdcd"
android:angle="270.0" />
And in styles.xml for listview item
<item name="android:divider">#drawable/divider</item>
<item name="android:dividerHeight">1px</item>

Android listview entire list getting selected

I seem to be having a UI problem with a listview. I have a listview and selecting any item on the list, highlights the entire listview. The selection is working fine, I get the correct item in the listener.
But the problem is that when i select any item, the entire listview gets highlighted, so it is difficult to tell which row got selected.
This is working fine on Android >3.1 - Currently i am testing on a 2.3 device.
<ListView
android:id="#+id/myList"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginTop="#dimen/margin_medium"
android:background="#drawable/border"
android:listSelector="#99000000"
android:scrollbarFadeDuration="1000000"
android:scrollbars="vertical" >
</ListView>
I recently had the same issue, but the reason was in Android < 3.0 bug: if you set a #color as a drawable for your list item pressed selector, then it will fill the entire list area. Solution is to use a 'shape' drawable instead of #color.
So, create a new XML file in res/drawable folder with similar content:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#color/my_list_item_pressed_color" />
</shape>
And reference created drawable in your ListView selector for state_pressed (also created as an XML file in res/drawable folder):
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:drawable="#drawable/list_item_pressed_bg" />
<item android:drawable="#android:color/transparent" />
</selector>
And use this selector in your ListView:
<ListView
.....
android:listSelector="#drawable/list_item_selector" />
That's all. Works at least with Android 2.2-2.3.
If you don't want to change your selector, you can also set it as the background of the list item's layout, rather than in the listSelector field of the ListView. The list items receive the "selected" state when touched and the color is displayed properly.
This works for all versions of Android.
Ex: This is your list item layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/background_color">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Your text here" />
</LinearLayout>
And your list with no list selector:
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
I fixed this by removing this line: (not sure why it worked)
android:listSelector="#99000000"

Listviews text-font

I have a issue with my listview, I basicly want the text in the fields (in the listview) to become black instead of white as default. I've followed a simple guide how manage this but it is not working for me. This is the guide: http://www.hascode.com/2011/12/writing-styles-and-themes-on-android/ .
This is my style.xml file were I try to enter a dark color to the text:
<style name="CustomTextView" parent="#android:style/Widget.TextView">
<item name="android:typeface">monospace</item>
<item name="android:textColor">#000000</item>
</style>
This is my bookmarks.xml were I try to load the style in the listview with following code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/CustomTextView">
</ListView>
</LinearLayout>
I've also tried to change the parent name in the style.xml to TextAppearance.Medium instead, but nothing works. Am I missing smomething, why canĀ“t I just change the damn color? Please help! Thanx!
you should set the style="#style/CustomTextView" to the UI element that represent your listview's row (the TextView where you will put the text, eg)
ListView is a kind of container... Inside list view you add the views.. in your case you are adding TextViews... so you should set the style of that TextView which you are adding to this ListView...

Categories

Resources