How do I imitate the behaviour of a pressed button in Android? - 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.

Related

Android selector. How to go to normal state after press?

I've got a list of cards, and when the user presses on them, it goes to different fragments.
I'm using a selector on a card to have a "pressed state". I use the following XML:
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:state_enabled="true"
android:state_pressed="true"
android:drawable="#drawable/hatching" />
<item
android:state_enabled="true"
android:state_focused="true"
android:drawable="#color/transparent" />
<item
android:drawable="#color/transparent" />
</selector>
When I press on a card, if the next fragment takes 1 second to load, the card stays in the "pressed" state, until the fragment is displayed. Does someone know how to do like in recent Google apps (Messenger, Gmail, ...), ie after the press, the card goes to "normal" state, and then after the fragment is ready, the fragment is displayed.
Thanks.
UPDATE: The problem doesn't seem to be in the selector. If instead of going to another fragment I go to display another activity, the card goes to normal state. It's only when going to a fragment. It's like if the fragment takes all the resources and prevent the normal state to be displayed.
Yet, if we go to the onTouchEvent() method of the View class, there's this code:
// Use a Runnable and post this rather than calling
// performClick directly. This lets other visual state
// of the view update before click actions start.
if (mPerformClick == null) {
mPerformClick = new PerformClick();
}
if (!post(mPerformClick)) {
performClick();
}
So I don't know why the normal state is not displayed...
Try with the following Selector for CardView
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/card_on_press"/>
<item android:state_focused="true" android:state_enabled="true"
android:drawable="#drawable/card_on_focus"/>
<item android:state_enabled="true"
android:drawable="#drawable/card_default"/>
<item android:state_enabled="false"
android:drawable="#drawable/card_on_press"/>
</selector>
Note: Drawable default will be fine as a transparent item as CardView provides a default background for all android versions
Try this...
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:state_pressed="true"
android:drawable="#drawable/hatching" />
<item
android:drawable="#color/transparent" />

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")

Button background change layout on pressed

i'm currently working with the camera api and want to implement a button, which starts and stops video recordings. the button has four different images: video_start_default, video_start_pressed, video_stop_default and video_stop_pressed.
it should change its layout when pressed and after starting/stopping the video.
i created this xml file to do so.
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:state_selected="false"
android:state_pressed="false"
android:drawable="#drawable/video_start_default" />
<item
android:state_selected="false"
android:state_pressed="true"
android:drawable="#drawable/video_start_pressed" />
<item
android:state_selected="true"
android:state_pressed="false"
android:drawable="#drawable/video_stop_default" />
<item
android:state_selected="true"
android:state_pressed="true"
android:drawable="#drawable/video_stop_pressed" />
<item android:drawable="#drawable/video_default" />
</selector>
in order to make the change between start and stop work, i set the select attribute in the onclicklistener
if(videoButton.isSelected())
videoButton.setSelected(false);
else{
videoButton.setSelected(true);
}
basically this works.
while pressing video_start_default, video_start_pressed is correctly shown but the problem is that after releasing video_start_pressed, video_stop_pressed is shown for a millisecond instead of video_stop_default. strangely the problem doesn't occure after clicking video_stop_pressed. here is video_start_default directly visible.
i hope the description is not to confusing
I think you have to make two selectors one for the play state and another for the pause state:
I have a nice tutorial for you that can help you a lot in your project
you have to see this link.
Cheers

How to enable the "pressed" look on LinearLayout

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?

Android ListView with Custom List Selector causing Flickring problem

I'm setting a selector.xml to the ListView Selector :
<item android:state_focused="false" android:state_selected="true"
android:state_pressed="true" android:drawable="#drawable/timeline_selected_rect"/>
<item android:state_focused="false" android:state_selected="true"
android:state_pressed="false" android:drawable="#drawable/timeline_selected_rect"/>
<item android:state_focused="true" android:state_selected="true"
android:state_pressed="true" android:drawable="#drawable/timeline_selected_rect"/>
but setting this is causing the TextView flickr over selection. this thing is not happening when the Default selector is used... whats wrong with this selector.
I have even added android:cacheColorHint="#00000000" as provided info by some other blogs.
I recently ran into this problem as well and found the solution by analyzing the Android source code.
You need to remove the android:state_selected="true" attributes since checking that state is unneeded. Once a list item loses it's focus, it also makes 'selected = false'. Since focus=false & selected=true will never occur, your first 2 <items> in the selector will never be shown.
Think of the tap like a mouse click with an onPressDown and onPressUp event.
So your new selector.xml should looks something like this to avoid the flickering:
<!-- {comment copied directly from Android source code}
Even though these two point to the same resource, have two states
so the drawable will invalidate itself when coming out of pressed state.
-->
<item android:state_pressed="true" android:state_focused="true"
android:drawable="#drawable/timeline_selected_rect"/>
<item android:state_pressed="true" android:state_focused="false"
android:drawable="#drawable/timeline_selected_rect" />
<item android:state_focused="true"
android:drawable="#drawable/timeline_selected_rect" />
<item android:state_window_focused="false"
android:drawable="#android:color/transparent" />
As an aside, you should seriously consider creating a separate drawable for the pressed state so that you give a bit of visual feedback. It is always nicer to see "something happen" when interacting with the UI; whether that is drawing a button in a "down" state while it is being pressed or changing the color slightly while tapping on a list item, the visual feedback is good practice.

Categories

Resources