how to change background image of button when clicked/focused? - android

I want to change the background image of a button when clicked or focused.
This is my code:
Button tiny = (Button)findViewById(R.id.tiny);
tiny.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Button tiny = (Button)findViewById(R.id.tiny);
tiny.setBackgroundResource(R.drawable.a9p_09_11_00754);
TextView txt = (TextView)findViewById(R.id.txt);
txt.setText("!---- On click ----!");
}
});
Is this code right? Does it calls a button on its event?

you can implement in a xml file for this as follows:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="#drawable/your_imagename_while_focused"/>
<item android:state_pressed="true" android:drawable="#drawable/your_imagename_while_pressed" />
<item android:drawable="#drawable/image_name_while_notpressed" /> //means normal
</selector>
now save this xml file in drawable folder and name it suppos abc.xml and set it as follows
Button tiny = (Button)findViewById(R.id.tiny);
tiny.setBackgroundResource(R.drawable.abc);
Hope it will help you. :)

Its very easy to implement . For that you need to create a one xml file(selector file) and put it in drawable folder in res. After that set xml file in button's background in your layout file.
button_background_selector.xml
<?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/your_hover_image" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#drawable/your_hover_image" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#drawable/your_hover_image"/>
<item android:drawable="#drawable/your_simple_image" />
</selector>
Now set the above file in button's background.
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#color/grey_text"
android:background="#drawable/button_background_selector"/>

Sorry this is wrong.
For changing background color/image based on the particular event(focus, press, normal), you need to define a button selector file and implement it as background for button.
For example: button_selector.xml (define this file inside the drawable folder)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#000000" /> <!-- pressed -->
<item android:state_focused="true"
android:color="#000000" /> <!-- focused -->
<item android:color="#FFFFFF" /> <!-- default -->
</selector>
<!-- IF you want image instead of color then write
android:drawable="#drawable/your_image" inside the <item> tag -->
And apply it as:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawable="#drawable/button_selector.xml" />

use this code
create xml file in drawable folder name: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/buutton_pressed" />
<item
android:drawable="#drawable/button_image" />
</selector>
and in button xml file
android:background="#drawable/button"

To change the button background we can follow 2 methods
In the button OnClick, just add this code:
public void onClick(View v) {
if(v == buttonName) {
buttonName.setBackgroundDrawable
(getResources().getDrawable(R.drawable.imageName_selected));
}
}
2.Create button_background.xml in the drawable folder.(using xml)
res -> drawable -> button_background.xml
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"
android:drawable="#drawable/tabs_selected" /> <!-- selected-->
<item android:state_pressed="true"
android:drawable="#drawable/tabs_selected" /> <!-- pressed-->
<item android:drawable="#drawable/tabs_selected"/>
</selector>
Now set the above file in button's background file.
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/button_background"/>
(or)
Button tiny = (Button)findViewById(R.id.tiny);
tiny.setBackgroundResource(R.drawable.abc);
2nd method is better for setting the background fd button

You just need to set background and give previous.xml file in background of button in your layout file.
<Button
android:id="#+id/button1"
android:background="#drawable/previous"
android:layout_width="200dp"
android:layout_height="126dp"
android:text="Hello" />
and done.Edit Following is previous.xml file in drawable directory
<?xml version="1.0" encoding="utf-8"?>
<item android:drawable="#drawable/onclick" android:state_selected="true"></item>
<item android:drawable="#drawable/onclick" android:state_pressed="true"></item>
<item android:drawable="#drawable/normal"></item>

You can also create shapes directly inside the item tag, in case you want to add some more details to your view, like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape>
<solid android:color="#81ba73" />
<corners android:radius="6dp" />
</shape>
<ripple android:color="#c62828"/>
</item>
<item android:state_enabled="false">
<shape>
<solid android:color="#788e73" />
<corners android:radius="6dp" />
</shape>
</item>
<item>
<shape>
<solid android:color="#add8a3" />
<corners android:radius="6dp" />
</shape>
</item>
</selector>
Beware that Android will cycle through the items from top to bottom, therefore, you must place the item without condition on the bottom of the list (so it acts like a default/fallback).

Create a file in drawable play_pause.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"
android:drawable="#drawable/pause" />
<item android:state_selected="false"
android:drawable="#drawable/play" />
<!-- default -->
</selector>
In xml file add this below code
<ImageView
android:id="#+id/iv_play"
android:layout_width="#dimen/_50sdp"
android:layout_height="#dimen/_50sdp"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:background="#drawable/pause_button"
android:gravity="center"
android:scaleType="fitXY" />
In java file add this below code
iv_play = (ImageView) findViewById(R.id.iv_play);
iv_play.setSelected(false);
and also add this
iv_play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
iv_play.setSelected(!iv_play.isSelected());
if (iv_play.isSelected()) {
((GifDrawable) gif_1.getDrawable()).start();
((GifDrawable) gif_2.getDrawable()).start();
} else {
iv_play.setSelected(false);
((GifDrawable) gif_1.getDrawable()).stop();
((GifDrawable) gif_2.getDrawable()).stop();
}
}
});

use <androidx.appcompat.widget.AppCompatButton /> instead of 'Button'

Related

Disabling a Button with an icon

I have a simple Button, which has a drawable set as icon:
<Button
android:id="#+id/bOk"
android:drawableStart="#drawable/icon_ok"
android:text="#string/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
when I disable the button, either in XML layout file: android:enabled="false"
or programmaticaly: bOk.setEnabled(false);
The button gets disabled, it is 'grayed out', but the icon remains as it was ine the enabled state.
How can I get a look, that the icon is also 'grayed out'?
Create a new grayed icon and add both inside a selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/icon_ok" android:state_enabled="true" />
<item android:drawable="#drawable/icon_ok_disabled" android:state_enabled="false" />
</selector>
Use inside button like: android:drawableStart="#drawable/selector"
For TextColor,
Create another selector inside res/color/mycustomtextcolor.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#666" android:state_enabled="false" />
<item android:color="#000" android:state_enabled="true"/>
</selector>
Inside your widget call using: android:textColor="#color/mycustomtextcolor"
Or
inside your style add another item using: <item name="android:textColor">#color/mycustomtextcolor</item>
Like a regular Button, a Button that has an image background is not grayed when disabled.
You have to use another image it appears grayed.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false"
android:drawable="#drawable/button_gray" /> ***button_gray is a Drawable image***
<item android:state_pressed="true"
android:drawable="#drawable/button_gray" />
<item android:drawable="#drawable/button_red" /> ***button_red is a Drawable image***
</selector>
You can tint your drawable on enabled and disabled state using a selector with bitmaps:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false">
<bitmap android:src="#drawable/icon_ok" android:tint="#color/disableColorWithAlpha" />
</item>
<item android:state_enabled="true">
<bitmap android:src="#drawable/icon_ok" android:tint="#color/colorAccent" />
</item>
</selector>

Android selector with textColor [duplicate]

This question already has answers here:
Android customized button; changing text color
(5 answers)
Closed 6 years ago.
I have created a selector:
btn_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- pressed -->
<item android:drawable="#drawable/btn_bg_pressed" android:state_pressed="true" />
<!-- focused -->
<item android:drawable="#drawable/btn_bg_focused" android:state_focused="true" />
<!-- default -->
<item android:drawable="#drawable/btn_bg_default" />
</selector>
btn_bg_pressed.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid
android:color="#color/btn_pressed"/>
</shape>
btn_bg_focused.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid
android:color="#color/reality_fix_yellow"/>
</shape>
btn_bg_default.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="2dp"
android:color="#color/btn_default_border_color" />
</shape>
I want to change the text color the button when the focus changes. I've tried adding android:color="color_code" and android:textColor="color_code" but failed.
Please help me on How to change the text color of a button with selector?
Define this way :
Create button_selector.xml file in res/color directory
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#000000" /> <!-- pressed -->
<item android:state_focused="true"
android:color="#000000" /> <!-- focused -->
<item android:color="#FFFFFF" /> <!-- default -->
</selector>
and in Button property define this way.
<Button
android:TextColor="#color/button_selector"
/>
There's a trick where you can reuse your background drawable resource to change color for your text.
In your layout.xml, declare your button background and textColor in the following way:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/btn_bg"
android:textColor="#drawable/btn_bg"
android:text="Hello" />
Now, in your drawable/btn_bg.xml, define android:color="#color/your_color_code" as the item attribute:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- pressed -->
<item android:color="#color/md_green_500" android:drawable="#drawable/btn_bg_pressed" android:state_pressed="true" />
<!-- focused -->
<item android:color="#color/md_red_500" android:drawable="#drawable/btn_bg_focused" android:state_focused="true" />
<!-- default -->
<item android:color="#color/md_blue_500" android:drawable="#drawable/btn_bg_default" />
</selector>
You should be able to see background and text color change for your button.
You can acheive this on your own TextView class that extends the Android TextView class and override the onTouchEvent(MotionEvent event)
You can then modify the instances text color based on the MotionEvent passed.
For example:
#Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// Change color
} else if (event.getAction() == MotionEvent.ACTION_UP) {
// Change it back
}
return super.onTouchEvent(event);
}

Effect for ImageButton in android

I want to create effects for ImageButton. For example, it will change color when clicked...How I do it? I want to do this in .xml file. Can you help me! Thank you!
I tried to create the state.xml file as follwing:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:state_enabled="true"
android:drawable="#drawable/btn_0" />
<item
android:state_focused="true"
android:state_enabled="true"
android:drawable="#drawable/btn_ac" />
</selector>
However, I can't set background for ImageButton. The error like this:
All you have to do it to add the "android:background" attribut to your ImageButton and set a drawable.
Your layout
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/my_btn"
android:background="#drawable/btn_drawable"/>
btn_drawable.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/blue"
/>
<item android:state_focused="true"
android:drawable="#drawable/white"
/>
<item android:drawable="#drawable/green" />
</selector>
In that code above you set a different drawable when your ImageButton is pressed (state_pressed), focused (state_focus) or when is normal (not pressed and not focused).
Here you can find with more detail.
Create a drawable like below and name it as btn_drawable.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="false"
android:drawable="#drawable/btn_disable" />
<item
android:state_pressed="true"
android:state_enabled="true"
android:drawable="#drawable/btn_click" />
</selector>
This is for an example,Like this you can add the <item/> according to your needs and state of the image button.
Then set the drawable as image button background.
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/btn_drawable"/>

How do I set a different color for the pressed state of the button?

I have some Buttons on my android app. They have an icon and text. I can set the background color of a Button in java code. If the button is clicked I want to display with a different color. So, how do I set a different color for the pressed state of the Button?
<Button
android:id="#+id/save"
android:layout_width="130dip"
android:layout_height="wrap_content"
android:scaleType="center"
android:drawableTop="#drawable/save"
android:text="Save"
android:textColor="#FFFFFF"
android:textSize="14dip"
>
The onCreate method:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.homescreen);
save = (Button)findViewById(R.id.save);
save.setBackgroundColor(Color.rgb(27,161,226)); }
create xml file using the button image like this with mybutton.xml in drawable folder
<?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/blue" />
<item android:state_focused="true" android:drawable="#color/gold" />
<item android:drawable="#color/grey" />
</selector>
and use this in button xml code
android:background="#drawable/mybutton"
add those color codes in the resource-->values-->colors.xml like this
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="blue">#0066cc</color>
<color name="gold">#e6b121</color>
<color name="grey">#cccccc</color>
</resources>
Reference : Change button background on touch
Below is the sample code for color state list used for a button
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:color="#c0c0c0"
android:state_selected="true"/>
<item
android:color="#ffffff"
android:state_pressed="true"/>
<item
android:color="#9A9A9A"
android:state_focused="false"
android:state_pressed="false"
android:state_selected="false"/>
</selector>
Also please check below link for color state list
http://developer.android.com/guide/topics/resources/color-list-resource.html
Use a StateList. Below is an example of a selector with a different drawable for the pressed state:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/drawable_for_pressed_state" android:state_pressed="true"/>
<item android:drawable="#drawable/drawable_for_normal_state"/>
</selector>
You need to use a drawable with selector for pressed states, more commonly done in xml links below.
http://developer.android.com/reference/android/graphics/drawable/StateListDrawable.html
http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
edittext_modified_states.xml
<?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/apptheme_textfield_activated_holo_light" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#drawable/apptheme_textfield_focused_holo_light" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#drawable/apptheme_textfield_disabled_focused_holo_light"/>
<item android:drawable="#drawable/apptheme_textfield_default_holo_light" />
</selector>
here: http://android-holo-colors.com goto this website and select your color and imort into your drawable. goto layout xml and set button background. android:background="#drawable/edittext_modified_states"
If you want to change button background color then just do as follow..
#Override
public void onClick(View v) {
if(v.getId() == R.id.btn01) {
btn1.setBackgroundColor(Color.RED);
btn1.setTextColor(Color.WHITE);
}
just add this code in onclick event of button.

How to Change color of Button in Android when Clicked?

I am working on Android Application. I want to have 4 buttons to be placed horizontally at the bottom of the screen. In these 4 buttons 2 buttons are having images on them. The border of the buttons should be black color and the border should be as thin as possible. When I click the button, I want the background of the button should be changed to blue color without the color of border to be changed and should be remained in that color for some time. How can I achieve this scenario in Android?
One approach is to create an XML file like this in drawable, called whatever.xml:
<?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="true"
android:drawable="#drawable/bgalt" />
<item
android:state_focused="false"
android:state_pressed="true"
android:drawable="#drawable/bgalt" />
<item android:drawable="#drawable/bgnorm" />
</selector>
bgalt and bgnormare PNG images in drawable.
If you create the buttons programatically in your activity, you can set the background with:
final Button b = new Button (MyClass.this);
b.setBackgroundDrawable(getResources().getDrawable(R.drawable.whatever));
If you set your buttons' style with an XML, you would do something like:
<Button
android:id="#+id/mybutton"
android:background="#drawable/watever" />
And finally a link to a tutorial.
Save this code in drawable folder with "bg_button.xml" and call "#drawable/bg_button" as background of button in your xml:
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>
<solid
android:color="#004F81" />
<stroke
android:width="1dp"
android:color="#222222" />
<corners
android:radius="7dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:startColor="#89cbee"
android:endColor="#004F81"
android:angle="270" />
<stroke
android:width="1dp"
android:color="#4aa5d4" />
<corners
android:radius="7dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
</selector>
Try This
final Button button = (Button) findViewById(R.id.button_id);
button.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP) {
button.setBackgroundColor(Color.RED);
} else if(event.getAction() == MotionEvent.ACTION_DOWN) {
button.setBackgroundColor(Color.BLUE);
}
return false;
}
});
Refer this,
boolean check = false;
Button backward_img;
Button backward_img1;
backward_img = (Button) findViewById(R.id.bars_footer_backward);
backward_img1 = (Button) findViewById(R.id.bars_footer_backward1);
backward_img.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
check = true;
backward_img.setBackgroundColor(Color.BLUE);
}
});
if (check == true) {
backward_img1.setBackgroundColor(Color.RED);
backward_img.setBackgroundColor(Color.BLUE);
}
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- default -->
<item
android:state_pressed="false"
android:state_focused="false">
<shape
android:innerRadiusRatio="1"
android:shape="rectangle" >
<solid android:color="#00a3e2" />
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
</shape>
</item>
<!-- button focused -->
<item
android:state_pressed="false"
android:state_focused="true">
<shape
android:innerRadiusRatio="1"
android:shape="rectangle" >
<solid android:color="#5a97f5" />
<padding
android:bottom="5dp"
android:left="10dp"
android:right="10dp"
android:top="5dp" />
</shape></item>
<!-- button pressed -->
<item
android:state_pressed="true"
android:state_focused="false">
<shape
android:innerRadiusRatio="1"
android:shape="rectangle" >
<solid android:color="#478df9"/>
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
</shape></item>
</selector>
If you want to change the backgorund image or color of the button when it is pressed, then just copy this code and paste in your project at exact location described below.
<!-- Create new xml file like mybtn_layout.xml file in drawable -->
<?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/pressed" /> <!--pressed -->
<item android:drawable="#drawable/normal" /> <!-- Normal -->
</selector>
<!-- Now this file should be in a drawable folder and use this
single line code in button code to get all the properties of this xml file -->
<Button
android:id="#+id/street_btn"
android:layout_width="wrap_content"
android:background="#drawable/layout_a" > <!-- your required code -->
</Button>
1-make 1 shape for Button
right click on drawable nd new drawable resource file .
change Root element to shape and make your shape.
enter image description here
2-now make 1 copy from your shape and change name and change solid color.
enter image description here
3-right click on drawable and new drawable resource file
just set root element to selector.
go to file and set "state_pressed"
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"android:drawable="#drawable/YourShape1"/>
<item android:state_pressed="false" android:drawable="#drawable/YourShape2"/>
</selector>
4-the end go to xml layout and set your Button background "your selector"
(sorry for my english weak)
Try this......
First create an xml file named button_pressed.xml
These are it's contents.
<?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/icon_1" />
<item android:state_focused="true"
android:state_pressed="true"
android:drawable="#drawable/icon_1_press" />
<item android:state_focused="false"
android:state_pressed="true"
android:drawable="#drawable/icon_1_press" />
<item android:drawable="#drawable/icon_1" />
</selector>
Noe try this on your button.
int imgID = getResources().getIdentifier("button_pressed", "drawable", getApplication().getPackageName());
button.setImageResource(imgID);
button_pressed.xml should be in the drawable folder.
icon_1_press and icon_1 are two images for button press and normal focus.
you can try this code to solve your problem
<?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/login_selected" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/login_mouse_over" /> <!-- focused -->
<item android:drawable="#drawable/login" /> <!-- default -->
</selector>
write this code in your drawable make a new resource and name it what you want and then write the name of this drwable in the button same as we refer to image src in android
I am use this code (with ripple effect):
<ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="#color/color_gray">
<item android:id="#android:id/mask">
<color android:color="#color/color_gray" />
</item></ripple>
public void onPressed(Button button, int drawable) {
if (!isPressed) {
button.setBackgroundResource(R.drawable.bg_circle);
isPressed = true;
} else {
button.setBackgroundResource(drawable);
isPressed = false;
}
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.circle1:
onPressed(circle1, R.drawable.bg_circle_gradient);
break;
case R.id.circle2:
onPressed(circle2, R.drawable.bg_circle2_gradient);
break;
case R.id.circle3:
onPressed(circle3, R.drawable.bg_circle_gradient3);
break;
case R.id.circle4:
onPressed(circle4, R.drawable.bg_circle4_gradient);
break;
case R.id.circle5:
onPressed(circle5, R.drawable.bg_circle5_gradient);
break;
case R.id.circle6:
onPressed(circle6, R.drawable.bg_circle_gradient);
break;
case R.id.circle7:
onPressed(circle7, R.drawable.bg_circle4_gradient);
break;
}
please try this, in this code i m trying to change the background of button on button click this works fine.
To deal properly for how long you want to have your button stay in your other color I would advise this version:
button.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
button.setBackground(getResources().getDrawable(R.drawable.on_click_drawable));
break;
case MotionEvent.ACTION_UP:
new java.util.Timer().schedule(
new java.util.TimerTask() {
#Override
public void run() {
((Activity) (getContext())).runOnUiThread(new Runnable() {
#Override
public void run() {
button.setBackground(getResources().getDrawable(R.drawable.not_clicked_drawable));
}
});
}
}, BUTTON_CLICK_TIME_AFTER_RELEASE_ANIMATION);
break;
default:
}
return false;
}
});
BUTTON_CLICK_TIME_AFTER_RELEASE_ANIMATION indicates after how much time [ms] the button will reset to the previous state (however you might want to use some boolean to check that the button hadn't been used in between, depending on what you want to achieve...).
Even using some of the comments above this took way longer to work out that should be necessary. Hopefully this example helps someone else.
Create a radio_button.xml in the drawable directory.
<?xml version="1.0" encoding="utf-8"?>
<!-- An element which allows two drawable items to be listed.-->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="#drawable/radio_button_checked" /> <!--pressed -->
<item android:drawable="#drawable/radio_button_unchecked" /> <!-- Normal -->
</selector>
Then in the xml for the fragment should look something like
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioButton
android:id="#+id/radioButton1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:button="#drawable/radio_button"
android:paddingLeft="10dp" />
<RadioButton
android:id="#+id/radioButton2"
android:layout_marginLeft="10dp"
android:paddingLeft="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="#drawable/radio_button" />
</RadioGroup>
</LinearLayout>
</layout>
Just add 1 line in XML for the click item
android:background="?android:attr/selectableItemBackground"
hai the most easiest way is this:
add this code to mainactivity.java
public void start(View view) {
stop.setBackgroundResource(R.color.red);
start.setBackgroundResource(R.color.yellow);
}
public void stop(View view) {
stop.setBackgroundResource(R.color.yellow);
start.setBackgroundResource(R.color.red);
}
and then in your activity main
<button android:id="#+id/start" android:layout_height="wrap_content" android:layout_width="wrap_content" android:onclick="start" android:text="Click">
</button><button android:id="#+id/stop" android:layout_height="wrap_content" android:layout_width="wrap_content" android:onclick="stop" android:text="Click">
or follow along this tutorial

Categories

Resources