Make an Android Button Change Background on Click Through Xml

Make an Android button change background on click through XML

To change the image by using code:

public void onClick(View v) {
if(v.id == R.id.button_id) {
ButtonName.setImageResource(R.drawable.ImageName);
}
}

Or, using an 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/login_selected" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="@drawable/login_mouse_over" /> <!-- focused -->
<item android:drawable="@drawable/login" /> <!-- default -->
</selector>

In OnClick, just add this code:

ButtonName.setBackgroundDrawable(getResources().getDrawable(R.drawable.ImageName));

Android Change background color when clicked

This is because by default when the button is touched it will take click instead of focus.If you want button to be focused and change its color when pressed add this to your button in xml.

android:focusableInTouchMode="true"

Button - Change background color on click

Create a shape named button_pressed.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android">

<solid android:color="@color/blue" />

<stroke
android:width="4dp"
android:color="@color/blue" />

<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />

</shape>

Suppose, you have two buttons whose IDs are R.id.btn and R.id.btn1

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="12dp"
android:background="@drawable/button_pressed"
android:onClick="onClick"
android:text="Press Me 1" />

<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="12dp"
android:background="@drawable/button_pressed"
android:onClick="onClick"
android:text="Press Me 2" />

</LinearLayout>

Write the onClick() method which will allow you to retain the changed color until another button is pressed.

Button button;

public void onClick(View v) {

Drawable dr = getResources().getDrawable(R.drawable.button_pressed);
dr.setColorFilter(Color.parseColor("#FF0000"), PorterDuff.Mode.SRC_ATOP);

switch (v.getId()) {
case R.id.btn:

if (button == null) {
button = (Button) findViewById(v.getId());
} else {
button.setBackgroundResource(R.drawable.button_pressed);
button = (Button) findViewById(v.getId());
}
button.setBackgroundDrawable(dr);

break;

case R.id.btn2:
if (button == null) {
button = (Button) findViewById(v.getId());
} else {
button.setBackgroundResource(R.drawable.button_pressed);
button = (Button) findViewById(v.getId());
}
button.setBackgroundDrawable(dr);

break;

default:
break;
}
}

I think, now you will get What you wanted to do.

Change button background color on click event?

We need an XML file, drawable selector. I named this file with res/drawable/bg_button.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_focused="true" android:drawable="@drawable/bg_button_pressed"/>
<item android:state_pressed="true" android:drawable="@drawable/bg_button_pressed" />
<item android:drawable="@drawable/bg_button_normal" /> <!-- normal state -->
</selector>

For res/drawable/bg_button_pressed.xml:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="color when pressed" />
</shape>

Create a new XML file, name it res/drawable/bg_button_normal.xml, and copy above code. Then, replace android:color with normal color, i.e. when button on normal state.

And finally:

<Button
android:id="@+id/..."
android:text="@string/..."
android:background="@drawable/bg_button"/>

If you didn't have res/drawable folder, create a new one.

Change Button Background when pressed - Android

Create XML into res/drawable/button.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="#343434" />
<stroke
android:width="1dp"
android:color="#171717" />
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:startColor="#343434"
android:endColor="#171717"
android:angle="270" />
<stroke
android:width="1dp"
android:color="#171717" />
<corners
android:radius="4dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
</selector>

And set to your Button as Background like

  <Button
android:id="@+id/btnValidate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@drawable/button"
android:text="SOS Trasition"
android:textColor="@android:color/white"
android:textSize="16sp"
android:textStyle="bold" />

And if you want Change Button Text Color then create XML file at res/color/button_text.xml:

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

And set this layout XML will apply the color list to a View:

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/button_text"
android:textColor="@color/button_text" />

Unable to change button background color on click

When I click the button the color changes and disappears I want it to stay until user press any other button

You have no code to do that. A Button does not have a durable state that changes when the use clicks on it. You will have to do something yourself, in Java/Kotlin code, to do that.

For example, you could toggle the activated state via setActivated() on the Button. Then, in your btn_selector resource, you could have different drawables for android:state_activated="true" than for android:state_activated="false".

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

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. :)



Related Topics



Leave a reply



Submit