Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7043363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:16:48+00:00 2026-05-28T02:16:48+00:00

I want to give the user the choice between a few different themes, and

  • 0

I want to give the user the choice between a few different themes, and was wondering if this is an alright way of doing things. I did a little test with this method and it worked, but I think there may be better ways and think it may cause some problems later on, so wanted to ask.

I was thinking of creating a different layout for each theme, and in onCreate just having a switch for the setContentView() method. I’d load a saved SharedPreference value (integer) first and depending on what that value was display the corresponding layout. Obviously the user could change the SharedPreference value with a button or something.

As these layouts would be basically the same but with different colours, I’d want to use the same IDs for my TextViews and other Views in each layout file. My main question is would this cause problems?

Sorry for the wall of text with no code. I’d just like to get a general idea of good practice for this situation. Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T02:16:49+00:00Added an answer on May 28, 2026 at 2:16 am

    I actually have this feature in my application and additionally, I allow users to change theme at runtime. As reading a value from preferences takes some time, I’m getting a theme id via globally accessible function which holds cached value.

    As already pointed out – create some Android themes, using this guide. You will have at least two <style> items in your styles.xml file. For example:

    <style name="Theme.App.Light" parent="@style/Theme.Light">...</style>
    <style name="Theme.App.Dark" parent="@style/Theme">...</style>
    

    Now, you have to apply one of these styles to your activities. I’m doing this in activitie’s onCreate method, before any other call:

    setTheme(MyApplication.getThemeId());
    

    getThemeId is a method which returns cached theme ID:

    public static int getThemeId()
    {
        return themeId;
    }
    

    This field is being updated by another method:

    public static void reloadTheme()
    {
        themeSetting = PreferenceManager.getDefaultSharedPreferences(context).getString("defaultTheme", "0");
        if(themeSetting.equals("0"))
            themeId = R.style.Theme_Light;
        else
            themeId = R.style.Theme_Dark;
    }
    

    Which is being called whenever preferences are changed (and, on startup of course). These two methods reside in MyApplication class, which extends Application. The preference change listener is described at the end of this post and resides in main activity class.

    The last and pretty important thing – theme is applied, when an activity starts. Assuming, you can change a theme only in preference screen and that there’s only one way of getting there, i.e. from only one (main) activity, this activity won’t be restarted when you will exit preference screen – the old theme still will be used. Here’s the fix for that (restarts your main activity):

    @Override
    protected void onResume() {
        super.onResume();
        if(schduledRestart)
        {
            schduledRestart = false;
            Intent i = getBaseContext().getPackageManager().getLaunchIntentForPackage( getBaseContext().getPackageName() );
            i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(i);
        }
    }
    

    scheduledRestart is a boolean variable, initially set to false. It’s set to true when theme is changed by this listener, which also updates cached theme ID mentioned before:

    private class ThemeListener implements OnSharedPreferenceChangeListener{
    
        @Override
        public void onSharedPreferenceChanged(SharedPreferences spref, String key) {
            if(key.equals("defaultTheme") && !spref.getString(key, "0").equals(MyApplication.getThemeSetting()))
            {
                MyApplication.reloadTheme();
                schduledRestart = true;
            }
        }
    
    
    sp = PreferenceManager.getDefaultSharedPreferences(this);
    
    listener = new ThemeListener();
    sp.registerOnSharedPreferenceChangeListener(listener);
    

    Remember to hold a reference to the listener object, otherwise it will be garbage colleted (and will cease to work).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to give the user the choice of searching by different properties. For
I am working on my app. I want to give user few buttons to
I want to give the user four choices, the first and second choices on
I want to give a user access to a stored procedure, but not to
In my application I want to give the user ways of selecting text from
I have a tree control and I want to give the user the ability
I am using a Lucene.Net index and want to give the user an option
Using Rails 3.1.3 and Ruby 1.9.3. I want to give the user a list
I have a service that performs background updates. I want to give the user
I want to give permission to a user to access my database using work

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.