Change Activity's Theme Programmatically

Change Activity's theme programmatically

As docs say you have to call setTheme before any view output. It seems that super.onCreate() takes part in view processing.

So, to switch between themes dynamically you simply need to call setTheme before super.onCreate like this:

public void onCreate(Bundle savedInstanceState) {
setTheme(android.R.style.Theme);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}

Change launch activity style programmatically

After some research I've discovered that this is not possible. Launch screens are used when we want to show something while our main functionality is loading, so we include the drawable we want to show in the Manifest to have this appearing fast while our main activity loads. This launch screen, as it is in the Manifest, appears before anything else, so if we could change the theme of the launch screen dinamically, we would lose that fast appearing while everything else loads.

How to change current Theme at runtime in Android

I would like to see the method too, where you set once for all your activities. But as far I know you have to set in each activity before showing any views.

For reference check this:

http://www.anddev.org/applying_a_theme_to_your_application-t817.html

Edit (copied from that forum):

    protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Call setTheme before creation of any(!) View.
setTheme(android.R.style.Theme_Dark);

// ...
setContentView(R.layout.main);
}



Edit

If you call setTheme after super.onCreate(savedInstanceState); your activity recreated but if you call setTheme before super.onCreate(savedInstanceState); your theme will set and activity
does not recreate anymore

  protected void onCreate(Bundle savedInstanceState) {
setTheme(android.R.style.Theme_Dark);
super.onCreate(savedInstanceState);


// ...
setContentView(R.layout.main);
}

Android:How to programmatically set an Activity's theme to Theme.Dialog

Would like to give a work around for this problem.

Problem : How to use the same activity as both dialog and full screen based.

Solution :

  1. Define your activity in your AndroidManifest.xml with the theme @android:style/Theme.Dialog
  2. In your respective .Java file, check for an intent extra that defines dialog mode.
  3. If it does not exist, set the Theme to android.R.style.Theme. This is the default theme which is applied if you do not define any theme.

Code :

boolean fDialogMode = getIntent().hasExtra("dialog_mode");

if( ! fDialogMode ) {
super.setTheme(android.R.style.Theme);
}

Alternate Solution:

A more complex solution is to use AlertDialog as below:

  1. Define a ListAdapter class extended from ArrayAdapter.
  2. return 1 in getCount function

    @Override
    public int getCount() { return 1; }
  3. In the getView function, inflate the layout of the activity you need and do any customization before returning the view.

    @Override
    public View getView( int position, View view, ViewGroup group ) {
    View v = view;
    if( v == null ) {
    v = getSystemService(Context.LAYOUT_INFLATER_SERVICE).inflate( <layout res id>, null );
    }

    ... Do any customization here ....

    return v;
    }

This is definitely a second choice option by if you are not doing too much processing in the activity class this could be an option.

Only reason to consider this solution could be that the logic to show it in a dialog is isolated to the places where it is used as a dialog.

Both the options worked for me but for obvious reasons I am taking the first option. :-)

Change Activity theme programmatically dosen't work

I found way to do that. In onCreate i put this

setTheme(Designs.getThemeNoActionBar(this));
setContentView(R.layout.activity_main);

get the theme from shared preferences and return theme without actionbar.

  public static int getThemeNoActionBar(Context context) {
SharedPreferences sharedPreferences = context.getSharedPreferences("Designs", Context.MODE_PRIVATE);
int theme = sharedPreferences.getInt("theme", R.style.AppTheme);
if (theme == nightModeTheme)
return R.style.NightMode_NoActionBar;
else if (theme == theme1)
return R.style.Theme1_NoActionBar;
else
return R.style.AppTheme_NoActionBar;
}

In style res

<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>

<style name="NightMode.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>

<style name="Theme1.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>

Switching application-wide theme programmatically?

Create a base activity for your app and override onCreate to set the theme. Derive all your other activities from this base activity.

Also check this tutorial:

http://www.androidengineer.com/2010/06/using-themes-in-android-applications.html



Related Topics



Leave a reply



Submit