1

I am trying to change the color of the background in my android app. At the moment when I press the button it changes the color for that specific activity and not for all the other activities. Is there a way to change it for the whole app?

screen

public class colorPicker extends AppCompatActivity {

    View view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_color_picker);

        view = this.getWindow().getDecorView();

    }

    public void goPink(View v){

        view.setBackgroundResource(R.color.Pink);
    }

    public void goGreen(View v){

        view.setBackgroundResource(R.color.Green);
    }

    public void goYellow(View v){
        view.setBackgroundResource(R.color.Yellow);

    }
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Dona M
  • 33
  • 6

3 Answers3

0

You could change the window background color of your app theme and don't use a background for activities or you can use a transparent background for activities.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
MarcGV
  • 1,242
  • 1
  • 10
  • 33
0

create a theme in your styles.xml and add following to that theme

<item name="android:windowBackground">@color/window_background

and set android:theme="@style/YourTheme" in your

<application>
...
</application

in manifest file

Krishna Meena
  • 5,693
  • 5
  • 32
  • 44
  • I believe this is a solution if I want to choose a specific color as a developer. I want the user to choose a color by pressing a button in the activity and then according to that, the color will change. I hope I am making it clear! – Dona M Mar 09 '17 at 19:14
  • Go through this http://stackoverflow.com/questions/33987678/programmatically-change-the-value-of-a-color-resource-obtained-from-api-response – Krishna Meena Mar 09 '17 at 19:59
0

In this case, You need to add few changes in your activity. In on onCreate

 if(getIntent().getExtras() != null)
    {
        int theme = getIntent().getIntExtra("theme", R.style.AppTheme);
        setTheme(theme);
        getApplication().setTheme(theme);
        //recreate();
    }

Condition onCLick

  if(condition)
   getIntent().putExtra("theme", R.style.AppTheme2);
   else
   getIntent().putExtra("theme", R.style.AppTheme);

and maintain 2 theme

 <style name="BaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
  <item name="colorPrimary"></item></style>

and the second theme similar to it just change the name as BaseTheme2.

But this is not suggested to change the app theme at runtime.

Ankit Aman
  • 999
  • 6
  • 15