I want to have an onClick that the chatlist-item changes it's color when clicked/focused. I don't want to use any Java code if possible.
I tried this:
chatlist_layout.xml
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="false"
android:layout_alignParentRight="false"
android:background="#drawable/round_corners"
android:listSelector="#drawable/selector">
</ListView>
selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="#color/ripple_material_light" /> <!-- focused -->
<item android:state_focused="true" android:state_pressed="true" android:drawable="#color/green_hsrt_1_default_CMYK_100_10_55_0" /> <!-- focused and pressed-->
<item android:state_pressed="true" android:drawable="#color/grey" /> <!-- pressed -->
<item android:drawable="#color/black" /> <!-- default -->
</selector>
what am I doing wrong?
Any kind of help will be greatly appreciated
The problem may be in ListView background. So try to add android:drawSelectorOnTop="true"
Related
If I select ONE row in my ListView, it is highlighted but whenever I enter the MultipleSelection mode (onLongClick, # selected rows, Contextual Action Bar), other rows I select don't remain highlighted BUT is selected according to CAB as it says, "3 selected rows".
main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
...
>
<HorizontalScrollView
...
>
<LinearLayout
...
>
<TableLayout
...
>
<TableRow
...
>
<TextView
...
/>
<TextView
...
/>
</TableRow>
</TableLayout>
<ListView
android:id="#+id/db_op_data_grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:listSelector="#drawable/rowselector"
android:divider="#android:color/transparent" />
</LinearLayout>
</HorizontalScrollView>
</FrameLayout>
rowselector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true"
android:drawable="#drawable/select_button_selectable" />
<!-- focused -->
<item android:state_focused="true" android:state_pressed="true"
android:drawable="#drawable/select_button_selectable" />
<!-- focused and pressed-->
<item android:state_pressed="true"
android:drawable="#drawable/select_button_selected" />
<!-- pressed -->
<item android:drawable="#drawable/select_button_selectable" />
<!-- default -->
</selector>
EDIT: The image looks like this:
Selected Rows but not Highlighted
You need to add state_selected in your selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/select_button_selectable" android:state_focused="true" />
<!-- focused -->
<item android:drawable="#drawable/select_button_selectable" android:state_focused="true" android:state_pressed="true" />
<!-- focused and pressed-->
<item android:drawable="#drawable/select_button_selected" android:state_pressed="true" />
<!-- pressed -->
<item android:drawable="#drawable/select_button_selected" android:state_selected="true" />
<!-- selected (ADD THIS) -->
<item android:drawable="#drawable/select_button_selectable" />
<!-- default -->
</selector>
I am beginner in android, and of course i have a problem with ColorStateList.
I want to set it to ListView, but it doesn't work, maybe someone know why?
it is declaration of TextView
<TextView
android:textColor="#color/text_color"
android:id="#+id/tv"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/in"
android:onClick="onClick"
android:clickable="true"
android:duplicateParentState='true'/>
and content of text_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#ffff0000"/> <!-- pressed -->
<item android:state_focused="true"
android:color="#ff0000ff"/> <!-- focused -->
<item android:color="#ff000000"/> <!-- default -->
</selector>
It is code from StackOverflow reply, should work :/
Try by setting listview.setItemsCanFocus(true);
I have a custom dialog which extends the Dialog class and a simple custom layout like the one from below:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:drawable/list_selector_background"
>
<TextView
android:id="#+id/text_view"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:background="#color/transparent"
android:textColor="#color/black"
android:textSize="16sp"
android:focusable="true"/>
My problem is that the trackball makes the text not visible when a row is selected like in the attached image.
The list_selector_background code is from Android and it looks like this:
<?xml version="1.0" encoding="utf-8"?>
<item android:state_window_focused="false" android:drawable="#color/transparent" />
<!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. -->
<item android:state_focused="true" android:state_enabled="false" android:state_pressed="true" android:drawable="#drawable/list_selector_background_disabled" />
<item android:state_focused="true" android:state_enabled="false" android:drawable="#drawable/list_selector_background_disabled" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#drawable/list_selector_background_transition" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#drawable/list_selector_background_transition" />
<item android:state_focused="true" android:drawable="#drawable/list_selector_background_focus" />
And my ListView xml code is this:
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="true"
android:background="#color/white"
android:cacheColorHint="#color/transparent">
</ListView>
I saw that on an AlertDialog (the one from Android) is working okay. What should I do to solve this problem?
try this: remove "android" text from this line. like;
android:background="#drawable/list_selector_background"
add in list view
android:cacheColorHint="#android:color/transparent"
android:drawSelectorOnTop="false"
android:listSelector="#android:color/transparent"
android:fadingEdge="none"
this one work for me
Hi I am using listview which have multiple items adding dynamically..
I want to change the text color of listitem any idea
<?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="wrap_content"
android:background="#659EC7">
<ImageView android:id="#+id/icon" android:layout_width="72px"
android:layout_height="wrap_content"
android:layout_marginTop="5px" />
<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="5px"
android:paddingBottom="5px">
<TextView android:id="#+id/item1" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView android:id="#+id/item2" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:paddingTop="30px" />
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#android:color/darker_gray"/>
</TwoLineListItem>
</LinearLayout>
I am using 2linelistIem
Here is the code I wrote and used:
It keeps the android style for all other states. it just overlays the transparent state with your desired color:
Layer list drawable for list view with custom background
as you can see in my question there is a minor bug with the focused state, but it is still the best solution i ever saw :D
Use this type of technique to change the color dynamically:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="#color/black" /> <!-- focused -->
<item android:state_focused="true" android:state_pressed="true" android:drawable="#color/black" /> <!-- focused and pressed-->
<item android:state_pressed="true" android:drawable="#color/green" /> <!-- pressed -->
<item android:drawable="#color/black" /> <!-- default -->
</selector>
I have created a ListView and applied a selector to it as follows
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/my_btn_pressed" />
<item android:state_focused="true"
android:drawable="#drawable/my_btn_focussed" />
<item android:drawable="#drawable/my_btn_normal" />
</selector>
When focussed or pressed, the background of the ListView item comes as specified in the selector. But the default background is never applied, can you tell me what is wrong?
By the way, this is the customised row xml I've used:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textColor="#FFFFFF"
android:textStyle="bold"
android:textSize="20sp" >
</TextView>
Thanks,
Kiki
The solution was presented as an answer to this question.
Try using this code:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:state_focused="false" android:drawable="#drawable/my_btn_pressed" />
<item android:state_focused="true" android:state_pressed="false" android:drawable="#drawable/my_btn_focussed" />
<item android:state_focused="false" android:state_pressed="false" android:drawable="#drawable/my_btn_normal" />
</selector>
In your Textview, you should try to change this...
android:background="#drawable/<b>my_btn_normal</b>"
to this...
android:background="#drawable/<b>your selector's name</b>"