Background change on button when pressed, isn't working properly - android

This is my xml file for the button:
<?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/play_pressed" />
<item android:drawable="#drawable/play" />
</selector>
But this only works only one time, when I go to another activity and return then this doesn't work.
When I first run the activity the background of the button changes as it should, but when i go to another activity and return back to the previous one, then the background change while button pressed isn't working....
EDIT:
The problem was in my java code. It was replacing the background to a static png image every-time (except the first time)...The above code works as it is expected.

It should be like below:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="false"
android:state_pressed="false"
android:drawable="#drawable/play_default" />
<item android:state_pressed="true"
android:drawable="#drawable/play_pressed" />
<item android:state_selected="true"
android:state_pressed="false"
android:drawable="#drawable/play_pressed" />
</selector>

Related

Prolonging the focusabilty of a View

in my application I'm using instead of Buttons, ImageViews which have a selector as background. They are working fine though the focus is lost really quickly and as a result the effect isn't visible to the user 100%. Is there a way that I can delay the focus which the views receive for like a second? another way that I've tried was to change the ImageView's background when it was touched and change it back on the onPause()
Background Selector
<?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/actionbar_background" />
<item android:state_focused="false"
android:drawable="#drawable/normal" />
</selector>
normal.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF"/>
</shape>
Ok this is what I did to overcome it I changed the background selector as
<?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/actionbar_background" />
<item android:state_selected="true"
android:drawable="#drawable/actionbar_background" />
<item android:state_pressed="false"
android:drawable="#drawable/normal" />
</selector>
and on the function bound by the image view
v.setSelected(true);
selectedView = v;
and on onStop()
selectedView.setSelected(false);
Hope this will help anyone who stumbles upon this post

Android change imageButton when finger is hover on that

i'm create simple application to work with custom buttom if finger is pressed or released and hover on that. in this simple selector project have 4 state for change background image, but i need to change this button when finger pointer is focus or hover on that but that does not work and only press work correctly in my project.
<?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/logo"/>
<item android:state_focused="true"
android:state_pressed="true"
android:drawable="#drawable/gohome" />
<item android:state_focused="false"
android:state_pressed="true"
android:drawable="#drawable/goran" />
<item android:drawable="#drawable/logo" />
</selector>
In touch UI there is no such thing as "hover". Hover is when your pointer is over a control. Without having a pointer, you cannot reproduce this step.
You can play with "focused", "pressed" and default.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true"
android:drawable="#drawable/logo"/>
<item android:state_pressed="true"
android:drawable="#drawable/goran" />
<item android:drawable="#drawable/logo" />
</selector>

Define drawable for CheckedTextView for checked and pressed state in Android

I have current code
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_checked="true"
android:drawable="#drawable/list_button_selected" />
<item
android:state_pressed="true"
android:drawable="#drawable/list_button_pressed" />
<item
android:drawable="#drawable/list_button" />
</selector>
for my CheckedTextView and it works. But when it's checked and I press on it, it shows default android color for background. How can I overwrite that? I tried
<item
android:state_checked="true"
android:state_pressed="true"
android:drawable="#drawable/list_button_selected" />
But it does not work.
In my case, order is important. I had to switch checked and pressed and its working now.

Making a button look pressed in

I'm using Eclipse to write the android application. I've added some standard buttons from the Form Widgets tab and successfully got them opening new windows which display additional buttons.
I would like the button that was pressed, to change appearance and continue to look pressed in after it is selected.
create xml file using the button image like this
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:drawable="#color/transparent" />
<item
android:drawable="#drawable/closebutton" />
</selector>
add the color folder in values
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="transparent">#00000000</color>
</resources>
can use selector
<?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/button_settings" />
<item android:state_focused="true"
android:state_pressed="true"
android:drawable="#drawable/button_settings" />
<item android:state_focused="false"
android:state_pressed="true"
android:drawable="#drawable/button_settings_selected" />
<item android:drawable="#drawable/button_settings" />
</selector>
now set this drawable on background property of button in XML, now in coding take a boolean flag when button is pressed set the flag and set the bacground of the button(selected image) and on again click reset flag value and change the imageBackground to the selector again, thats it!!!
Use ImageButton and change the background of the button.
you can use this kind of images depends of you need. Like this https://web.archive.org/web/20160429124711/http://www.devahead.com/blog/2011/08/creating-a-custom-android-button-with-a-resizable-skin/

On click effect on the image button

I have created image buttons instead of normal buttons.On click on the image button I want a click effect as in the normal icon click effect(On click of menu the background of the icon will be yellow color) of android phones.Using the following code I code show an image on click of the image button .What changes I have to make further.
<item android:state_enabled="true"
android:state_pressed="true"
android:drawable="#drawable/production_order" />
android:state_enabled="false"
android:drawable="#drawable/prodution_orders_icon" />
Try this code
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="#drawable/camera" />
<item android:state_pressed="false" android:drawable="#drawable/camera" />
<item android:state_pressed="true" android:drawable="#drawable/camera_active" />
</selector>
Instead of using directly ImageButton you can use Button with customized background
Here is the sample tutorial.
create xml file using the button image like this
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:drawable="#color/transparent" />
<item
android:drawable="#drawable/closebutton" />
</selector>
add the color folder in values
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="yellow">#ffffff00</color>
</resources>

Categories

Resources