How to use Custom textColor for TextView inside Custom ListView? What i need is i have my listview with dark gray background. If i select any row. That row should become white and TextView Color should become dark gray. Can anybody give me snippet or lead me to some nice tutorial?
I tried this. Below is my custom row for ListView
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/list_selector" >
<TextView
android:id="#+id/rowListTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:text="#string/app_name"
android:textColor="#color/list_item_text"
android:textSize="10sp"
android:textStyle="bold" />
</RelativeLayout>
And this is xml from res/color/ folder
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="#color/dark_gray"/>
<item android:state_focused="true" android:color="#color/dark_gray"/>
<item android:state_pressed="true" android:color="#color/dark_gray"/>
<item android:color="#color/white"/>
</selector>
It doesnt workout for me. I am able to set row color to white but i am not able to set text color to gray!
i am new to android. help me with this. Thanks in advance!
Try this color state list for textColor
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#fff"/>
<item android:state_activated="true" android:color="#fff"/>
<item android:color="#000" />
</selector>
Android guide does not mention state_activated attribute but it works for me.
Related
This is my row_layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/selector_navigation_item_background"
android:paddingBottom="#dimen/dp12"
android:paddingLeft="#dimen/dp15">
<TextView
android:id="#+id/tv_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#drawable/selector_text_color"
android:textSize="#dimen/sp15"
android:layout_marginTop="#dimen/dp12"/>
</RelativeLayout>
Here is selector of TextView:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#android:color/white" android:state_checked="true"></item>
<item android:color="#android:color/white" android:state_selected="true"></item>
<item android:color="#color/light_green_color"></item>
</selector>
I want to change TextView color on list item click. How can I do so ??
Changing TextView selector to this worked:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/grey_color" android:state_pressed="true"></item>
<item android:color="#color/light_green_color"></item>
</selector>
If you want change textview's text color then you can try this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:color="your color here"/>
<item android:state_focused="true" android:color="your color here"/>
<item android:state_selected="true" android:color="your color here"/>
</selector>
and have to assign above to android:textColor="#drawable/selector_text_color"
esle if you want change text view' background color then try this:
tv_item.setBackgroundResource(R.drawable.tab_bg_selector);
I often do this to achieve it:
declare inside yourTextView xml: android:textIsSelectable="true" and android:clickable="true"
In your adapter getView, set yourTextView.setSelected(true or false); base on your own condition.
However, there will be some drawback with onClickListener with your TextView if you do as above. Therefor, I suggest that you should handle the checkstate yourself inside onClickListener in getView.
I am using a drawable to change the background and text of textViews inside of my navigation drawer. I would like to keep the background white for the text area, but by testing keeping the background white does not show the ripple effect on the background, instead it does it to the text making the text gray. In the picture below, the middle one is being pressed causing the ripple effect.
Here are my drawable files that are used to make the changes in colors
Background:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/selected" android:state_activated="true" />
<item android:drawable="#color/white" />
</selector>
Text:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/primary" android:state_activated="true" />
<item android:color="#color/primary_text" />
</selector>
textView layout file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25.0sp"
android:background="#drawable/activated_background"
android:textColor="#drawable/activated_text"
android:id="#id/text1"
android:layout_weight="0"
android:layout_marginTop="8dip"
android:layout_marginBottom="8dip" />
</RelativeLayout>
you should use 2 drawable files and use it as your view background.
for pre-lolipop versions:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/white_list_item_selected_background" android:state_pressed="true" />
<item android:drawable="#android:color/white" />
</selector>
for lolipop (v21):
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/white_list_item_selected_background" >
<item android:drawable="#android:color/white" />
</ripple>
If you use custom background, ripple effect will not be shown. You may have to use other ripple library such as Material Ripple.
In my case ripple effect is working after the first click, but for first click it didn't work for me. Have changed the background selector file with android:state_activated="true" and in main.xml android:clickable="true" then it's work fine for all time.
selector.xml (under res\drawable\selector.xml)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true" android:drawable="#drawable/card_bg_pressed" android:state_enabled="true" android:state_pressed="true"/>
<item android:state_activated="true" android:drawable="#drawable/card_bg_focused" android:state_enabled="true" android:state_focused="true"/>
<item android:state_activated="true" android:drawable="#drawable/card_bg_selected" android:state_enabled="false" android:state_selected="true"/>
</selector>
In activity_main.xml
<com.mysample.RecyclingImageView
android:id="#+id/imageview_overlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#drawable/selector"
android:clickable="true" />
Is it possible to define state list for compound drawables in Android?
This is my button in layout xml:
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="#drawable/btn_top"
android:text="#string/button_text" />
This is my drawable xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_enabled="true" android:drawable="#drawable/btn_top_enabled" />
<item android:drawable="#drawable/btn_top_disabled" />
</selector>
This works for background, but its not working for drawableTop. The first state is always displayed. Is this even possible in Android, and if so am I missing something?
look ad TextView.java source code and you will see that yes, you can use StateListDrawable as a compound Drawable that displays according to TextView state
Try:
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_enabled="true" android:drawable="#drawable/btn_top_enabled" />
<item android:state_activated="true" android:drawable="#drawable/btn_top_enabled" />
<item android:drawable="#drawable/btn_top_disabled" />
</selector>
And
button.setActivated(true);
I can't figure out what I am doing wrong...
I have a ListView with a custom layout.xml file. In there, i define a TextView like this
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="foo"
android:textColor="#drawable/listitem_textcolor_selector"/>
The listitem_textcolor_selector.xml looks something like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#ff0000" />
<item android:state_selected="true" android:color="#ff0000" />
<item android:state_focused="true" android:color="#ff0000" />
<item android:color="#000000" />
</selector>
This kind of works. If I select a row, it will properly change the color of the text to red. The only problem is, that it will not stay red. After a second or so, that color will change back to black.
The main problem here is that the background of the row will change it's color and this color will stay, but the color of the text does not, even though the selector for the list item itself looks identical (expect the colors).
Can anybody tell me what I am missing?
Any help is appreciated as I have no idea on how to fix this :)
Thanks
Edit:
Maybe I should also point out that I am testing on a Samsung Galaxy Tab 10.1 tablet. I once heard something about "TouchMode" without really knowing weather this will have something to do with my problem...
In the listitem_textcolor_selector.xml add these states:
<!-- Activated -->
<item
android:state_activated="true"
android:color="#ff0000" />
<!-- Active -->
<item
android:state_active="true"
android:color="#ff0000" />
After this, the selected item will keep it's color state until something else is selected.
Hope this helped.
I win the problem!
In selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true" android:color="#ff0000" />
<item android:state_active="true" android:color="#ff0000" />
<item android:state_pressed="true" android:color="#ff0000" />
<item android:state_selected="true" android:color="#ff0000" />
<item android:state_focused="true" android:color="#ff0000" />
<item android:color="#000000" />
</selector>
In activity: myListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
pl=position;
myListView.setItemChecked(pl, true);
adapter.notifyDataSetChanged();
.....
item.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:textColor="#drawable/selector"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
/>
And my checked item in red color after i click them. Only one checked item. Enjoy!
I got the trick. if you are using a custom xml for your list item,then you can change text color like:
custom_listitem:
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dip"
android:paddingRight="10dip"
android:paddingTop="7dip"
android:paddingBottom="7dip"
android:textSize="18sp"
android:textStyle="bold"
android:textColor="#drawable/listitem_textcolor_selector"
/>
And now you will have to use this xml name to your array adapter which you use to populate your listview.
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.custom_listitem, options);
listView.setAdapter(adapter);
save listitem_textcolor_selector.xml in the color folder of your res and then use it like this: android:textColor="#color/listitem_textcolor_selector"
I'm developing a ListView app for Android. I want to use the default ListView selector, for example the one in Market when you touch an item.
I don't get a highlight in my ListView, where's the way to correctly implement it? I've read about the "Touch mode" although I can't get it to work.
Thanks in advance!
<ListView
android:id="#id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
You have to assign to each row layout a background composed by a selector that assign a different background for each state. Suppose that your item row layout is a RelativeLayout defined in a xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dip"
android:background="#drawable/transparent_selector"> ...
and transparent_selector is defined here: watch that carefully one drawable shape belongs to a single state.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="#drawable/shape_7"/>
<item android:state_pressed="true" android:drawable="#drawable/shape_7" />
<item android:state_pressed="false" android:drawable="#drawable/transparent_shape" />
</selector>
and a shape is defined here:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#FF4FDBD5"
android:endColor="#FF1AA6A1"
android:type="linear"
android:angle="90"/>
</shape>
or in the other way when you construct the ListView items you can assign the selector to the 'convertView'
cheers
For your id attribute, change it to android:id="#android:id/list. This is the default ID for the ListView.
Try this selector. It works for me
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_selected="true" android:drawable="#android:color/transparent" />
<item android:state_selected="true" android:drawable="#android:color/transparent" />
<item android:state_pressed="true" android:state_selected="false" android:drawable="#android:color/transparent" />
<item android:state_selected="false" android:drawable="#drawable/list_item_back" />
</selector>