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>
Related
I would like to add arrows as dividers between my listview items. I've gotten pretty far, but my arrow is stretched and there is no option to set the divider width. Here is what I have so far...
In my styles.xml:
<style name="dividedListStyle" parent="#android:style/Widget.ListView">
<item name="android:cacheColorHint">#android:color/transparent</item>
<item name="android:divider">#drawable/baseline_expand_more_black_24</item>
<item name="android:dividerHeight">50dp</item>
</style>
Then in my layout file:
<ListView
android:id="#+id/checklist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/menu"
android:layout_alignParentTop="true"
style="#style/dividedListStyle">
</ListView>
This is what the divider looks like:
This is what I need:
The ListView divider is stretched to full width of the ListView and your selected height (<item name="android:dividerHeight">50dp</item>). That's why your image is stretched.
To avoid it and keep the size/ratio unchanged, there are such ways:
Use 9-patch drawable. You may use a default Android 9-patch editor to convert your arrow to 9-patch (you still need some basic knowledge about how 9-patch works)
Make your arrow drawable a part of the drawable or a layer-list drawable:
<?xml version="1.0" encoding="utf-8"?>
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center"
android:src="#drawable/baseline_expand_more_black_24">
</bitmap>
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>
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"
I created a custom layout (using LayoutInflator) for list items. Here are the relevant files:
my_list.xml - the main activity which has the ListView
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:listSelector="#drawable/listitem_selector"
>
</ListView>
list_item.xml - references drawable .png for background
android:background="#drawable/list_item_bg"
listitem_selector.xml
<selector>
<item android:state_focused="false" android:drawable="#drawable/list_item_bg" />
<item android:state_pressed="true"
android:drawable="#drawable/list_item_pressed_bg" />
<item android:state_focused="true"
android:drawable="#drawable/list_item_pressed_bg" />
</selector>
If I don't use the listitem_selector.xml to override the default state styles, I get the ugly default Android green/orange colors, but otherwise the layout looks fine. When I use the listitem_selector.xml, it adds extra space at the top of each list item. (if I use smaller images, it still adds space)
here's a screenshot: [edit - not screenshots allowed.]
How can I override the default list item state styling without adding the weird space?
p.s. - I referenced this very helpful post to get where I got.
I want to change the color of child divider of ExpandableListView by writing:
android:childDivider="#drawable/yellow"
in layout file. However, when I collapse the item, I found the background of the ExpandableListView turn yellow (#drawable/yellow) , but I just want to change the color of child divider. Who can tell me why? To my surprise, if I change it by java code like
expandableListView.setChildDivider(this.getResources().getDrawable(R.drawable.yellow));
it works normally. It is very weird, who can tell me the reason?
<!-- if I set childDivider in Layout xml, it can't work normally.
However, if set childDivider in java code, it work normally -->
<ExpandableListView android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:dividerHeight="2dp"
android:divider="#drawable/yellow"
android:childDivider="#drawable/yellow"
/>
create a drawable with small height and set it to childDivider
property
"child_separator.xml":
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#color/child_separator_color"/>
<size android:height="1dp"/>
</shape>
expandable list view:
android:childDivider="#drawable/child_separator"
Simply set child divider as the color of your choice.
<ExpandableListView
android:id="#+id/list_shopping"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:childDivider="#color/LightGrey"
android:dividerHeight="1dp" />
You just need to remove android:divider="#drawable/yellow" from your layout. This should resolve your problem if I understood it correctly.
Here is what I mean: