Android button with image background and text and stateful functionality - android

I want to create "button" in XML layout (if possible) which has background image based on state (this image is NOT 9-patched) and with text. Which component to use?
My button_states.xml looks like:
<?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/my_pressed" />
<item android:drawable="#drawable/my_normal" />
</selector>
my_pressed and my_normal are NON 9-patched images.
Now if I use regular button
<Button
android:id="#+id/my_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/button_states"
android:text="#string/my_text" />
button it is rendered without background image.
How to do "button" with text and stateful "static" backgrounds?
Thx
Regards

The easiest way to accomplish this is doing it in code. Use setBackgroundDrawable() on the (regular) Button to set the desired background. If you use your selector as the provided drawable you should be fine.
[Edit] To do this using resources, place your button_state.xml in the res\drawable folder in use the following code:
Button button = (Button) findViewById(R.id.my_button);
button.setBackgroundResource(R.drawable.button_state);

Related

How to change the selector/ripple color on a standard button?

I'm using a standard button in my layout:
<Button
android:id="#+id/earlier_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="earlier"/>
And I figured out how to change it's color without changing it's default style:
earlierButton = (Button) findViewById(R.id.earlier_button);
earlierButton.getBackground().setColorFilter(getResources().getColor(R.color.red_500), PorterDuff.Mode.MULTIPLY);
My question is now, how can I set a new selector/ripple or change the color of the current one? Does anyone know the default theme/style, which needs to be overwritten for that? Talking about these buttons:
You can set selector.
See this link for detail. https://stackoverflow.com/a/28431028/850347
You have Button,
<Button
android:id="#+id/earlier_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="earlier"/>
And you may have selecor,
btn_selecor.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/btn_normal" android:state_pressed="false"/>
<item android:drawable="#drawable/btn_pressed" android:state_pressed="true"/>
</selector>
btn_normal, btn_pressed is png images.
Note that this btn_selecor.xml must be placed folder name "drawable"
Finally, set selector to your button programmatically.
Drawable buttonDrawable = getResources().getDrawable(R.drawable.btn_selector);
buttonDrawable.mutate();
btn1.setBackground(buttonDrawable);
Edit1:
Sure, you can set android default button selector by using code below.
In this case, you don't need to make your own selector.
Drawable buttonDrawable = getResources().getDrawable(android.R.drawable.btn_default);
buttonDrawable.mutate();
btn1.setBackground(buttonDrawable);

how to add a rounded rectangular toggle_switch in android?

I need to make a rounded rectangular toggle_switch in android like the one given below:
Can anyone guide me the complete steps to do so.
I solved my problem as follow :
Added a toggle button to my xml layout file :
<ToggleButton
android:id="#+id/ToggleButton1"
android:layout_width="120dp"
android:layout_height="25dp"
android:layout_marginRight="30dp"
android:layout_weight="2"
android:background="#drawable/toogle_switch"
android:text="ToggleButton"
android:textOff=""
android:textOn="" />
Then defined a custom togglebutton Background "toogle_switch" in 'drawable' folder as below:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/switchon" android:state_checked="true"></item>
<item android:drawable="#drawable/switchoff" android:state_checked="false"></item>
</selector>
switchon & switchoff are the 2 images I have shown in question.
Hope it helps everyone.! :)
Here you go:
http://developer.android.com/guide/topics/ui/controls/togglebutton.html
Exact image shown is determined by so called 'selector' or 'state list', which is a piece of XML that maps button states to images.
http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
You need to:
prepare images for all possible states of the button (toggled, pressed, etc) and place them in drawable folders
write a state list (selector) that binds images with button states
connect this state list to button android:background property

how can I implement non-rectangular shapes for buttons in android

hi i have to realize this layout . it has this layout.
I could try to use the icons as imagebuttons but the active state of a button is somewhat like this one !
How should i proceed with this ?
You should use selector as follows:
Prepare 2 images for button states, and put it into res/drawable folder.
button_normal_green.png – Default image button.
button_pressed_yellow.png – Display when button is pressed.
Now, create a new XML file in “res/drawable/” folder, in whatever name you want, in this case, we just give a name as “new_button.xml“. This file defined which button state is belong to which image.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_pressed_yellow" android:state_pressed="true" />
<item android:drawable="#drawable/button_normal_green" />
</selector>
3.set background to button
<ImageButton
android:id="#+id/imageButtonSelector"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/new_button" />
Have a look at Complete Example

how to use an image as a button

this should be an easy one and I really thought that what I was doing was correct but apparently it isn't. I have an image that I want to use as a button, I have the button created and working. But now I just want this image to show up instead of the standatd android "button".
Anyone know how to do this?
Make a use of <selector
Create in your drawable-hdpi folder xml file called button_image.xml
and add the following lines:
<?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/picture" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#drawable/picture_pressed" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#drawable/picture_pressed" />
<item android:drawable="#drawable/picture" />
</selector>
and then in your main xml where you declare your button add this line
android:background="#drawable/button_image"
And then you get effect of reall button. Note that you need two pictures to get effect of button being clicked.
If you dont want to use the selector and use just picture instead then just
android:background="#drawable/myimage"
Use ImageButton class.
You can Use ImageButtons instead of regular buttons
The just set your background from your image in your XML
<ImageButton android:background="#drawable/mypicture"/>
A nice tutorial is Here
Follow me, You can use many controls in android to solve this problem (Button, ImageView, ImageButton, or even TextView ... ). Just sets an image as its background and then implements OnClickListener to handle click event.
Example, with ImageView,
- in layout file:
<ImageView
android:id="#+id/button_login"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:background="#drawable/button_login"
/>
in behind-code file:
ImageView login;
login = (ImageView) findViewById(R.id.button_login);
login.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//TODO: your code here
}
});
You need to write the android:background="#drawable/my_pic" to your xml file at the button. Where my_pic is your pic's name. Hope it helps you.

ImageButton in Android homescreen widget

I have a homescreen widget with an imagebutton. I have the button working with a pending intent, but I can't seem to figure out how to change the button image when it is pressed.
I tried using a selector and it works in my widget test activity, but not in the remoteview.
How could I implement this functionality in the home screen widget?
You can set a different Image within your remote view.
remoteView.setInt(R.id.buttonimg, "setImageResource", R.drawable.button_on);
I was able to get it working by moving the selector inside it's own XML file and putting in res/drawable and then referencing that xml file as my resource for my imagebutton in the app-widget's layout xml.
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 now have a different problem, however. The selector only allows me to change the image onPress. On release, the image changes back >:-(
How do I get the image state change on press and stay in that state until next press (like the ToggleButton)?

Categories

Resources