How to enable the "pressed" look on LinearLayout - android

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?

Related

How do I imitate the behaviour of a pressed button in Android?

I'm trying to make a custom button, that behaves just like a 'pressed button' in Android with my own background images.
Pressing changes my button's image (color is darker), but I can't seem to get the focussed / focussed&pressed states right.
When you release your finger on the default button there is a circle filling the whole button but it won't happen with mine.
That's my code:
<?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="false" android:drawable="#drawable/fertig_focussed" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#drawable/fertig_focussed_pressed" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#drawable/fertig_pressed"/>
<item android:drawable="#drawable/fertig_regular" />
</selector>
Thanks in advance!
That's because android check stats in the order and get the one matching first.
You need put "pressed" first, focus pressed second, then focused.

Android Button doesn't update UI sometimes in ExpandableListView

I'm using Buttons in children of ExpandableListView.
I've added custom drawable to backgrounds of the buttons so that it can show another images per status.
It shows/update backgrounds of the buttons normally but sometimes it doesn't when buttons are pressed in a specific child.
Here is the drawable I use for button background.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#drawable/button_plus_highlight" />
<item android:state_focused="true" android:drawable="#drawable/button_plus_highlight" />
<item android:drawable="#drawable/button_plus_normal"/>
</selector>
Add this parameter on ListView in XML
android:descendantFocusability="blocksDescendants"
I hope this helps. :)

Setting button drawable for RadioButton using drawable xml not working for checked state

I've got a group of radio buttons, and I want to set the button's background to a solid color when checked. I created a drawable resource, using a selector and item def's like:
<item android:state_checked="true" android:state_pressed="false"
android:drawable="#color/app_tint"/>
with several variations while trying to get it to work. In the layout containing the buttons, I've tried setting both button and background properties (not at the same time, just one or the other in testing) like:
android1:background="#drawable/radio_state"
OR
android1:button="#drawable/radio_state"
I've read several posts, and I feel I'm close, just missing something to get it done. Thanks.
Here's one we did for an app:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ic_bcnav_ebilling_focus"
android:state_checked="true" />
<item android:drawable="#drawable/ic_bcnav_ebilling_focus"
android:state_selected="true" />
<item android:drawable="#drawable/ic_bcnav_ebilling_focus"
android:state_pressed="true" />
<item android:drawable="#drawable/ic_bcnav_ebilling_focus"
android:state_focused="true" />
<item android:drawable="#drawable/ic_bcnav_ebilling" />
</selector>
Each state has a different drawable, although in this example, we don't really care about all states being very different - just focus=true get a highlighted drawable (it has "..._focus")

How to create a state-specific drawable xml for a toggle RadioButton

When creating button drawables, I typically follow the following format to implement an "onClick" change of background:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="true"
android:drawable="#drawable/RESOURCE_FOR_CLICKED" />
<item android:state_focused="false" android:state_pressed="true"
android:drawable="#drawable/RESOURCE_FOR_CLICKED" />
<item android:drawable="#drawable/RESOURCE_NORMAL" />
</selector>
This works fine for me when I am creating traditional buttons as I want them to return to their original state once onClick is finished, and I don't have to implement any code.
However, this does not work for RadioButtons because I actually want their background drawable to be different in the non-pressed state once they have selected.
Are there XML attributes for states involving radio buttons that I should be aware of to implement this sort of thing? If not, do I have to manage the changing of backgrounds in code?
android:state_checked will let you specify a drawable for when a radio item is selected.

how to change button color when it is clicked in android

By default when button is clicked something like orange color will surround the button for short time, that indicates buttons is clicked. But this feature is not working when button contains background image. This is happening in list view too.why ? Any Ideas? TIA
I used setBackgroundColor(Color.BLUE); But here the color is applied and not gone...
You need to use a selector as your background resource :
<?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/button_pressed" />
<item android:state_focused="true" android:drawable="#drawable/button_focus" />
<item android:drawable="#drawable/button_normal" />
</selector>
This selector makes use of 3 separate drawables to change the image of the button , you may use the same image for pressed and focuses.
You need to put this XML into your drawables directory and use it as a background for your button.
For another solution refer : Standard Android Button with a different color
i too had the same problem. so instead of setting the background color,i included three images of button in three different colors , so based on state focused,pressed,default the respective image will replace the other. and it looks like change in the color of the button.
**<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/c2"
android:state_pressed="true" />
<item android:drawable="#drawable/c1"
android:state_focused="true" />
<item android:drawable="#drawable/c1" />
</selector>**
above is the content of the xml file,which must be assigned to the background of the respective button
android:background="#drawable/drawable_button
hope this might be helpful for you

Categories

Resources