How to set background color of an Activity to white programmatically?
Get a handle to the root layout used, then set the background color on that. The root layout is whatever you called setContentView with.
setContentView(R.layout.main);
// Now get a handle to any View contained
// within the main layout you are using
View someView = findViewById(R.id.randomViewInMainLayout);
// Find the root view
View root = someView.getRootView();
// Set the color
root.setBackgroundColor(getResources().getColor(android.R.color.red));
Changing background color in a activity.
In your styles.xml
set the android:windowBackground
attribute for whatever theme you are using for application or activity. You can find the theme you are using in your AndroidManifest.xml.
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<!-- Customize your theme here. -->
<item name="android:windowBackground">@color/somecolor</item>
</style>
</resources>
Note: If you have views in your layout that aren't transparent, it will block you from seeing the color you set here.
Android: Changing Background-Color of the Activity (Main View)
Try creating a method in your Activity
something like...
public void setActivityBackgroundColor(int color) {
View view = this.getWindow().getDecorView();
view.setBackgroundColor(color);
}
Then call it from your OnClickListener passing in whatever colour you want.
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 :
Android Studio, Change the color of the background by pressing a switch button
Ok, did find on an Indian video.
the short solution, add activity main XML the line of code with //JUST THIS
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rView" // JUST THIS
tools:context=".MainActivity">
and in the mainactivity.java
final Switch BackgroundColorButton = (Switch) findViewById(R.id.BackgroundColorButton);
BackgroundColorButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(BackgroundColorButton.isChecked())
{
BackgroundColorButton.setText("White Mode");
screenView.setBackgroundColor(Color.BLACK);//JUST THIS
}
else
{
BackgroundColorButton.setText("Dark Mode");
screenView.setBackgroundColor(Color.WHITE);//JUST THIS
}
}
});
Trying to change main activity background color on button click
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
Button btn = (Button) findViewById(R.id.newgame_button);
btn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
changeBackground(v);
}
});
}
Change this to
Activity activity;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
actvity = this;
Button btn = (Button) findViewById(R.id.newgame_button);
btn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
actvity.findViewById(android.R.id.content).setBackgroundColor(Color.BLACK);
}
});
}
Change background color of the screen
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.colourred) {
//toolBar.setBackgroundColor(Color.RED);
getWindow().getDecorView().setBackgroundColor(Color.RED);
getWindow().getDecorView().setBackgroundColor(Color.parseColor("#FFCC33"));
//or like below with color code
//toolBar.setBackgroundColor(Color.parseColor("#FFCC33"));
return true;
}else if(id==R.id.color_green){
getWindow().getDecorView().setBackgroundColor(Color.parseColor("#green_color_code"));
}else if(id==R.id.other_id){
getWindow().getDecorView().setBackgroundColor(Color.parseColor("#other_color_code"));
}
}
And if you wish to change activity background color then visit this How to set background color of Activity to white programmatically?
getWindow().getDecorView().setBackgroundColor(Color.WHITE);//change activity bg color
Related Topics
How to Set Seekbar Min and Max Value
How to Navigate from One Screen to Another Screen
Android - How to Replace Part of a String by Another String
How to Get Difference Between Two Dates in Android, Tried Every Thing and Post
Flutter Apps Are Too Big in Size
How to Change Package Name of an Android Application
When Is Onattach Called During the Fragment Lifecycle
Resize Image to Full Width and Fixed Height with Picasso
How to Resume an Interrupted Download - Part 2
How to Call Launch() More Than Once in Java
How Returns Xxxsize from Jcomponent(S) Added to the Jlabel
Jersey Rest Web Service with Activemq Middleware Integration