1

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() );
        }
    }
}
Ravi
  • 34,851
  • 21
  • 122
  • 183
AllMac
  • 31
  • 10

1 Answers1

1

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: https://stackoverflow.com/a/11576546/534471

Community
  • 1
  • 1
Emanuel Moecklin
  • 28,488
  • 11
  • 69
  • 85
  • Thanx! But this way I change only a single activity Theme, not entire App, right? I need to change Theme for all Activities. – AllMac Jan 06 '14 at 18:19
  • protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setTheme(R.style.JUCEES); setContentView(R.layout.activity_main); – AllMac Jan 06 '14 at 18:25
  • Yes that changes the theme for one Activity. If you pick the theme in code then you'll have to set it in each Activity. You can implement your own Activity class (Like MyActivity) that extents Activity and sets the theme in the onCreate(). Each of your Activity classes will extend MyActivity and call through to super.onCreate before calling setContentView. – Emanuel Moecklin Jan 06 '14 at 18:41
  • I did update my Answer. I am not able to change theme for every Activity, except Main_Activity. – AllMac Jan 06 '14 at 19:31
  • As I mentioned you need to restart an Activity before it picks up the theme change. You're not doing this for MainActivity so it won't work. Add the lines of code I posted above and you'll be good. If other Activities are running they need to restart as well. – Emanuel Moecklin Jan 06 '14 at 19:53
  • When I use finish (); it quits the App. I did as in Update 2. And added info about how I return info from my Config.java file, when theme is selected. – AllMac Jan 07 '14 at 02:10
  • Solved with different order. :p finish(); startActivity( getIntent() ); – AllMac Jan 07 '14 at 03:03