2014年3月12日 星期三

[ Android ] : enable settings page of your Aplication

You can easily add "Settings feature" to your android app, just following these 10 steps:
( Android 4.0 & 4.4 works fine )

1. You need create your android app project with eclipse first. (here sample project is "enableSettings")

2. right click your android project >> new >> others


3. select "Android Activity"

4. select "Settings Activity"

5. No need to change, next step.

6. This "settings feature" need dependency android lib, if you can't press "Next" button, just install it.

7. Now just start your app see if can run normally, UI should like this:

8. then add this code in your android MainActivity.java, after that you should be able to go to "SettingsActivity" page:

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
       
        switch (item.getItemId()) {
       
        case R.id.action_settings:
            
            Intent i = new Intent(this, SettingsActivity.class);
            startActivityForResult(i, 0);

            break;

        default:
            break;
        }
        return true;
    }

9. You can see the template settings from android now, and there are check box for boolean variable, editable dialog for string variable, list dialog for int & string ...etc, you can easily use what you need.



10. Next step you need to do a little study into the "SettingsActivity.java" file to know how to use it; and "SharedPreferences" class to access/store your app's preference.