Java – How to account for changes when switching topics?

How to account for changes when switching topics?… here is a solution to the problem.

How to account for changes when switching topics?

I’m working on an android app that needs to switch themes based on what the server provides themeCode . I’m using Save Theme Code and Apply it using sharePref setTheme(R.style.themeName); . It works fine until like

Such a basic theme property

colorPrimary
colorPrimaryDark
colorAccent
windowActionBar
windowNoTitle

To do this, I styles.xml created different styles in . But I have a limitation, some fields say EditText there are changes to EditText

  • first name
  • email
  • Phone
  • Passwords, etc

Similarly,TextView there are variations of the TextView

  • title
  • Single
  • Multi-line
  • links, etc

Before the multi-topic requirement, I had created separate topics for everyone

  • Apptheme.Edittext.email
  • Apptheme.Edittext.Password
  • Apptheme.Edittext.PersonName, etc
    and applied to specific views in the xml, such as

     style="@style/AppTheme.EditText.PersonName"  
    

Now I’ve read a lot of tutorials/posts but haven’t found a way to fix property changes. Please help apply these changes, for which I would be grateful.

Greeting:
Inzimam Tariq

Best Solution

In my opinion, changing the app theme at runtime, definitely requires reloading the activity; This creates problems at some point in most cases (if the project scales to medium size, with user controls such as toggles or switches, the app may easily crash if the user repeatedly clicks to switch)


I recommend using custom control classes (TextView, buttons, etc.); where this property is different from the current theme value from sharedPref.
This approach has a drawback; It will require manually changing all of the current screen’s Views and the views already rendered in memory, if any, and the rest will transition more smoothly than our traditional approach

Edit: Example of CustomTextView ##

This is an example of a custom TextView class

public class CustomTextView extends android.support.v7.widget.AppCompatTextView {
private static final String TAG = "TextView";
private Typeface tf = null;
private SharedPreferenceUtils preferenceUtils = SharedPreferenceUtils.getInstance();

/**
 * @param context:This is an abstract class whose implementation is provided by Android Operating System.
 * @param attrs:A      collection of attributes, as found associated with a tag in an XML document.
 * @param defStyle:
 */
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    if (preferenceUtils.getBooleanValue(PrefsKeyValue.bTheme)) {
        this.setTextColor(ResourceUtils.getColor(R.color.lightThemeTextColor));
    } else {
        this.setTextColor(ResourceUtils.getColor(R.color.colorWhite));
    }

    try {
        TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.CustomEditText, defStyle, 0);

        String str = a.getString(R.styleable.CustomTextView_FontEnum);
        int original = a.getInt(R.styleable.CustomEditText_FontEnum, 0);
        CustomEnum.CustomFontType customEnumValue = CustomEnum.CustomFontType.fromId(a.getInt(R.styleable.CustomEditText_FontEnum, 0));
        a.recycle();
        switch (customEnumValue) {
            case BOLD:
                setTypeface(HelveticaNeueBold.getInstance(context).getTypeFace());
                break;

            case LIGHT:
                setTypeface(HelveticaNeueMedium.getInstance(context).getTypeFace());
                break;

            case REGULAR:
                setTypeface(HelveticaNeue.getInstance(context).getTypeFace());
                break;

            default:
                break;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public CustomTextView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}


public boolean setCustomFont(Context ctx, String asset) {

    try {
        tf = Typeface.createFromAsset(ctx.getAssets(), asset);
    } catch (Exception e) {
        LogUtils.LogE(TAG, "Could not get typeface: " + e.getMessage());
        return false;
    }

    setTypeface(tf);
    return true;
}}

Here I changed the text color based on the theme value of sharedPref

 if (preferenceUtils.getBooleanValue(PrefsKeyValue.bTheme)) {
        this.setTextColor(ResourceUtils.getColor(R.color.lightThemeTextColor));
    } else {
        this.setTextColor(ResourceUtils.getColor(R.color.colorWhite));
    }

Then use this class as a textview tag in the XML file.

    <com.mypkg.customview.CustomTextView
    style="@style/signup_textViewStyle"
    android:text="@string/activity_login_password" />

I believe you can handle property changes for control themes in the same way.

Related Problems and Solutions