Yoda
Yoda

Reputation: 18068

Changing font color for entire application or activity via code(java)

Is it possible to change font color for entire application or activity via code(java)? I want to read color from shared preferences and then change the font color inside the activity. I have done that for background and it works, but I do not know how to change font globaly.

    public void usePreferences(){
        SharedPreferences settings = getSharedPreferences(OptionListActivity.MY_PREFERENCES, MODE_WORLD_READABLE);

        String backColorAsString = settings.getString(getResources().getString(R.string.background_color), "0");
        Log.i(getResources().getString(R.string.font_color), backColorAsString);
        int backColorRGB = 0;
        if (backColorAsString.equals("RED"))
            backColorRGB = Color.RED;
        else if (backColorAsString.equals("BLUE"))
            backColorRGB = Color.BLUE;
        else if (backColorAsString.equals("GREEN"))
            backColorRGB = Color.GREEN;

        findViewById(android.R.id.content).setBackgroundColor(backColorRGB);

//works great till here


String fontColorAsString = settings.getString(getResources().getString(R.string.font_color), "0");
        int fColorRGB = 0;
        if (fontColorAsString.equals("RED"))
            fColorRGB = Color.RED;
        else if (fontColorAsString.equals("BLUE"))
            fColorRGB = Color.BLUE;
        else if (fontColorAsString.equals("GREEN"))
            fColorRGB = Color.GREEN;

//WHAT TO DO NOW?   
    }

EDIT:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
    }
    usePreferences();
}

Upvotes: 0

Views: 521

Answers (1)

Aashir
Aashir

Reputation: 2621

PART 1

You could create a custom TextView. For the text color to be set the fastest, set the global color in your application class. (Not main activity)

public class ColorTextView extends TextView {

    private static int color = Color.BLUE;

    public ColorTextView(Context context) {
        super(context);
        this.setTextColor(color)
    }

    public ColorTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.setTextColor(color)
    }

    public ColorTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.setTextColor(color)
    }

    public static void setGlobalColor(int newcolor) {
        color = newcolor;
    }
}

And use it in the xml like:

<your.package.name.ColorTextView
     //other stuff
/>

And finally you can set color in your code like:

ColorTextView.setGlobalColor(yourColor);

PART 2

Setup an application class like the following and paste the usepreferences() code into it.

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        // paste code and set color here
    }
}

Finally, for this to run, you'll have to declare it in your Manifest in the application tag:

android:name="your.package.name.MyApplication"

Upvotes: 2

Related Questions