How to Change Background Color in Android App

How to change background color in android app

You need to use the android:background property , eg

android:background="@color/white"

Also you need to add a value for white in the strings.xml

<color name="white">#FFFFFF</color>

Edit : 18th Nov 2012

The first two letters of an 8 letter color code provide the alpha value, if you are using the html 6 letter color notation the color is opaque.

Eg :

enter image description here

How to change background color of app permanently

You can use Android Shared Preferences to remember the selected background color for the app.So every time you open the app you can check the value of the shared preference and apply the color accordingly.

Use a common base activity class that all the other activities will derive and in the "OnCreate" and "OnResume" methods write the code to read shared preference value and apply back ground color.This way when you open any activity selected background color will be applied.

Try below code, it is tested and working.

BaseActivity Class

 public class BaseActivity extends ActionBarActivity {

private static final String PREFS_NAME="color_settings";
SharedPreferences prefsReader = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
prefsReader=getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
}

@Override
public void setContentView(int layoutResID) {
super.setContentView(layoutResID);
setBackgroundColor();
}

protected void setBackgroundColor()
{
int background_resource_id= prefsReader.getInt("background_resource_id",0);
View bgView= findViewById(R.id.main_container);
bgView.setBackgroundColor(getResources().getColor(background_resource_id));
}
protected void setCurrentBackgroundColor(int colorResourceId)
{
SharedPreferences.Editor editor=getSharedPreferences(PREFS_NAME, MODE_PRIVATE).edit();
editor.putInt("background_resource_id", colorResourceId);
editor.commit();
}
}

Activity Class

public class MainActivity extends BaseActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

//save the color resource value in shared pref
setCurrentBackgroundColor(R.color.red);

setContentView(R.layout.activity_main);

}

}

Colors.xml color list

<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="blue" type="color">#FF33B5E5</item>
<item name="purple" type="color">#FFAA66CC</item>
<item name="green" type="color">#FF99CC00</item>
<item name="orange" type="color">#FFFFBB33</item>
<item name="red" type="color">#FFFF4444</item>
<item name="darkblue" type="color">#FF0099CC</item>
<item name="darkpurple" type="color">#FF9933CC</item>
<item name="darkgreen" type="color">#FF669900</item>
<item name="darkorange" type="color">#FFFF8800</item>
<item name="darkred" type="color">#FFCC0000</item>
</resources>

how to change background color by changing the theme in android?

Sample layout

 <FrameLayout
style="@style/colorAccentStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content/>

styles.xml

<resources>

<style name="colorAccentStyle">
<item name="android:background">?colorAccent</item>
</style>

Simpler solution

<FrameLayout
android:background="?colorAccent"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

Android Studio: Unable to change background color and text color based on boolean

You need an array of booleans to keep track of the state for each item in the listview

Try changing

if(urgent == true) {
newView.setBackgroundColor(Color.RED);
textView.setTextColor(Color.WHITE);
}

To

if(urgent[position]) {
newView.setBackgroundColor(Color.RED);
textView.setTextColor(Color.WHITE);
} else {
//Put here the default colors
newView.setBackgroundColor( );
textView.setTextColor( );
}

this should fix the issue, also you don't need to do if(boolean == true) if(boolean) is enough

Android change Dialog background color from app settings via styles

I have read that on a newer devices the attributes with android: prefix should be used or to use AppCompat theme as a parent to get rid of confusing with this prefix.

How to change background color of MaterialButton through styles.xml

By Material Official Source you should use app:backgroundTint in your style

meanwhile for checking "Widget.MaterialComponents.Button.OutlinedButton" styles you should check buttons on Device or Emulator, sometimes buttons with this style dosent show good in IDE

Can't change activity background colour to default

Try to create a new theme and change the background at theme level.

Note: this an example with a material theme, but it may not differ a lot from the support version.

<!-- themes.xlm -->    
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.MyApplication" parent="Theme.MaterialComponents.NoActionBar">

<!-- ... other theme attributes -->
<!-- ... changing theme window background. -->
<item name="android:windowBackground">@color/your_color</item>
<item name="android:colorBackground">@color/your_color</item>
</style>
<!-- ... -->
</resources>

In your activity

<application
...
android:theme="@style/Theme.MyApplication">
<activity
...
android:theme="@style/Theme.MyApplication">
...
</activity>
</application>


Related Topics



Leave a reply



Submit