Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use not a precompiled Theme

Is it possible to create the Theme instead of having the limited number of themes precompiled (meaning both the OS shipped themes and resource xml) ? The theme is going to be applied during the application launch based on the user configuration (values are changing at runtime but before the styled activity is going to be created, the values are coming from http services from a large range rather than a set).

Any other solution is welcome until it requires to use the customized View classes everywhere.

What I need for now is to set the global default TextView text color and of course I don't want to use the subclass everywhere, I think there's no huge disaster from loosing the optimization or at least it would be great to see the performance difference.

like image 649
A-Live Avatar asked Nov 04 '22 19:11

A-Live


2 Answers

Well this might be a bit too hacky but here's a shot.

public class BaseActivity extends Activity{

    @Override
    public void onResume() {
        ViewGroup root = ((ViewGroup)findViewById(android.R.id.content));
        applyTheme(root);
    }

    private void applyTheme(View view){
        if (view instanceof ViewGroup && ((ViewGroup) view).getChildCount() !=0){
            for (int i = 0; i< ((ViewGroup) view).getChildCount(); i++){
                applyTheme(((ViewGroup) view).getChildAt(i));
            }
        } else {
            if (view instanceof TextView){
                ((TextView) view).setTextColor(your_color_from_server_here);
            }
        }
    }
}

And have all your activities extend BaseActivity.

like image 102
Benito Bertoli Avatar answered Nov 15 '22 01:11

Benito Bertoli


Override ContextThemeWrapper.getTheme() in your Activity, and provide your own Theme.

Or apply predefined theme in Activity.onCreate before calling to super.onCreate, providing own theme resource id:

@Override
public void onCreate(Bundle si){
   setTheme(R.style.MyTheme);
   super.onCreate(si);
}

Using theme in xml style file is preferred. You can inherit some system theme, and override only needed attributes. Example of your style.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <style name="MyTheme" parent="android:Theme.Holo">
      <item name="android:textColorPrimary">#0f0</item>
   </style>
</resources>

Styling and themes are powerful, however there's lack of in-depth documentation, and no high-level tools.

like image 20
Pointer Null Avatar answered Nov 15 '22 00:11

Pointer Null