Domanda

Ok, the question is. In my Android app I have two separate activities for options and for the main activity. There is the place in the main activity, when it checks for changes in options and applies the styles. It looks like this:

if (prefs.getBoolean("opt_changed", true)) {
        Theme = prefs.getInt("theme", Theme);
        Font = prefs.getInt("font", Font);
        Size = prefs.getInt("size", Size);

        SetApplicableStyle(this, Theme, Font, Size);

        /** Setting opt_changed to false. */
        SharedPreferences.Editor editor = prefs.edit();
        editor.putBoolean("opt_changed", false);
        editor.commit(); // apply changes
    }

SetApplicableStyle method, called here, looks this way:

public void SetApplicableStyle (DTypeActivity dTypeActivity, int Theme, int Font, int Size) {
    // Retrieving the EditText and the View as objects
    final EditText edit_text = (EditText) findViewById(R.id.editText1);
    final View main_view = (View) findViewById(R.id.mainview);

    // Setting the theme
    switch(Theme){
    case 1:
        SetThemeLight (this);
    break;
    case 2:
        SetThemeBlue (this);        
    break;
    case 3:
        SetThemeDark (this);
    break;
    }

    // Setting the font
    switch(Font){
    case 1:
        SetFontSans (this);
    break;
    case 2:
        SetFontSerif (this);        
    break;
    case 3:
        SetFontMono (this);
    break;
    }

    // Setting the size
    switch(Size){
    case 1:
        SetSizeSm (this);
    break;
    case 2:
        SetSizeMd (this);       
    break;
    case 3:
        SetSizeBg (this);
    break;
    }
}

And as the example of the Set[Something][Somewhat] methods, there is the SetThemeLight one:

public void SetThemeLight (DTypeActivity dTypeActivity) {
        final EditText edit_text = (EditText) findViewById(R.id.editText1);
        final View main_view = (View) findViewById(R.id.mainview);  
        main_view.setBackgroundDrawable(getResources().getDrawable(R.drawable.grey_background));
        edit_text.getBackground().setAlpha(0);
        edit_text.setTextColor(getResources().getColor(R.color.DrText));

}

My question concerns the amount of methods, that are used in this simple app. I've been thinking on reducing the amount of code and implemented the SetApplicableStyle method. Now I'm thinking whether it would be alright to get rid of Set[Something][Somewhat] and put the lines from them straight to the cases of SetApplicableStyle switches. My main concern is the amount of methods, but I know, that huge methods are also a bad practice. What would be the better solution here?

Full source code is available here.

È stato utile?

Soluzione

I assume you duplicate most of the code in the SetThemeX methods. I would thus recommend introducing a class that captures the essence of a theme and use that:

class MyTheme {
    public int background;
    public int alpha;
    public int color;
    public MyTheme(int background, int alpha, int color) {
        this.background = background;
        this.alpha = alpha;
        this.color = color;
    }
}

Make one method to set your theme:

public void setTheme(DTypeActivity dTypeActivity, MyTheme theme) {
    final EditText edit_text = (EditText) findViewById(R.id.editText1);
    final View main_view = (View) findViewById(R.id.mainview);
    main_view.setBackgroundDrawable(getResources().getDrawable(theme.background));
    edit_text.getBackground().setAlpha(theme.alpha);
    edit_text.setTextColor(getResources().getColor(theme.color));
}

And keep a map somewhere in which you store these themes:

Map<Integer, MyTheme> themes = new HashMap<>();
themes.put(1, new MyTheme(R.drawable.grey_background, 0, R.color.DrText));
// put other themes

In your SetApplicableStyle method you can then simply use

public void SetApplicableStyle (DTypeActivity dTypeActivity, int theme, int font, int size) {
    setTheme(dTypeActivity, themes.get(theme);
    // set font and size similarly
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top