Android - Change app Theme on onClick

76,845

Following blog can solve your problem:

http://mrbool.com/how-to-change-the-layout-theme-of-an-android-application/25837

Copying the blog code for quick reference:

Assuming that you already defined following three themes in the XML file R.style.FirstTheme, R.style.SecondTheme and R.style.ThirdTheme

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class ChangeThemeActivity extends Activity implements OnClickListener
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        Utils.onActivityCreateSetTheme(this);
        setContentView(R.layout.main);

                    findViewById(R.id.button1).setOnClickListener(this);
          findViewById(R.id.button2).setOnClickListener(this);
          findViewById(R.id.button3).setOnClickListener(this);
    }
     @Override
     public void onClick(View v)
     {
          // TODO Auto-generated method stub
          switch (v.getId())
          {
          case R.id.button1:
          Utils.changeToTheme(this, Utils.THEME_DEFAULT);
          break;
          case R.id.button2:
          Utils.changeToTheme(this, Utils.THEME_WHITE);
          break;
          case R.id.button3:
          Utils.changeToTheme(this, Utils.THEME_BLUE);
          break;
          }
     }
}

Let us write the below code in the "Utils" file:

import android.app.Activity;
import android.content.Intent;
public class Utils
{
     private static int sTheme;
     public final static int THEME_DEFAULT = 0;
     public final static int THEME_WHITE = 1;
     public final static int THEME_BLUE = 2;
     /**
      * Set the theme of the Activity, and restart it by creating a new Activity of the same type.
      */
     public static void changeToTheme(Activity activity, int theme)
     {
          sTheme = theme;
          activity.finish();
activity.startActivity(new Intent(activity, activity.getClass()));
     }
     /** Set the theme of the activity, according to the configuration. */
     public static void onActivityCreateSetTheme(Activity activity)
     {
          switch (sTheme)
          {
          default:
          case THEME_DEFAULT:
              activity.setTheme(R.style.FirstTheme);
              break;
          case THEME_WHITE:
              activity.setTheme(R.style.SecondTheme);
              break;
          case THEME_BLUE:
              activity.setTheme(R.style.Thirdheme);
              break;
          }
     }
}

Hope it helps...

EDIT 1:

following is the reason AlertDialog does not take custom theme:

Implementation in Builder.create() is:

public AlertDialog create() {
    final AlertDialog dialog = new AlertDialog(P.mContext);
    P.apply(dialog.mAlert);
    [...]
}

which calls the "not-theme-aware" constructor of AlertDialog, which looks like this:

protected AlertDialog(Context context) {
    this(context, com.android.internal.R.style.Theme_Dialog_Alert);
}

There is a second constructor in AlertDialog for changing themes:

protected AlertDialog(Context context, int theme) {
    super(context, theme);
    [...]
}

that the Builder just doesn't call.

Check out following post for more relevant fixes..

How to change theme for AlertDialog

Following is the most voted answer:

  new AlertDialog.Builder(
  new ContextThemeWrapper(context, android.R.style.Theme_Dialog))
Share:
76,845
user2606414
Author by

user2606414

Android Developer, need some help with easy stuff? contact!

Updated on August 20, 2020

Comments

  • user2606414
    user2606414 over 3 years

    i know there is a way to change the app default theme on button click. Blackmart developers have done it. I’ve already searched Google like 1000 pages but i found just this (not working)

    getApplication().setTheme(Theme.Holo)
    

    As i already created a new style in res/values/styles.xml is there any other way to dynamically change it? Even rebooting the application?

  • user2606414
    user2606414 over 10 years
    Yeah, saw this morning… not working either because of List View…
  • Praful Bhatnagar
    Praful Bhatnagar over 10 years
    what is the problem with the ListView..? are you able to change theme for all other view (except ListView) using this method?
  • Praful Bhatnagar
    Praful Bhatnagar over 10 years
    are you using ListActivity?
  • user2606414
    user2606414 over 10 years
    Yes for some other reasons… Oh, i noticed that it doesn’t change in a custom DialogAlert view… :(
  • Praful Bhatnagar
    Praful Bhatnagar over 10 years
    check out the most voted answer in following post..
  • Praful Bhatnagar
    Praful Bhatnagar over 10 years
    check out my edit 1 for AlertDialog issue..
  • user2606414
    user2606414 over 10 years
    just solved AlertDialog issue, now the problem is the ListActivity… I think i’ll take Pavlos’ suggestion…Thank you for your hard work Praful… You can take a look at my app and if I’ve done it at play.google.com/store/apps/… Thank you!!!
  • Yaroslav Mytkalyk
    Yaroslav Mytkalyk over 10 years
    Very good answer. Should have much more votes.
  • Karue Benson Karue
    Karue Benson Karue almost 8 years
    Just to add something from above answer. Please note that Activity is deprecated and android is now using AppCompatActivity. So replace activity with AppCompatActivity. e.g. changeToTheme(Activity activity, int theme) change to changeToTheme(AppCompatActivity activity, int theme) Then you can say: activity.recreate()