Change button background on touch - java

i want to change the green background of a button when this is touched without using OnTouchListener. i have this situation: i put some butons in a layer, then the layer in another layer, and the last layer in onother layer. when i touch the button the background is changing(i m using OnTouchListener right now) but if i drag the finger outside of the button and then get it out of the screen the background of the button remains the image from the state when it s touch(otherwise if i click the button and the the finnger off the button it is k the background is changing)

1. Prepare 3 images for button states, and put it into resource/drawable folder.
2. create a new XML file in res/drawable/ folder, in whatever name you want, in this case, we just give a name as my_button.xml. This file defined which button state is belong to which image.
Now, you can refer to this button via this Id : #drawable/my_button.
File : res/drawable/my_button.xml
create xml file using the button image like this with my_button.xml in drawable folder
<?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_focused_orange"
android:state_focused="true" />
<item android:drawable="#drawable/button_normal_green" />
</selector>
add a normal button, and attach the background image to above “my_button” via
android:background:#drawable/my_button

The selector will be as below
<?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" /> <!-- pressed -->
<item android:state_focused="true" android:drawable="#drawable/btn_focused" /> <!-- focused -->
<item android:drawable="#drawable/btn_default" /> <!-- default -->
</selector>

Related

How to set a button background and back according to selected state not pressed state

I currently have a sound button,and I would like to change its background every time when it's selected and clicked(I'm developing on a pair of android glasses, so when the button is selected it's not pressed, thus two different states).
I have used the xml file to change button background when selected so far:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/no_music"
android:state_selected="true" />
<item android:drawable="#drawable/no_music"
android:state_focused="true" />
<item android:drawable="#drawable/music" />
</selector>
And together in the onClick method of the button, I set the button background according to the state:
public void musicPlay(View view) {
Button music = (Button) findViewById(R.id.music);
if(isPlaying) {
music.setBackgroundResource(R.drawable.no_music);
MusicManager.release()
}else{
music.setBackgroundResource(R.drawable.music);
MusicManager.start(this);
}
isPlaying = !isPlaying;
}
When I click the button, each time it would change its background. But when I select it, it would only change background once. Is there any method that I can use to make selected state the same as the pressed one?
Thank you very much.
Do this in your code:
public void musicPlay(View view) {
Button music = (Button) findViewById(R.id.music);
if(isPlaying) {
music.setPressed(true);
MusicManager.release()
}else{
music.setPressed(false);
MusicManager.start(this);
}
isPlaying = !isPlaying;
}
Also in your xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/no_music" android:state_selected="true"/>
<item android:drawable="#drawable/no_music" android:state_pressed="true"/>
<item android:drawable="#drawable/music"/>
</selector>
you could make an drawable resource as resource for the button:
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:constantSize="true">
<item android:drawable="#drawable/musicon" android:state_checked="false"/>
<item android:drawable="#drawable/musicoff" android:state_checked="true" />
<item android:drawable="#drawable/default" />
something like this should work. good luck.
Set your button pressed property to true, then verify if your button background.xml file has selected item.

How to make an opaque Activity?

I'm using QuickAction3D library and I would like to make the background sort of opaque when the QuickAction it's clicked, just like a normal AlertDialog would do, it's there a way of doing this?.
Thank you very much
EDIT
I meant the Activity go to an opaque color, and the quick action preserve it's current color. For example, If an AlertDialog it's shown, the background activity would be turned opaque, and the AlertDialog it's on a clear color.
Quich action 3d
See in that library for the background they have used a selector drawable action_item_btn.xml, you can customize the code to make as per ur need.They have set as
<item
android:state_pressed="true"
android:drawable="#drawable/action_item_selected"/>
for the background if u want to set the selector for the items in the list u can set selector for the imageView present there like,
Create a background drawable resource, e.g. item_background.xml, in your /drawable folder, containing something like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/color1" android:state_pressed="true"/>
<item android:drawable="#color/color2" android:state_selected="true"/>
<item android:drawable="#color/color3" android:state_activated="true"/>
<item android:drawable="#color/color4"/>
</selector>
Then provide the color values in your /values/colors.xml file:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="color1">#DDff8800</color>
<color name="color2">...</color>
<!-- more colors... -->
</resources>
Finally, set that drawable resource as the item background in your layout with android:background="#drawable/item_background".

Change only one state (the default state) of an ImageButton background

I'd like to change the background for an image button. Basically the image I have looks great on a transparent background and a bit lousy when there are a bunch of them all with a non-transparent background.
This should be easy - just change the android:background on the button to a transparent color (via a drawable):
click_background.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true"><color android:color="#FF008080" />
</item>
<item android:state_pressed="true"><color android:color="#FF008080" />
</item>
<item><color android:color="#00000000" />
</item>
</selector>
Then the button
<ImageButton
android:id="#+id/players"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:background="#drawable/click_background"
android:contentDescription="#string/players"
android:src="#drawable/speaker_tile" />
The problem is that I actually want to retain the state_focussed and state_pressed versions of the background (which I presume is derived from the theme). I would like to have the background appear when the button is pressed or selected (and appear with the exact same colour/drawable) that would be used by the theme normally when a button is pressed would be helpful.
I was wondering if there is a way to do one of the following, but have so far been unable to find anything on either:
Define a selector that in some what inherits from another (similar to the way a theme can inherit from another).
Create an entirely new selector but have it's XML reference the color/drawable of the theme's state_focussed and state_pressed used for an image button. Edit: It looks like this option is out. I would have needed attributes, but you can't reference them from a drawable. See here
I'd like to do this in Declarative XML rather than Programmatic Java if possible.
You can copy the drawables used by the Android OS into your project and use them in your state-list drawable. You can find the images in {android-sdk-directory}/platforms/android-##/data/res/drawable[-*dpi]
EDIT:
If you go to {android-sdk-directory}/platforms/android-##/data/res/values, you'll find the themes.xml and styles.xml files used by Android. Using them you can figure out which drawables you'll be looking for.
For example, the default theme on newer versions of Android is Theme.Holo. This theme has a default style for ImageButtons declared like so:
<item name="imageButtonStyle">#android:style/Widget.Holo.ImageButton</item>
In styles.xml, this style is defined as follows:
<style name="Widget.Holo.ImageButton" parent="Widget.ImageButton">
<item name="android:background">#android:drawable/btn_default_holo_dark</item>
</style>
Thankfully the background attribute is right there defined in plain sight. Sometimes it's inherited from a parent style and you have to find that instead. In any case, here's the drawable (found in the /drawable directory since it's xml):
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_enabled="true"
android:drawable="#drawable/btn_default_normal_holo_dark" />
<item android:state_window_focused="false" android:state_enabled="false"
android:drawable="#drawable/btn_default_disabled_holo_dark" />
<item android:state_pressed="true"
android:drawable="#drawable/btn_default_pressed_holo_dark" />
<item android:state_focused="true" android:state_enabled="true"
android:drawable="#drawable/btn_default_focused_holo_dark" />
<item android:state_enabled="true"
android:drawable="#drawable/btn_default_normal_holo_dark" />
<item android:state_focused="true"
android:drawable="#drawable/btn_default_disabled_focused_holo_dark" />
<item
android:drawable="#drawable/btn_default_disabled_holo_dark" />
</selector>
So those are the drawables Android uses for the background of a standard ImageButton in the default (holo dark) theme.

Android ImageButton doesnt change on focus

I have 4 buttons, they are all image buttons with "#null" as background.
In the xml file I have:
<ImageButton android:src="#drawable/buttonimgplay" android:id="#+id/imageButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="startGame" android:background="#null"></ImageButton>
buttonimgplay is another xml file containing this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/playpressed" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/playabitpressed" /> <!-- focused -->
<item android:drawable="#drawable/playnotpressed" /> <!-- default -->
However, when the user presses the button on the screen it doesnt change, as it switches view to something else. on buttons i havent initialised (ie attached a method) yet this image switching works fine.
Any ideas how I can get the image on the button to change before the view is changed?
Thanks,
Ben
Have you tried adding state_selected to your statelist as well? I don't remember if I had the exact same scenario, but I know that I had to start adding this to some of my state list drawables to get them to work, and I believe it was exactly what you're experiencing.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/playpressed" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/playabitpressed" /> <!-- focused -->
<item android:state_selected="true"
android:drawable="#drawable/playabitpressed" /> <!-- selected -->
<item android:drawable="#drawable/playnotpressed" /> <!-- default -->

How to modify ImageButton image on press through XML?

I have an image button set up like so:
<ImageButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/navup"
android:layout_weight="33"
android:layout_margin="5dip"
android:src="#drawable/up_button_icon"
android:background="#drawable/up_button" />
"#drawable/up_button_icon" is a png.
/res/drawable/up_button.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/up_button_press" />
<item
android:drawable="#drawable/up_button_norm" />
</selector>
The "press" states are png's that have a sunken look to them. Think of hitting a button on your tv's remote control.
The button is rendered correctly, however there is a problem when the button is pressed. The up_icon image stays stationary. For a better UI, the icon should shift down 2px so that it follows the "sunken" background.
Ideally, I would like to set this up through the XML layouts, but I am open to code solutions as well.
I think making a sunken version of the icon too and create another StateListDrawable could be a solution.

Categories

Resources