I want to create custom ImageButton, but to work like on/ off button. On click on button image to be changed to pressed(until another button is pressed)!
On picture This month button is on , This year off .
How can I create button like this?
Do I need to use and how ?
Thanks
Use the selector XML tag to help you achieve this. Here, see this link about StateListDrawables. So their example shows
a button.xml file:
<?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" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/button_focused" /> <!-- focused -->
<item android:drawable="#drawable/button_normal" /> <!-- default -->
</selector>
and then links an actual button to that xml:
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="#drawable/button" />
Sergey Glotov and wheaties thanks for your help
For every ImageButton I create separate selectors for default and pressed state, I create ImageButton in xml:
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/month_button"
android:id="#+id/btnMonth"
android:onClick=ButonMonthClick"/>
And onClick event for all ImageButton I change background image programmatically:
btnWeek.setBackgroundResource(R.drawable.week_pressed);
btnMonth.setBackgroundResource(R.drawable.month_default);
Thant solve my problem!
Related
I would like to adding some effects like color change to my android button when user focus/click the button
My android button layout is here :
<Button
android:id="#+id/btnUpload"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#color/btn_bg"
android:text="தகவல் அனுப்புக"
android:textColor="#color/white"
android:padding="20dp"
android:margin="20dp"
android:layout_alignParentBottom="true" />
You should check out the documentation regarding color state lists.
Create an XML file in your drawable folder with the name of btn_background and place the following code in the file:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#ffff0000"/> <!-- pressed -->
<item android:state_focused="true"
android:color="#ff0000ff"/> <!-- focused -->
<item android:color="#ff000000"/> <!-- default -->
</selector>
After you have created this file, change the background attribute of your Button to point to the new background:
android:background="#color/btn_background"
Good luck and happy coding!
I have an ImageButton:
<ImageButton
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/btn_70s"
android:src="#drawable/btn_70s"
android:background="#null" /* Sets transparent BG for button image(PNG) */
/>
The image is "btn_70s", how do I set a "pressed" version of the image for when the ImageButton is tapped?
The documentation states to create an XML file and place it in the drawable directory:
<?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" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/button_focused" /> <!-- focused -->
<item android:drawable="#drawable/button_normal" /> <!-- default -->
</selector>
This to me says it would affect all the ImageButtons rather than an individual one. Would I save it as an individual XML for each button for example (btn_70s_custom.xml), and then use it like:
<ImageButton
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/btn_70s"
android:src="#drawable/btn_70s"
android:android:background="#drawable/btn_70s_custom"
/>
Only issue with this, I can't use #null to allow transparency. Any assistance is appreciate.
Have a look over there for a (possible) solution to your problem : Make button background transparent using selector
And for the record,
This to me says it would affect all the ImageButtons rather than an individual one. Would I save it as an individual XML for each button for example (btn_70s_custom.xml), and then use it like:
No, it won't set this theme for all the buttons, but only for the ones where you specify to use this selector.
Hope this helps!
You can try selectableItemBackground
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"/>
I would like to change the default button button style of Android radio buttons to suit my theme. How could I achieve that?
Create a selector drawable
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/radio_selected" android:state_checked="true" />
<item android:drawable="#drawable/radio_normal" />
</selector>
then in your xml of the radio button
android:button="#drawable/(your drawable name)"
example:
<RadioButton
android:id="#+id/radio_button_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/your_drawable_name"
android:text="your_text" />
See Adding custom radio buttons in android. Hope this helps you.
I can take action when a view (button) is clicked on home screen widget. The thing is, I also want to show an effect so user will feel that button is alive. (Like the blue glow when you press on start/stop button on Music Player widget.)
Is this possible? If so, how? If not, what would be the alternative?
Here is a sample code for widget selector..
buttonimg.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="false"
android:drawable="#drawable/button_off" />
<item
android:state_pressed="true"
android:drawable="#drawable/button_on" />
<item
android:drawable="#drawable/button_off" />
</selector>
widget_layout.xml
<ImageButton
android:id="#+id/buttonimg"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_x="0px"
android:layout_y="2px"
android:clickable="true"
android:src="#drawable/button"
>
I was wondering how you can create buttons like the ones at the bottom of the screen and the home button at the top?
Quite hard to understand if you have doubts about button design and states or button position.
If it's a design issue, you should create your button states on your favorite software, as example, photoshop.
Then, in your android layouts you point your button background src to a drawable XML containing all button states.
Example XML for button:
<Button android:id="#+id/back"
android:layout_width="184px" android:layout_height="44px"
android:background="#+drawable/back" />
After this, you need your custom picture for the button, and the button XML on your drawables folder,
here's the XML you need for your 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/button_pressed" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/button_focused" /> <!-- focused -->
<item android:drawable="#drawable/button_normal" /> <!-- default -->
</selector>
Follow up on: http://developer.android.com/reference/android/widget/ImageButton.html
The title bar you mentioned about is called "Action Bar"
You can learn how to implement it here:
http://android.cyrilmottier.com/?p=274
https://github.com/johannilsson/android-actionbar/
Regards.