selector with hinttext color in android - android

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)

Related

"Block" the pressed state of a button if it's not clickable in Kotlin, Android

My button has 3 states and the representation of each depends on a particular condition. (You can follow this question I've asked earlier to see the screenshots of what states my button has):
Different background for a button's states in Kotlin
I want my button to kind of "block" its pressed state if it is not clickable.
My button's states:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:drawable="#drawable/background_stroke_bluish_rounded_corners_2dp"/>
<item android:state_pressed="false"
android:drawable="#drawable/background_bluish_rounded_corners" /> <!-- pressed -->
<item android:state_pressed="true"
android:drawable="#drawable/background_dark_teal_rounded_corners" /> <!-- focused -->
<item android:state_enabled="true" android:drawable="#drawable/background_bluish_rounded_corners"/>
</selector>
How I tried to set the isClickable to false:
updateFragmentView?.mFragmentRootView?.btnUpdate?.isClickable = false
Right now it works and the needed event while isClickable = true doesn't happen, but what I want to implement is not to let the pressed state become visible if the button is not clickable. Is that possible to implement without changing the XML?
Something alike can be found in isUserInteractionEnabled for iOS. If set to false, the button's highlighted color doesn't work (if the button has one).
UPD: I've tried setEnabled(false) and also .isEnabled = true, but that does influence on the logic of my fragment, so that's not working for me.

Disable a button without using setEnabled property

I have a button where the selector is like
<?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/bg_circle_selected"/>
<item android:drawable="#drawable/bg_circle_disabled" />
</selector>
So when I click the button, the background will show a red colored circle.
I need to disable this button based on condition, so the highlight should not be shown.
If I so it as setEnabled false it will work
But there is one more case where the disabled button should give auditory feedback.
So when I give setEnabled as false the other requirements will not work because touch is disabled.
Is there any method to disable the button other than setEnabled ()?
You can achieve the above by custom selector where the button appears to be disabled but the fact it is still enabled and trigger click actions.
you can use
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:drawable="#drawable/bg_circle_disabled />
<item android:state_pressed="true" android:drawable="#drawable/bg_circle_disabled"/>
<item android:state_enabled="false" android:drawable="#drawable/bg_circle_disabled" />
also don't forget to keep track to the button status (enable/disabled)
You might want to consider setting the attributes of the button programmatically from the java file, not XML.
Give the button an id from your XML layout file, then reference it from the java file. In that way, you will have more control over how it behaves.
For example, android:id="#+id/my_button" in the XML.
Then Button button = findViewById(R.id.my_button); in the onCreate method.
Afterward, give it whatever attributes you want.

Change child TextView colour on parent LinearLayout touch

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().

Android textColor selector

I am trying to set up a selector for TextView textColor using the following code:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:color="?android:attr/textColorTertiary" />
<item android:color="?android:attr/textColorPrimary"/>
</selector>
However, the color always appears to be red instead of those theme colors. If I put hardcoded color, everything seems to work fine. (ex. <item android:state_enabled="false" android:color="#666666" /> ).
What is the problem and how to solve it? P.S. if anyone knows how to set theme's default disabled color for disabled item in the list, please share, that is what I am trying to achieve. Thanks.
As far as i can see you may have to use 3 states in a selector.
state enabled
state focused
state pressed
in exactly this order. This might help
You used selector for what reason?
If you want to make your text of text view always red then no need of selector. Just define color in color.XML or in string.XML using add color.
And if you want to chanhe it on selection or focus than use the states.
state enabled
state pressed
state focused
Than it will work as you need.

Textview not not going back to orginal color after being unpressed

I have a text view that I am setting the text color as follows:
<TextViw
android:id = "#+id/tv"
android:clickable = "true"
android:textColor = "#color/clickable_text"
android:textSize="16sp"
/>
And the selecor is
<item android:state_pressed "true" android:color = "#FF0000" /> //which is red
<item android:color = "#00FF66" /> //which is Green
The textview starts Green which is as expected. If I click (say hold on it) the color changes to Red which is good. But when release the click (i.e. untouch it). then the color changes to yellow!. Which is the default color android whould change a clickable view to upon clicking. WHY!!!!.
Shouldn't it come back to green to as I expected? The problem is that this color stasys on evenif I move between activities and come back
Any help?
Thank you
You usually want to have a pressed, focused, and default state. And always put the default state last by the way. My guess is if you long press and then release, the button is not presssed (red), but is still focused. So change your focused to green as well as below
<?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" /> <!-- pressed -->
<item android:state_focused="true"
android:color = "#00FF66" /> <!-- focused -->
<item android:color = "#00FF66" /> <!-- default -->
</selector>
I know this isn't the exact answer for your question, but the idea from this link might help you: Android how to make TextView text bold when pressed or focussed. I hope this helps you.

Categories

Resources