AllMac AllMac - 4 months ago 14
Android Question

Change Theme not working

I am trying to change my theme choosing an

BaseExpandableListAdapter
option. I am able to select options, and it appears to be running OK. I have good results in Log. However themes do not change. Could anybody help? What could I be missing?

Here is part of my
MainActivity
:

public class MainActivity extends Activity {

static final int THEME_REQUEST = 1;

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == THEME_REQUEST) {
if (resultCode == RESULT_OK) {
Bundle bundle = data.getExtras();
System.out.println("Troca tema: " + bundle.getInt("theme"));
alteraTema(bundle.getInt("theme"));
}
}
}

public void alteraTema(int sTheme) {
switch (sTheme)
{
default:
case 0:
System.out.println("Tema BA");
this.setTheme(R.style.JUCEB);
this.recreate();
break;
case 1:
System.out.println("Tema ES");
this.setTheme(R.style.JUCEES);
this.recreate();
break;
}
}

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

if (Configuracao.urlApi == null){
Intent intent = new Intent(MainActivity.this, Configuracao.class);
startActivityForResult(intent, THEME_REQUEST);
}


Here is my styles.xml:

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

<style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light"></style>

<!-- Application theme. -->
<style name="Inicial" parent="AppBaseTheme">
<item name="android:actionBarStyle">@style/Inicial.ActionBar</item>
</style>

<style name="Inicial.ActionBar" parent="@android:style/Widget.Holo.ActionBar">
<item name="android:background">@drawable/logo_generico</item>
<item name="android:icon">@android:color/transparent</item>
<item name="android:titleTextStyle">@style/NoTitleText</item>
<item name="android:subtitleTextStyle">@style/NoTitleText</item>
</style>

<style name="JUCEES" parent="AppBaseTheme">
<item name="android:actionBarStyle">@style/JUCEES.ActionBar</item>
</style>

<style name="JUCEES.ActionBar" parent="@android:style/Widget.Holo.ActionBar">
<item name="android:background">@drawable/logo_es</item>
<item name="android:icon">@android:color/transparent</item>
<item name="android:titleTextStyle">@style/NoTitleText</item>
<item name="android:subtitleTextStyle">@style/NoTitleText</item>
</style>

<style name="JUCEB" parent="AppBaseTheme">
<item name="android:actionBarStyle">@style/JUCEB.ActionBar</item>
</style>

<style name="JUCEB.ActionBar" parent="@style/Widget.AppCompat.ActionBar">
<item name="android:background">@drawable/logo_ba</item>
<item name="android:icon">@android:color/transparent</item>
<item name="android:titleTextStyle">@style/NoTitleText</item>
<item name="android:subtitleTextStyle">@style/NoTitleText</item>
</style>

<style name="NoTitleText">
<item name="android:textSize">0sp</item>
<item name="android:textColor">#00000000</item>
</style>




Update 1

I did these changes:

public void alteraTema(int sTheme) {
switch (sTheme)
{
case 0:
temaEscolha = (R.style.JUCEB);
setTheme(temaEscolha);
System.out.println("Tema BA");
break;
case 1:
temaEscolha = (R.style.JUCEES);
setTheme(temaEscolha);
System.out.println("Tema ES");
break;
}


And

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


Now I am able to change Theme for every Activity, except Main_Activity.
And it works only for the first time I change the theme. When I try change again, don't work.

Update 2

I did this:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == THEME_REQUEST) {
if (resultCode == RESULT_OK) {
Bundle bundle = data.getExtras();
System.out.println("Troca tema: " + bundle.getInt("theme"));
alteraTema(bundle.getInt("theme"));
startActivity(getIntent());
finish();
}
}
}


And in my Confi.java, where is my selection, I have this:

private void finishWithResult() {
Intent intent = new Intent();
intent.putExtra("theme", Selecao);
setResult(Activity.RESULT_OK, intent);
finish();
}


Solved

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == THEME_REQUEST) {
if (resultCode == RESULT_OK) {
Bundle bundle = data.getExtras();
System.out.println("Troca tema: " + bundle.getInt("theme"));
alteraTema(bundle.getInt("theme"));
finish();
startActivity( getIntent() );
}
}
}

Answer

A theme has to be set before you call setContentView() (see documentation). Once it's set you can't alter the theme but by restarting the Activity. So in your onActivityResult method you need something along these lines:

    startActivity( getIntent() );
    finish();

Of course you have to remember the selected theme somehow (SharedPreferences or put it in the Intent).

See also this post: http://stackoverflow.com/a/11576546/534471