Mateus
Mateus

Reputation: 409

Change Resources (XML strings)

I need to build an application to three types of users: Beginner, Intermediate and Advanced. Each level has a specific XML messages (written according to the user level) and will be charged in terms of performance. If the user sets the level PreferencesActivity Advanced, XML load application to display messages from this user-level ...

How do I build it correctly?

onCreate () always invoke the XML level and pre-configured in Preferences Shared? I think this is a bad idea ...

Thank you! Mateus!

Upvotes: 0

Views: 151

Answers (1)

Reed
Reed

Reputation: 14974

On the first run, you could have the user choose whether they're Beginner, Intermediate, or Advance using 3 different buttons. Then in the onClick save to SharedPreferences which they chose, then in onCreate get what they chose and set accordingly. Here's what I mean:

Button beginnerButton = (Button)findViewById(R.id.beginnerButton);
beginnerButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(); 
                            SharedPreferences.Editor editor = pref.edit();
                          editor.putInt("level", 1);
        }
    }); 

You would do the same for the other two buttons, but make intermediate putInt(..., 2), and advanced putInt(..., 3)

Then in your PreferenceActivity in onCreate

SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences();
Int level = pref.getInt("level", 1);
if (level==1)
   addPreferencesFromResource(R.layout.beginner);
else if (level==2)
   addPreferencesFromResource(R.layout.intermediate);
else addPreferencesFromResource(R.layout.advanced);

I'd say that's probably the easiest way to handle it.

If you have to set click listeners and things, though, it may be better to use three different activities with the same premise. You would have a start screen that would get "level" from sharedprefs and then startActivity(...) based on what was returned.

Upvotes: 2

Related Questions