简体   繁体   中英

How change the background of all activities?

I will also try this :

public Button button;  
public View background;

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

    button = (Button)findViewById(R.id.color);
    background = findViewById(R.id.colorBackg);

    button.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view) {
            int colorCreat = Color.rgb(255, 255, 255);
            background.setBackgroundColor(colorCreat);
        }
    });

}

It works but when I change the activity the color resets. How I can change the color of all background permanently ? Thanks.

Create a style under styles.xml in res directory of your project

<resources xmlns:tools="http://schemas.android.com/tools">

   <!-- Base application theme. -->
    <style name="app_theme" parent="Theme.AppCompat.NoActionBar">
        <item name="android:windowBackground">@android:color/black</item>
    </style>

</resources>

The above theme can then be applied to you application element in manifest like this

<application
 android:theme="@style/Theme.Activity.Default"/>

So this implies all the activity will inherit the above style declared in Application tag, unless any individual Activity overrides it specifying some other style.

Hope it helps.

As already answered by Mayank Bhatnagar , you should create an style inside the styles.xml file. See the guide for detailed implementation.


If you want to be able to dynamically change the theme, I recommend you to use a Settings to define which them to use. And then in your activities to listen to preference change.

First create a Listener to preference change, that will react to style change.

public class StylePreferenceChangeListener implements OnSharedPreferenceChangeListener {

    //String constant used as ID to the style preference (used here and in SettingActivity)
    public static String PREFERENCE_STYLE_ID = "preference.style";

    //The activity that we want to modify style
    private Activity activity;

    public StylePreferenceChangeListener(Activity activity) {
        super();
        this.activity = activity;
    }

    // listener implementation
    public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
        if(PREFERENCE_STYLE_ID.equals(key)) {
            int newStyleId = prefs.getInt(key, R.style.default_style);
            activity.getTheme.applyStyle(newStyleId, true);
    }

}

And in your activity, create an instance of this Listener. And register/unregister it inside onResume()/onPause().

public class myActivity extends Activity {

    //Our listener reference
    StylePreferenceChangeListener myListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate();
        setContentView(R.layout.myactivity);
        //create instance now so we are sure the activity exist
        myListener = new StylePreferenceChangeListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        //register our listener
        getSharedPreferences().registerOnSharedPreferenceChangeListener(myListener);
    }

    protected void onPause() {
        super.onPause();
        //unregister our listener
        getSharedPreferences().unregisterOnSharedPreferenceChangeListener(myListener);
    }

    protected void onDestroy() {
        super.onDestroy();
        //The activity is meant to be destroy, makes no sense to keep our listener
        //Drop his reference to allow garbage collecting
        myListener = null;
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM