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.
Related
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.
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.
I currently have icons in a GridView on my app. When they are clicked, an orange square appears around them briefly as a highlight. The only problem is that I think this looks amateurish and would like to change the shape so that it clips the icon in the GridView instead of a large square. If you are unsure as to what I mean, it is carried out successfully in the Catch Notes app, on their dashboard/home screen. I was just wondering whether anybody knew a way to tackle this or if it is simply a small layout attribute.
https://play.google.com/store/apps/details?id=com.threebanana.notes&hl=en
Thanks in advance, all help would be appreciated!
It looks effectively like this EditText in the image below. The way that it borders the EditText in orange is exactly how it borders the icons in the home screen when clicked.
First, in your layout file, specify a drawable background:
android:background="#drawable/bg_your_view"
Then edit the bg_your_view file which is in drawable:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/bg_your_view_pressed" android:state_pressed="true"></item>
<item android:drawable="#drawable/bg_your_view_normal" android:state_focused="false"></item>
</selector>
At last, create corresponding drawable files for pressed and normal states:
bg_your_view_pressed.xml contains the shape, the color you want for highlight.
bg_your_view_normal.xml is similar, just without highlight effect.
I've decided that the easiest way to solve this problem is to simply reference another image when the image on the GridView has been clicked. This means that the second image can just be edited in Photoshop in order to have a glow around it.
Hi im applying custom images to button on different states, for which i create an xml file in appliication drawable folder in which i define states of button and images like this
Btn_enter_cutom.xml
<item android:drawable="#drawable/btn_enter" />
<item
android:state_pressed="true"
android:drawable="#drawable/btn_enter_pressed" />
and assign this xml file to background attribute of the button.
button's default image is btn_enter.png but when this button presses i want to apply btn_enter_presses.png,but when i pressed the button nothing will happens,is there any problem with the xml file please help me in this regards.thanks
A state list will always display the first matching line. Reverse your two items, since the first one acts as a default and prevents any further testing.
I have a linear layout in which each row is inflated programatically and I want the rows to behave like the ListView when clicked on. That is, I want the row to highlight in the exact same way/colour that the default ListView does. How would I go about doing this?
Ok I have finally figured out how to do this...basically it is done using a selector like the color selector linked by style except instead of 'color' use a drawable for the states and you can refer to the default list drawable that is used in ListView by this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#android:drawable/list_selector_background" />
</selector>
and using this xml as the background for my View.
All the public default drawables can be found here: http://developer.android.com/reference/android/R.drawable.html
I was able to do the same with a text view that I wanted to behave like a list item by using:
<Textview
....
android:background="#android:drawable/list_selector_background"
/>
This might be a good place to start looking.
Although, i would advise you to use the ListView itself, rather than implementing it again.
if you still have a problem with that then please remember that some of UI elements are not clickable (RelativeLayout), so you have to add one more line:
<RelativeLayout
....
android:clickable="true"
...
To your listview set property
android:listSelector="#color/test"
and this test color set any transparent color you like. you can create any transparent color by using hex transparent color