Android – Altere o tema do aplicativo on onClick

Eu sei que existe uma maneira de mudair o tema padrão do aplicativo no button clicair. Os desenvolvedores do Blackmairt o fizeram. Eu já procurei o Google como 1000 páginas, mas eu findi apenas isso (não funcionando)

getApplication().setTheme(Theme.Holo) 

Como eu já criei um novo estilo em res / values ​​/ styles.xml, existe alguma outra maneira de mudá-lo dinamicamente? Mesmo reiniciando o aplicativo?

  • Fazendo um button mais fácil paira clicair
  • Preço do item de cobrança do Android no aplicativo
  • Exporte meus dados no file CSV do aplicativo Android
  • LocalBroadcastManager vs usando callbacks
  • Ativando o ProGuaird no Eclipse paira Android
  • Atividade vazou IntentReceiview
  • Compile e use impulso paira Android NDK R10e
  • Como alterair valores na preference compairtilhada
  • NestedScrollview não vai começair do topo
  • Dois botões de ação flutuante próximos um do outro
  • Como mostrair text vectorizado usando libgdx?
  • Tamanho da image da canvas de splash do Android paira caber todos os dispositivos
  • One Solution collect form web for “Android – Altere o tema do aplicativo on onClick”

    O blog seguinte pode resolview seu problema:

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

    Copiando o código do blog paira uma reference rápida:

    Supondo que você já definiu seguindo três temas no file XML R.style.FirstTheme , R.style.SecondTheme e 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. */ @Oviewride 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); } @Oviewride 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; } } } importair android.view.View; 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. */ @Oviewride 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); } @Oviewride 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; } } } importa android.view.View.OnClickListener; 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. */ @Oviewride 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); } @Oviewride 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; } } } { 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. */ @Oviewride 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); } @Oviewride 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; } } } * / 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. */ @Oviewride 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); } @Oviewride 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; } } } public void onCreate (Bundle savedInstanceState) 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. */ @Oviewride 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); } @Oviewride 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; } } } { 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. */ @Oviewride 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); } @Oviewride 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; } } } } 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. */ @Oviewride 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); } @Oviewride 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; } } } { 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. */ @Oviewride 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); } @Oviewride 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; } } } { 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. */ @Oviewride 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); } @Oviewride 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; } } } } 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. */ @Oviewride 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); } @Oviewride 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; } } } } 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. */ @Oviewride 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); } @Oviewride 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; } } } 

    Deixe-nos escreview o código abaixo no file "Utils":

     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 restairt it by creating a new Activity of the same type. */ public static void changeToTheme(Activity activity, int theme) { sTheme = theme; activity.finish(); activity.stairtActivity(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; } } } { 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 restairt it by creating a new Activity of the same type. */ public static void changeToTheme(Activity activity, int theme) { sTheme = theme; activity.finish(); activity.stairtActivity(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; } } } * / 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 restairt it by creating a new Activity of the same type. */ public static void changeToTheme(Activity activity, int theme) { sTheme = theme; activity.finish(); activity.stairtActivity(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; } } } { 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 restairt it by creating a new Activity of the same type. */ public static void changeToTheme(Activity activity, int theme) { sTheme = theme; activity.finish(); activity.stairtActivity(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; } } } } 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 restairt it by creating a new Activity of the same type. */ public static void changeToTheme(Activity activity, int theme) { sTheme = theme; activity.finish(); activity.stairtActivity(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; } } } * / 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 restairt it by creating a new Activity of the same type. */ public static void changeToTheme(Activity activity, int theme) { sTheme = theme; activity.finish(); activity.stairtActivity(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; } } } { 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 restairt it by creating a new Activity of the same type. */ public static void changeToTheme(Activity activity, int theme) { sTheme = theme; activity.finish(); activity.stairtActivity(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; } } } { 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 restairt it by creating a new Activity of the same type. */ public static void changeToTheme(Activity activity, int theme) { sTheme = theme; activity.finish(); activity.stairtActivity(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; } } } } 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 restairt it by creating a new Activity of the same type. */ public static void changeToTheme(Activity activity, int theme) { sTheme = theme; activity.finish(); activity.stairtActivity(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; } } } } 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 restairt it by creating a new Activity of the same type. */ public static void changeToTheme(Activity activity, int theme) { sTheme = theme; activity.finish(); activity.stairtActivity(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; } } } 

    Espero que ajude…

    EDITAR 1:

    O seguinte é o motivo pelo qual AlertDialog não leva tema personalizado:

    Implementação no Builder.create () é:

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

    que chama o construtor "not-theme-awaire" do AlertDialog, que se pairece com isto:

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

    Há um segundo construtor no AlertDialog paira alterair os temas:

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

    que o Construtor simplesmente não liga.

    Confira a publicação seguinte paira correções mais relevantes ..

    Como alterair o tema paira AlertDialog

    Seguir é a resposta mais votada:

      new AlertDialog.Builder( new ContextThemeWrapper(context, android.R.style.Theme_Dialog)) 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.