EditText bottom line colour changing on focus - android

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.

Related

Image "inside" button while keeping standard animations

I would like to define a button in Android, through XML, which, below the standard Button graphics (change color when clicked, slightly rounded edges,...), shows an image of my choice. I would like the final product to be somewhat like this:
I have tried change the src and background of an ImageButton, but it does not provide the intended effect. Could you please point me some way of achieving this?
Maybe that's not exactly what you mean by standard, but when you set a background, you end up having to recreate the behavior when the button is clicked. The best way to do it in Android is by using a selector as your button's background. Create a XML drawable with the selector in it.
Example:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#drawable/button_bg_pressed" />
<item android:drawable="#drawable/button_bg_default" />
</selector>
That way, when your button is clicked, it will change the background to other image that does what you want.
EDIT:
There are other modifiers such as focused, if you need.
If you want Lollipop's ripple effects, I think you can create a Layout and put your button inside it. Change the Layout background and set the button background to transparent. If that doesn't work, try adding android:background="?android:attr/selectableItemBackground" to your button.
As Paulo said, you can achieve the click effect with a selector.
See this answer (https://stackoverflow.com/a/30192562) it gives the code for a very nice and customizable ripple effect.

EditText different state after user inserting content

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.

ImageButton background swiching on it's own? Drawables gone wild?

I having an issue where the ImageButton background is being drawn with a different drawable from one of my resources. I'm setting the background to be transparent but on some cases it's picking up one of my drawables called bottom_shadow.9.png. Why why!? It's freaking weird...
I've seen this issue before... Some of my app users have complained seeing this issue and now I'm determined to figure this out! Take a look below what I currently have. Any tips or ideas would help.
The color value I created under values/colors.xml:
<color name="transparent">#00000000</color>
My ImageButton under my one xml layout under layout/:
<ImageButton
android:id="#+id/ibHelp"
android:layout_width="wrap_content"
android:layout_height="#dimen/settings_list_item_height"
android:background="#color/transparent"
android:contentDescription="#string/content_desc_more_information"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:scaleType="centerInside"
android:src="#drawable/btn_help" />
This is what I'm seeing on the generated R.java file:
public static final class drawable {
public static final int bottom_shadow=0x7f020000;
}
public static final class color {
public static final int transparent=0x7f080009;
}
This is what it should look like:
This is what I'm seeing:
Could it be related to this issue?
http://code.google.com/p/android/issues/detail?id=20283
I think you are facing the same issue as I on another project : When using the transparent color #00000000 on a background, Android will not actually make it transparent but instead use the background drawable of the element directly under it.
Not sure what I've just said is clear but to check if this is it, I found a quick and easy solutions : Don't use #00000000 as your background transparent but any other completely transparent color : #00FF0000 or even #00F00000 should do it.
See the issue I raised in Google tracker : http://code.google.com/p/android/issues/detail?id=24653
Why are you creating your own color when it's built into Android.R.color? I would try using:
android:background="#android:color/transparent"
Whether or not it fixes your problem, it's simpler and cleaner.
I think you wanna your button's background to be some kind of color, but you have assigned both a src and a color of the button(in the layout xml), which means that the button may use the src picture as the background, not a pure color. I don't know if I made the point.
Just to add to this, I was seeing really strange periodic display corruption in my transparent ImageButton background because I was specifying the items in my background selector as follows:
<item android:drawable="#android:color/transparent" android:state_focused="false" android:state_pressed="false" android:state_selected="true"/>
This might seem to work occasionally, but I definitely had cases where the ImageButtons would render with a ghastly all-white background instead of the nice transparent background.
Note that I was mixing up the android:drawable syntax with a color resource. The correct way to specify a color resource seems to be either an android:color="#FF00FF" attribute or as a child element of item using a element. I searched long and hard, and eventually found this post.

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"

Categories

Resources