ImageButton in Android homescreen widget - android

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

Related

Image Button change size onClick

On Android Studio I made an image perform like a button. Though what I can't find anywhere is how do I make it so when I click on the image aka the button it becomes a bit smaller and when you release it comes back to original size. Ik that onClick you can perform functions and activities and what not but how do I change the actual button image so as I'm holding down the click button it gets smaller and when i release it comes back to its original size.
You can use selectors to achieve that. For example define this in your drawable folder:
selector_state_xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_bg_selected" android:state_selected="true"></item>
<item android:drawable="#drawable/button_bg_pressed" android:state_pressed="true"></item>
<item android:drawable="#drawable/button_bg_normal"></item>
</selector>
And use this as your View drawable:
<Button
android:id="#+id/your_button"
android:background="#drawable/selector_state_xml"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
An alternative could be using ScaleAnimation

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

Android - change background of component while KEEPING selector?

Is this possible? I have for example simple linearLayout with selector to create "click effect":
<LinearLayout
android:id="#+id/clickToChangeColor"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#color/click_effect" />
This is selector click_effect:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/pressed" android:state_enabled="true" android:state_pressed="true"/>
<item android:drawable="#drawable/unpressed" android:state_enabled="false"/>
</selector>
Everything works just fine, but when I press on this layout, I would like to launch activity (like color picker - but it doesn't matter) and then change color of linearlayout for example to blue. BUT: keep selector "click effect". Trying to do this almost for 2 hours, but nothing works...
PS: I know it can be done for example with another layout inside this layout, apply some padding and apply selector to outer layout and then I can change background of inner layout etc - but it's only ugly workaround
<ImageView
android:id="#+id/imageViewSelectedColor"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#color/click_effect"
android:src="#color/picked color" />
Use an image view instead now you can change src according to picked color.

Android button with image background and text and stateful functionality

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

What makes a button change color on mouseclick?

Why is this button changing color to orange when clicked:
<Button android:background="#android:drawable/btn_plus" ...>
but this one is not?
<Button android:background="#drawable/ic_btn_round_plus" ...>
Edit:
Found another type of button (text and image) that changes color to orange when clicked
without having to create a selector:
<Button android:text="List" android:drawableTop="#drawable/list" ...>
because the first one is from android framework and has a selector associated to it, and the other one is a custom from your code, and you obviously didn't put a selector on it.
This is nicely explained here.
In short you need to put a selector drawable in the background of your button, instead of just one drawable :
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="#drawable/ic_btn_round_plusorange" />
<item android:state_pressed="true" android:drawable="#drawable/ic_btn_round_plusorange" />
<item android:drawable="#drawable/ic_btn_round_plus" />
</selector>
and you create you copy of your drawable but with an orange color added to it for instance.
Android system will switch the drawable when the button is clicked or selected.

Categories

Resources