I'm working with list view, and try to achive desire look. Long stroy short - I want to change list item text View color when item is pressed. Default text color for textView is White, and when I press item in listView text Color automaticly change to Black. It is default settings and it works pretty well. But when I set color in text View, for example in Red then nothing happen when item in list View is pressed. I try to set color selector for Text View but it doesn't work(even if I set duplicateParentState=true)
I Just want some default text color and diffrent text color when user press item.
Thx for help.
put this file in res/color folder
color_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false"
android:color="#color/skyblue" />
<item android:state_focused="true" android:state_pressed="true"
android:color="#color/white" />
<item android:state_focused="false" android:state_pressed="true"
android:color="#color/white" />
<item android:color="#color/skyblue" />
</selector>
now set this selector in your Textview's textcolor property
prepare a color.xml in "values" folder
color.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="white">#ffffffff</color>
<color name="blue">#39abdf</color>
</resources>
You have to override the baseadapter. It is not difficult. This will allow you to target each element in each row and even allow for clicking of buttons and such on each row.
Here is one link. I know there are more. Ill post them if I come across them again.
Android: ListView elements with multiple clickable buttons
Android : BaseAdapter how to?
You can override the onItemClick() event and use the View that is given to you in the method. Using that, you can call View.findViewById(R.id.textview) for each of the textviews to change their font and font color.
Related
I have an imageButton. I created selector XML to change its background when it's pressed ... like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#color/default_blue" />
</selector>
and I used this in java with :
tempIBtn.setBackgroundResource(R.drawable.buttons_drawable_resource);
I have a textview and a pic on the image button (in addition to image of this imageButton.)
I want to change the color of this textview and pic when the imageButton is pressed but in selector tag I just can change attributes of imageButton not other textviews and... How can I do that?
You mentioned you wish to change the color of textview but never mentioned color of text in textview. this can be done very easily using the same selector file. Implement this way for proper implementation, so you can just get things done inside onclicklistener no need to handle button states. Modify your selector file as
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#color/default_blue" android:color="#color/default_blue"/>
<item android:state_pressed="false" android:drawable="#color/default_color" android:color="#color/default_color"/>
</selector>
in your layout xml for textview set
android:textColor = "#drawable/buttons_drawable_resource"
Note: Do not skip to add android:state_pressed="false" in selector as i gave above with default selections for button as well as textview text color else it will crash with NPE.
I have some TextViews which are dinamically added to a LinearLayout. These TextViews are clickable and have an onLongClickListener (I also intend to add onClickListener later)
Here is the thing, I want these TextView to change their background color when pressed and I read that you can use selectors to do such thing.
So I made this xml file in res/drawable/text_view_pressed.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="#000000"/>
<item android:state_pressed="false"
android:color="#FFFFFF"/>
</selector>
I tried to create a TextView and using this xml file likes this:
TextView t = new TextView(this);
t.setBackgroundColor(R.drawable.text_view_pressed);
But when I do this, it will give this error in t.setBackgroundColor: "Should pass resolved color instead of resource id here: getResources().getColor(R.color.text_view_pressed)" but it doesn't work as intended if I use getResources().getColor(R.color.text_view_pressed).
Anyone got an idea how to do it?
You are on the right track. However there is an important detail.
There are two types of resources that can be affected by states: ColorStateList and StateListDrawable.
A color state list can only be used in certain contexts, for example in TextView.setTextColor(). As far as I can see, you cannot use a color state list as parameter of setBackgroundColor() if you want to change the background of a View when it's pressed. You need a state list drawable for that. And in a state list drawable, the android:drawable attribute is mandatory.
So, to sum up:
The xml file should be placed in res\drawable,
Its structure should be slightly different (i.e. state list, not color list), and
You need to use setBackgroundResource() instead of setBackgroundColor().
Example file:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#android:color/white" />
<item android:drawable="#android:color/black"/>
</selector>
If you want to use custom colors instead of white and black, you simply need to define them as resources in res\values and reference them from here.
I have one button. I have added background image to my button using background attribute. Now when I click on button, I am not getting the default orange color. How can I get that color.
One more query, In the above scenario how can I change the default orange color to some other color.
Try this http://developer.android.com/guide/topics/ui/controls/button.html#Style, change state of your button
You have to make your button custom to do that changes for that create an xml file named custom_btn in yoyr drawable folder and paste the code in it as follows:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true"
android:drawable="#drawable/tick_btn" /> <!-- pressed -->
<item android:drawable="#drawable/untick_btn" /> <!-- default -->
</selector>
and in the button you have to add android:button="#drawable/custom_btn"
// try this
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="hover color or drawable" android:state_pressed="true"></item>
<item android:drawable="hover color or drawable" android:state_focused="true"></item>
<item android:drawable="color or drawable" android:state_enabled="true" android:state_focused="false" android:state_pressed="false"></item>
<item android:drawable="hover color or drawable" android:state_enabled="false"></item>
</selector>
if you're just trying to see the "default orange color" then use an ImageButton and apply your drawable to the src (rather than the background). You will then see the native image background behind your image and it will continue to do whatever it was doing before.
I am not getting the default orange color. How can I get that color.
When you add a background to a button it no more remains a default raw button , It becomes a custom button as you have rendered the default behavior by adding some background to it. Now you need to add color to your custom button on your own because the OS deals with only raw buttons not custom.
How can I change the default orange color to some other color.
To change the button state after its pressed can be done in two ways
1) Either add a background image.
2) or Add a xml to the button.
This is a very nice tutorial on Customizing Android buttons.
By default when button is clicked something like orange color will surround the button for short time, that indicates buttons is clicked. But this feature is not working when button contains background image. This is happening in list view too.why ? Any Ideas? TIA
I used setBackgroundColor(Color.BLUE); But here the color is applied and not gone...
You need to use a selector as your background resource :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#drawable/button_pressed" />
<item android:state_focused="true" android:drawable="#drawable/button_focus" />
<item android:drawable="#drawable/button_normal" />
</selector>
This selector makes use of 3 separate drawables to change the image of the button , you may use the same image for pressed and focuses.
You need to put this XML into your drawables directory and use it as a background for your button.
For another solution refer : Standard Android Button with a different color
i too had the same problem. so instead of setting the background color,i included three images of button in three different colors , so based on state focused,pressed,default the respective image will replace the other. and it looks like change in the color of the button.
**<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/c2"
android:state_pressed="true" />
<item android:drawable="#drawable/c1"
android:state_focused="true" />
<item android:drawable="#drawable/c1" />
</selector>**
above is the content of the xml file,which must be assigned to the background of the respective button
android:background="#drawable/drawable_button
hope this might be helpful for you
I want to remove the default orange focus color from GridView and from the EditText.
I have tried the android:focusable="false" but it is not working.
I think if this works as the other views, you have to use a selector in which you define various states for your view. A selector is a drawable (stored in the drawable folder) and you use it as if it was just an image. For instance, you could do something like that, if you want the focus to be red instead of orange :
selectorgridview.xml :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:color="#color/darkred" />
<item android:color="#color/white" />
</selector>
Then, you put the background of your GridView with it : android:background="selectorgridview"
I actually never tried it on a GridView but I think it works as the other views. More infos in the docs from google