EditText different state after user inserting content - android

I have two EditText with two states. A focused state and a default state.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="#drawable/focused" />
<item android:drawable="#drawable/not_focused" />
</selector>
This is working as expected and the states differ only in the background (color, etc).
I've added setOnFocusChangeListener on the edittext to detect the focus.
How can I set the state of my edittext to be the same as the focused state only if I have any content in my edittext? (for example after user inserting some input)?
In the setOnFocusChangeListener I can detect the edittext is focused and if it has any content but I can't figure it out how to maintain the focused state, that is, the same background

You can use a TextWatcher to check when the text changes, then inside that, add an IF, then inside that IF check if the EditText is different from null, and, add your code.
What this will do, is that anytime the text inside the EditText changes, it will call the IF and see wether or not it has text inside and accordingly change it.

Related

How to change hint text color of edittext when TextInputLayout is used

I have a screen with edittexts. When these edittexts are disabled, I need the hint color to be in one color
When these are enabled, I need the hint to be in a different color like this
This works fine when I tried like this
editText.setHintTextColor(color);
The problem is , I have to use a TextInputlayout to show the floating hint. So, When I add the EditText inside TextInputLayout this code doesn't work at all. I tried many things with TextInputLayout as shown here. But nothing works. All I need is the hint text color (Not floating hint text) of the EditText should be in one color when enabled , and in different color when disabled, when used inside a TextInputLayout. Please help!
Note: My question is not a duplicate of this question. There , it is mentioned about the floating hint text of the TextInputLayout when it is focused What I am talking about the normal hint text when the edit text is not focused and also when it is empty.
Add these to your App theme
<item name="colorControlNormal">#color/color</item>
<item name="colorControlActivated">#color/color</item>
<item name="colorControlHighlight">#color/color</item>
<item name="android:textColorHighlight">#color/color</item>

EditText bottom line colour changing on focus

Here is the EditText's abc_edit_text_material drawable XML:
<selector>
<item android:state_enabled="false" android:drawable="#drawable/abc_textfield_default_mtrl_alpha"/>
<item android:state_pressed="false" android:state_focused="false" android:drawable="#drawable/abc_textfield_default_mtrl_alpha"/>
<item android:drawable="#drawable/abc_textfield_activated_mtrl_alpha"/>
</selector>
It defines a set of images but not colours (and I can not track the moment when the colour is applied).
The following piece of code makes the bottom line always stay red, though its thickness changes with focus (so, the same <selector> is still in use):
Drawable back = edittext.getBackground();
back.setColorFilter(0xffff0000, PorterDuff.Mode.SRC_ATOP);
editText.setBackground(back);
But if after that I restore the default drawable, colours start changing according to the current focus state (gray unfocused, accent focused):
back.setBackgroundResource(R.drawable.abc_edit_text_material);
The questions:
Why is the same selector used after applying a modified Drawable?
Why do colours start to respond to focus changes after re-setting the same XML drawable as the background? Is there some object storing a set of colours corresponding to selector's state_enabled, etc?
(Maybe, the answer to this one will also make clear the previous two)
At which moment and how is colour applied by default? I mean, does it check whether the background is default and which class calls for this colouring? I tried looking through the sources from AppCompatEditText to View and did not find anything like that
Why not? By getBackground() you retrieve a StateListDrawable, which seem to accept color filters.
Responding to focus is the default behavior to make clear which item is currently focusing and where is your keyboard input going. You are not resetting the filtered background; you are pointing to the original one.
Class stores its default background and applies filters to the unfocused (colorControlNormal) and focused (colorControlActivated) state. If you change it, well, it changes and filters are gone.

How to make a TextView flash its background when clicked?

I have like 10+ textview embedded in a scrollview to give a ListView effect(I don't want to use a ListView here for some particular reasons).
Some details about the textview is that it has a background which is an image.
So my question is when I click on a particular textview among the 10+ views taht I have, I want to animate the background like the ones in a native ListView. If this is possible can someone provide some pointers please?
If not animating the background can we atleast animate the borders of the clicked textview?
Thanks,
Sana.
Set your TextView's background to state list selector drawable such as:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#drawable/your_pressed_background" />
<item android:drawable="#drawable/your_normal_background" />
</selector>
You save the above XML in the res/drawable directory and reference it in your TextView like an other resource.
Based on the state of your TextView, android will select the background drawable. When your TextView is pressed, the pressed background will be drawn. When it is not, your normal background will be drawn.

Android: Visual version of Button.performClick

I know that I can trigger the OnClickListener.onClick of a Button manually in code by calling performClick, but that doesn't seem to make it visually appear as it's been clicked. I'm looking for a way to manually make a button appear as if it's been clicked. Do I need to manually change the background drawable and invalidate (and then change it back again on a Handler.postDelayed call), or is there a more framework-y way of doing this?
EDIT
I know how to make the button have different drawables to appear pressed when the user initiates the press. The question is this:
Is there a simple way to make a button appear pressed programmatically when not physically pressed by the user?
SOLUTION
I just subclassed Button and made the button aware of it's normal background as a StateListDrawable and the Drawable that is used as the pressed state. I expose a method that manually sets the background to the "pressed" drawable, and I use Handler.postAtTime to have it return to it's normal background so it can be used as a regular button again when I'm done.
Although this question is very old, I figured I'll still answer it. You don't need to subclass the View. First call performClick(), visual cue won't last long, but then you can set the button's pressed state via view.setPressed(true); and then reset it a couple of milliseconds later like this.
handler.postDelayed(new Runnable() {
#Override
public void run() {
view.setPressed(false);
}
}, 100);
Ya, you have to create 2 drawables. One for pressed state and other for normal state.
Then you will have to create an xml 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/focused_drawable" />
<item
android:state_pressed="false"
android:drawable="#drawable/unfocused_drawable" />
</selector>
Place this xml inside your drawable folder. You can also add focused state as
android:state_focused="true"
Then inside your layout which is used by your activity, give inside your button tag:
android:background="#drawable/your xml file name"

different color for certain item in my list

I have a list with items, some items can be in 'viewed' status. i want those items to have different background color then the rest.
list view basically has a single selector for the whole list, setting a background color on one of the items prevent wont do the trick since the selector is drawn below my item's layout as background and counts on them being transparent.
is there a way to define more then one selector for a list ? if not is it possible to create a selector that has an extra state ? basically all i want is the regular list selector plus an extra state that it's ColorDrawable is defined in my colors.xml.(since i can't inherit from a drawable and the list_selector drawables of android are not visible in the SDK for me to use i wanted just to add a state, but then how do i enforce using the extra state ?)
Found the answer.
My problem is not the state of the selector, i want the selector in the same state for ALL items!
the problem is that i want the item to be regular if certain boolean in items in the adapter is false and color X if the boolean is true, but even if the color is X i still want it to be transparent once it's selected or pressed.
So the solution is the following xml:
<?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/transparent" /> <!-- pressed -->
<item android:state_selected="true"
android:drawable="#android:color/transparent" /> <!-- focused -->
<item android:drawable="#color/viewed_hotels_color" /> <!-- default -->
</selector>
i found out the even though the list itself is the one that gets the state , it DOES pass the state on to it's children (I.E the list items) which i thought it didn't do.
Only android:focused wont be effective here, android list items are not focused but selected... so as you can see in any state that is not pressed or selected i give my default color, otherwise it's transparent and the selector is visible to all.
in my adapter i need the following code(in getView() method):
View itemView = convertedView; <- i'll spare you the layout details...
Item item = getItem(position);
if( item.isViewed() ){
itemView.setBackgroundResource(R.drawable.viewed_item_background);
}else{
itemView.setBackgroundDrawable(null);
}
this makes sure every item will get the right background. of course if an item state is changed to viewed and i want it to show i need to call notifyDatasetCahnged() on the adapter.
You can implement a custom subclass of your Adapter and override the method getView(). As this method is called by the list to get the View for a single item you can define how the a single list item looks like by overriding getView(). So changing the background color of an already viewed item shouldn't be a problem.
UPDATE
The AbsListView class which the ListView class extends has a method called setDrawSelectorOnTop(). I think you can use it to control the behavior of the selector.

Categories

Resources