Rothariger
Rothariger

Reputation: 293

Change theme depending on the activity?

i need to have the application with two or more styles (i have now just two, but in the future will be at least ten), i have the style part resolved. but the thing is the manifest, i need to launch the same activity, but with a different theme associated.

<activity
            android:name=".StartActivity"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:theme="@style/DefaultTheme"
            android:windowSoftInputMode="adjustPan">
            <intent-filter android:icon="@drawable/ic_launcher">
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".StartActivity"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name_mc"
            android:screenOrientation="portrait"
            android:theme="@style/MCTheme"
            android:windowSoftInputMode="adjustPan">
            <intent-filter android:icon="@drawable/ic_launcher">
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

well the thing is that i get a duplicated error. Ok, i get that i can't have the same activity twice (at least that is the exception i get).

is there a workaround for this? i do need the two launchers.

regards.

Upvotes: 1

Views: 140

Answers (2)

marcinj
marcinj

Reputation: 50036

One work around, if your activity is named StartActivity, then you can make StartActivityWithTheme1 extends StartActivity, StartActivityWithTheme2 extends StartActivity, etc. So you have all your logic in StartActivity.

Also if you want more than one launcher activity then read this SO: Two launcher activities

Upvotes: 1

rupesh jain
rupesh jain

Reputation: 3440

You can set the theme programatically.use this method-http://developer.android.com/reference/android/view/ContextThemeWrapper.html#setTheme%28int%29

To set the theme dynamically at runtime, call setTheme() in your activity's onCreate() method, before calling setContentView().

Upvotes: 0

Related Questions