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/
Related
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>
I have an image for my button.
In order to make use of it I have to have 1 additional image for each state:
1. disabled
2. selected
3. pressed
etc..
in iOS all those additional states are handled automatically and deferred from original image provided.
Is it possible to accomplish this for Android?
Is it possible to accomplish this for Android?
No, It is NOT. You need to have all states's images with you to define the selector
You can define button_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Selected state -->
<item android:drawable="#drawable/button_bg_selected" android:state_selected="true"></item>
<!-- Pressed state -->
<item android:drawable="#drawable/button_bg_pressed" android:state_pressed="true"></item>
<!-- Disabled state -->
<item android:drawable="#drawable/button_bg_disabled" android:state_enabled="false"></item>
<!-- Normal state -->
<item android:drawable="#drawable/button_bg_normal"></item>
</selector>
Then simply assign this selector as a background of a button
<Button
android:id="#+id/button1"
android:background="#drawable/button_selector"
android:layout_width="200dp"
android:layout_height="126dp"
android:text="Hello" />
Refer : Button Selector
Hope it will help you ツ
For selected and pressed you can use an xml file with root for button's background but for disabled I guess you need to handle it in java code. I'm not sure about disabled state.
You will have to use selector for android.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/pressed"
android:state_pressed="true" />
<item android:drawable="#drawable/focused"
android:state_focused="true" />
<item android:drawable="#drawable/normal" />
</selector>
android:background="#drawable/selector_button" />
just have a look of this link.
As far as I know you need to handle this states in a new resource file called cursor: e.g:
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/edittext_selector" android:layout_height="fill_parent"
android:layout_width="fill_parent">
<!-- Image display in background in select state -->
<item
android:state_pressed="true"
android:drawable="#drawable/edittextback1">
</item>
<!-- Image display in background in select state -->
<item
android:state_enabled="true"
android:state_focused="true"
android:drawable="#drawable/edittextback2">
</item>
<!-- Default state -->
<!--<item android:state_enabled="true"-->
<!--android:drawable="#drawable/your_ninepath_image">-->
<!--</item>-->
</selector>
This answer refers to how you can handle button states in Android
You can create a button layout file separately in put that in res > layout folder
For Instance:
if the layout file name is btn.xml
<?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/btn_pressed" />
<item android:drawable="#drawable/btn_normal"/>
</selector>
You can set BackgroundResource of a Button
yourButton.setBackgroundResource(R.layout.btn);
EDIT
In addition you can refer to this link which is more closer to answer your question
I want to create a button animation like nexus back button in below image.
When it's clicked, it's highlighted with oval shape and then bending the transparency of background color smoothly.
Approximately similar effect you can achive by selector like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:exitFadeDuration="#android:integer/config_mediumAnimTime" >
<item android:state_pressed="false" android:state_focused="true" android:drawable="#color/red" />
<item android:state_pressed="true" android:drawable="#color/dark_red" />
<item android:drawable="#color/red" />
</selector>
add this selector as bg_selector.xml in your res/draweble folder.
And then set it is as background of TextView(or other View):
android:background="#drawable/bg_selector"
P.S: All magic in android:exitFadeDuration
Is there a way to add a pressed state to an ImageView? I have an image that i place a click listener on, and when I press it I want to change to imageview src for a second to mimic the pressed state of buttons or listview items.
Can I add a selector xml to the src attribute?
Figured it out. You CAN add a selector xml to your src attribute of an ImageView.
In my case, i created "addbuttonbg.xml" in my drawables:
<?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/quickaddbutton" />
<item android:state_pressed="true"
android:drawable="#drawable/quickaddbutton_click" />
<item android:state_selected="true"
android:state_pressed="false"
android:drawable="#drawable/quickaddbutton" />
</selector>
Then set your imageview src to #drawable/addbuttonbg
Yes you can add a selector to get the press effect
Sample:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/postbutton_press" android:state_pressed="true"/>
<item android:drawable="#drawable/postbutton_press" android:state_focused="true"/>
<item android:drawable="#drawable/postbutton_normal"/>
</selector>
To the ImageView's android:src="#drawable/post_btn_click" attribute or android:background="#drawable/post_btn_click" to a button attribute
post_btn_click -> selector drawable's file name
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>