Android Button color changing on onClick?
buttoncolor.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>
Now use like below:
b1 = (Button) findViewById(R.id.b1);
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
b2.setBackgroundDrawable(getResources().getDrawable(R.drawable.whatever));
}
});
b2 = (Button) findViewById(R.id.b2);
b2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
b1.setBackgroundDrawable(getResources().getDrawable(R.drawable.whatever));
}
});
Changing color of buttons with onclick events
Try this:
Button button = (Button)findViewById(R.id.grid_button);
button.setOnClickListener(new View.OnclickListener()
{
@Override
public void onClick(View view){
button.setBackgroundColor("#4FC3F7");
}
};
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"
How to change color of button when being click,and revert back to default color in next click?
Hi try to this hope this can help you...
In XML
<Button
android:id="@+id/btnClick"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:text="click"/>
In Adapter Class
boolean click = true;
holder.btnClick.setTag(position);
holder.btnClick.setId(position);
holder.btnClick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (click) {
holder.btnClick.setBackgroundColor(Color.RED);
click = false;
} else {
holder.btnClick.setBackgroundColor(Color.WHITE);
click = true;
}
notifyDataSetChanged();
}
});
How to change the color of a button?
You can change the colour two ways; through XML or through coding. I would recommend XML since it's easier to follow for beginners.
XML:
<Button
android:background="@android:color/white"
android:textColor="@android:color/black"
/>
You can also use hex values ex.
android:background="@android:color/white"
Coding:
//btn represents your button object
btn.setBackgroundColor(Color.WHITE);
btn.setTextColor(Color.BLACK);
Android: Change button color when clicked
boolean tmp = false;
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
tmp = !tmp;
v.setBackgroundColor(tmp ? Color.RED : Color.BLUE);
}
});
EDIT: apparently you want to have a more complex example
First create a drawable XML in and name it pink_button.xml and place the following code inside
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#FF5EF1"/>
<corners android:radius="15dp"/>
<stroke
android:width="1dp"
android:color="#303030"/>
</shape>
Now make a blue_button.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#008DFF"/>
<corners android:radius="15dp"/>
<stroke
android:width="1dp"
android:color="#303030"/>
</shape>
Now make some demo activity layout, I used button_demo_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:id="@+id/btnDemo"
android:layout_width="150dp"
android:layout_height="30dp"
android:layout_centerInParent="true"
android:layout_marginTop="100dp"
android:background="@drawable/pink_button"
android:gravity="center"
android:text="PINK"
android:textColor="@android:color/white"
android:textSize="15sp"/>
</RelativeLayout>
And finally the activity, name it whatever you want I used ButtonDemoActivity
public class ButtonDemoActivity extends Activity {
private Button btnDemo;
private boolean isPink = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.button_demo_activity);
btnDemo = (Button) findViewById(R.id.btnDemo);
btnDemo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
isPink = !isPink;
int resId = isPink ? R.drawable.pink_button : R.drawable.blue_button;
btnDemo.setBackgroundResource(resId);
btnDemo.setText(isPink ? "PINK" : "BLUE");
}
});
}
}
And this is what the final look of the buttons will be in each state
Change Button Color When Clicked Android Studio
You can use a MaterialButtonToggleGroup
to select from a group of choices:
<com.google.android.material.button.MaterialButtonToggleGroup
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:singleSelection="true"
app:selectionRequired="true">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1"
style="?attr/materialButtonOutlinedStyle"
/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2"
style="?attr/materialButtonOutlinedStyle"
/>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 3"
style="?attr/materialButtonOutlinedStyle"
/>
</com.google.android.material.button.MaterialButtonToggleGroup>
Related Topics
How to Check If My Edittext Fields Are Empty
Determine If Android App Is Being Used For the First Time
How to Set Ringtone in Android from My Activity
Push Notification Works Incorrectly When App Is on Background or Not Running
How to Use Asynctask Correctly in Android
How to Sign Android App With System Signature
Android Studio Doesn't See Device
Offline Speech Recognition in Android (Jellybean)
Android - Sms Broadcast Receiver
Error:(1, 0) Plugin With Id 'Com.Android.Application' Not Found
Android and Facebook Share Intent
How to Add a Button Dynamically in Android
Viewpager.Setoffscreenpagelimit(0) Doesn't Work as Expected
Can Not Find a View With Findviewbyid()
Retrieve Database or Any Other File from the Internal Storage Using Run-As
How to Control the Width and Height of the Default Alert Dialog in Android