Change layout background color on button click
Shared preferences could be your solution. Oncreate of every activity check for the variable and set the color.
public void onClick(View v){
switch (v.getId()){
case R.id.blue:
backgroundColor("blue");
break;
}
}
private void backgroundColor(String color) {
// TODO Auto-generated method stub
SharedPreferences prefs = getSharedPreferences("BackgroundColor", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.clear();
editor.putString("Color", color);
editor.commit();
}
In other Activities
SharedPreferences prefs = getSharedPreferences("BackgroundColor",
MODE_PRIVATE);
String bgcolor = prefs.getString("Color","Anydefaultcolor");
now you can set your layout to bgcolor
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.
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"
.
Android button background color
If you want to keep the general styling (rounded corners etc.) and just change the background color then I use the backgroundTint property
android:backgroundTint="@android:color/holo_green_light"
How to change background of a button in android and retain it after few seconds
This works fine with me you need to make view final so you can access it inside handler body
buyNow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {
view.setBackgroundColor(Color.RED); //set the color to red
// Delay of 2 seconds (200 ms) before changing back the color to black
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
view.setBackgroundColor(Color.BLACK); //set the color to black
}
}, 200);
}
});
Related Topics
Pdf Not Open in Webview from Url
Retrofit 2 Json Response Json Array and Object
Images Are Not Visible in React-Native App Release Build
Android Studio - Textview Not Showing in Design View Layout
Android Textview Text Not Getting Wrapped
Android:Strike Out Text With Bold or Thicker Line Than Default Strike_Thru_Text_Flag
Alarm Manager Won't Work When the App Is Killed in the Background and Device Is Locked
Onclicklistener Does Not Work in Fragment
Import Android.Support.V7.App Cannot Be Resolved
How to Add/Remove Object in Recyclerview Using Arraylist in Android
Android:Get Current Date and Time from Firebase
How to Get the Current Time on a Button Click
How to Make Service That Is Unremovable Even If Killed by the System
How to Avoid Overlap View in Relative Layout in Android
Remove Old and Add New Git in Android Studio