Android: Changing Background-Color of the Activity (Main View)

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 :

Sample Image

How to change the background color of a view from a different activity in android?

As your activity Sequence is MainActivity -> Categories -> Sets -> Scores.

You've two options to change the color with two different life cycle of the change.

  1. To change the color on a temporary basis, this will reset itself after closing the app or resrtating the 'Sets' activity. It can be done in two ways: Using Public Static Variable and using a public function.

  2. To change the color on a permanent basis until the app is uninstalled/reinstalled. You should use SharedPreferences. SharedPreferences acts like a private data stored in device's memory for further use and it stays there unchanged until and unless the app is removed/data is cleared. Although, apps with root permission can access any app's SharedPreferences data and can modify it as well.
    You can use SharedPreferences as explained here. Or, you can use some library to access it an easy way. The way I use it in all my apps is TinyDB(it's just a java/kotlin file). This works as:

    //store the value from ScoreActivity after completion as

    TinyDB tinyDB = TinyDB(this);
    tinyDB.putBoolean("isSet1Completed",true);

    //access the boolean variable in SetsActivity to change the color of any set that
    //is completed and if it's true, just change the color.

    TinyDB tinyDB = TinyDB(this);
    Boolean bool1 = tinyDB.getBoolean("isSet1Completed");

But, it's your choice what way you want to prefer.
Now, this was about the lifecycle of the change you'll do: Temp or Permanent. Now, we'll talk about how you change the color.

  • Using public static variable in Sets activity. What you can do is you can set the imageView/textview whose background you want to change as public static variable. Remember, this idea is not preferred as it causes memory leak but it's just easy.

    Declare it as public static ImageView imageview;(or TextView) intialize it in the
    onCreated() as imageView = finViewById(R.id.viewId); in Sets activity.
    Call
    it as new SetsActivity().imageView.setBackgroundColor(yourColor); in ScoreActivity.

  • Second way is to create a public function in SetsAcitvity, putting the color change code in it, and then calling it from the ScoreActivity. Just declare it as public void changeColor(){ //your work} and call it from ScoreActivity as new SetsActivity().changeCOlor(). You can also pass some arguments to the function like setId.

I've provided you every thing you need. Rest you should figure out yourself to actually learn it and not copy it.

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);
}
});
}

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.

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));

Trying to change the background color in one activity from code in another activity (Kotlin/Android Studio)

What you probably want to look into is startActivityForResult. You can pass an int, and your Activity can be notified when it returns the result.

// Define some constants, the request code and the place we're passing the result.
companion object {
private const val COLOR_REQUEST= 10001
const val EXTRA_COLOR = "EXTRA_COLOR"
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}

// Now, call startActivityForResult instead, passing the request code.

fun setColor_click (view: View) {
val intent = Intent (this, ChangeColor::class.java)
startActivityForResult(intent, COLOR_REQUEST)
}

fun changeColor_click (view: View) {
val cl: ConstraintLayout = findViewById(R.id.constraint_Layout)
cl.setBackgroundColor(color_Background)
}

// And now you can handle the result.

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if( requestCode == COLOR_RESULT && resultCode == Activity.RESULT_OK && data != null ) {
val color = data.extras.getInt(EXTRA_COLOR)
// TODO use the color.
}
super.onActivityResult(requestCode, resultCode, data)
}

Now, in your ChangeColor Activity, you want to make sure you return the colour when calling finish.

fun Red_clicked(view: View) {
val intent = Intent()
intent.putExtra(MainActivity.EXTRA_COLOR, color_Red)
setResult(Activity.RESULT_OK, intent)
finish()
return
}

And there you go. You'll want to change it and maybe pass something easier. But the id of the color should work for now.



Related Topics



Leave a reply



Submit