I have a LinearLayout containing 2 TextView, one larger and black, one smaller and light grey. The LinearLayout's background is white by default then on touch quite a strong blue, and when it's touched (IE Blue), the grey text is lost in the colour, so I want to change it to white.
I've seen (using buttons) I can give textColor a resource file like this to change the text's colour
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:color="#ffffff" />
<item android:state_focused="true" android:state_pressed="true" android:color="#000000" />
<item android:state_focused="false" android:state_pressed="true" android:color="#000000" />
<item android:color="#ffffff" />
</selector>
Could I do something similar for my Layout/TextView? I thought about using an OnTouchListener, but the colour didn't change at the same time as the background did, and I've not found an onTouchEnd event yet to reset the colour back to grey.
You can applicate android:duplicateParentState="true" to the two TextView into your LinearLayout.
Then, the click event will be propagate to the two child views.
How about adding a onTouchListener to the parent ==> Returning false from this call back ==> Also fetch all children of the parent in same call back ==> Cast them to TextViews and set text color ==> Call Parent.invalidate().
Related
Basically, I want to change the color of hint when the EditText property focusable and foucsableInTouchMode is set to false.
It's all good with textColor but got useless with textColorHint property of Editext.
I tried my best effort but all in vein, so my code is as follows
I have create a drawable called "colorListfile" stated as follows,
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_window_focused="false"
android:state_enabled="true"
android:color="#color/colorPrimaryDark" />
<item
android:state_window_focused="false"
android:state_enabled="false"
android:color="#color/white" />
<item
android:state_focused="true"
android:color="#color/login_signup_header_textcolor" />
<item
android:color="#color/colorPrimary" />
</selector>
My EditText is shown below,
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="zuluzulu01"
android:textColorHint="#drawable/colorListfile"
android:hint="Name" />
But this or any other approach didn't work. But this all code work fine when this property is set to textColor.
Any help would be greatly appreciated.
You said you want
to change the color of hint when the EditText property focusable and focusableInTouchMode is set to false
Your ColorStateList works perfectly. It's only that it depends on state_focused (i.e. the View has focus) and state_window_focused (i.e. the window of the View has focus).
To make sure, I've just tested with two EditTexts (so the EditText can lose focus while the user is busy in the same window), a Button (to en-/disable the EditText) and an emulator running Nougat (so I can switch the focus to another window)
Unfortunately, there are no selector item attributes state_focusable and state_focusableInTouchMode.(See the documentation for Color List Resource)
So if you really want the hint color to depend on focusable and focusableInTouchMode being set to false, you will have to set the desired color programmatically every time you toggle these attributes.
myEditText.setHintTextColor(Color.GREEN)
I have an image button (star) which is used to mark something as favorite. I envision that when the user clicks on the star, the star will turn yellow. When they click on an already yellow start, it will go back to normal.
Transition from one color to the other would make a call to the server. I am doing that part already.
To change the color on click I did this.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="true"
android:drawable="#drawable/ic_action_fav" />
<item android:state_focused="false" android:state_pressed="true"
android:drawable="#drawable/ic_action_fav" />
<item android:drawable="#drawable/ic_action_ic_action_star" />
</selector>
However, this changes the color only for the time being when the buttons is clicked. It doesn't remain changed on the click, in other words, it doesn't toggle.
How can I toggle the color of a button on each click?
Use android:state_selected in the state list along with View.setSelected(boolean selected) in your Java code.
I have the following UI presented when my ListView is empty:
Now, I want that when the user will press this "New Reminder" layout, it'll change to a "highlighted" state (with the blue focus background in ICS and the yellow color in GB)
The layout is clickable and the onClick method is called, but there is no indication for the user while he press his finger down.
I tried setting focusable to true, but it didn't do the trick.
What can I do to give any view the default "pressed" effect?
Thank you!
With your layout, you can set background with drawable like below.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="#android:drawable/star_big_on" />
<item android:state_pressed="true" android:drawable="#android:drawable/star_big_on" />
<item android:drawable="#android:drawable/star_big_off" />
</selector>
Draw-able like below
<?xml version="1.0" encoding="UTF-8"?>
<item android:state_enabled="true" android:state_pressed="true" android:drawable="#drawable/left_radio_selected"/>
<item android:state_enabled="false" android:drawable="#drawable/left_radio_inactive"/>
<item android:state_enabled="true" android:state_selected="true" android:drawable="#drawable/left_radio_selected"/>
<item android:drawable="#drawable/left_radio_active"/>
and set your linerlayout.setSelected(true);
I don't know if I understand you correctly. If you want to create pressed state for this layout, you have to prepare state drawable xml (more info). Up there you can set what should your layout looks like while in pressed state.
I'm not sure but your question seems a little vague. From my understanding what you want might be suggested in this post Force a ListView item to stay "pressed" after being clicked?
In an android app, I have a GridView holding TextView created by an adapter. In those TextView I add an icon which has three states (pressed, selected and default.)
I removed the default selector of the GridView with android:listSelector="#00000000" and I would like the selected state of the icon to display instead. But although the pressed state works (ie when the TextView is pressed, the pressed version of the icons is shown) the seleted doesn't.
I've tried those tricks (found at different places on the web) but it didn't work either:
setting
android:descendantFocusability="afterDescendants"
or
android:drawSelectorOnTop="true"
or (in the TextViews)
android:duplicateParentState="true" />
And if I set the TextView to be focusable, it gets focus independently of the GridView (ie, clicking on it doesn't call the GridView onClick method...)
The icon is defined in a xml file like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true"
android:state_pressed="true"
android:drawable="#drawable/ic_flag_bg_pressed"/>
<item android:state_focused="false"
android:state_pressed="true"
android:drawable="#drawable/ic_flag_bg_pressed"/>
<item android:state_focused="true"
android:drawable="#drawable/ic_flag_bg_selected"/>
<item android:state_focused="false"
android:state_pressed="false"
android:drawable="#drawable/ic_flag_bg_default"/>
</selector>
Is there a way to tell the GridView to pass the focused state to its children ?
Got stuck on this as well. An alternate approach that can look okay is to use a border style selector and draw it on top.
I made a button that changes the background drawable on different states, this way:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#drawable/btn_location_pressed" /> <!-- pressed -->
<item android:state_focused="true" android:drawable="#drawable/btn_location_pressed"/> <!-- focused -->
<item android:drawable="#drawable/btn_location"/> <!-- default -->
</selector>
The problem here is that I'm also trying to change the textColor as I do with the drawable but I'm not being able to. I already tried android:textColor and android:color but the first doesn't work whilst the seconds changes my background.
The next code is part of my layout. Regarding to the text color it only works for the normal state text color, thus not changing it to the white one while pressed
<Button android:id="#+id/location_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:background="#drawable/location"
android:textSize="15sp"
android:textColor="#color/location_color"
android:textColorHighlight="#FFFFFF"
/>
Has anybody got a clue?
Create a stateful color for your button, just like you did for background, for example:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Focused and not pressed -->
<item android:state_focused="true"
android:state_pressed="false"
android:color="#ffffff" />
<!-- Focused and pressed -->
<item android:state_focused="true"
android:state_pressed="true"
android:color="#000000" />
<!-- Unfocused and pressed -->
<item android:state_focused="false"
android:state_pressed="true"
android:color="#000000" />
<!-- Default color -->
<item android:color="#ffffff" />
</selector>
Place the xml in a file at res/drawable folder i.e. res/drawable/button_text_color.xml. Then just set the drawable as text color:
android:textColor="#drawable/button_text_color"
Another way to do it is in your class:
import android.graphics.Color; // add to top of class
Button btn = (Button)findViewById(R.id.btn);
// set button text colour to be blue
btn.setTextColor(Color.parseColor("blue"));
// set button text colour to be red
btn.setTextColor(Color.parseColor("#FF0000"));
// set button text color to be a color from your resources (could be strings.xml)
btn.setTextColor(getResources().getColor(R.color.yourColor));
// set button background colour to be green
btn.setBackgroundColor(Color.GREEN);
ok very simple first go to
1. res-valuse and open colors.xml
2.copy 1 of the defined text their for example
#FF4081 and change name for instance i changed to white and change its value for instance i changed to #FFFFFF for white value like this
<color name="White">#FFFFFF</color>
then inside your button add this line
b3.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.White));
ok b3 is the name of my button so changed of the name of ur button all others will be same if u use white color if you change different color then change white to the name of your color but first you have define that color in colors.xml like i explained in pont 2
Changing text color of button
Because this method is now deprecated
button.setTextColor(getResources().getColor(R.color.your_color));
I use the following:
button.setTextColor(ContextCompat.getColor(mContext, R.color.your_color));
Use getColorStateList like this
setTextColor(resources.getColorStateList(R.color.button_states_color))
instead of getColor
setTextColor(resources.getColor(R.color.button_states_color))