4

It is easy to change theme attributes in styles.xml, and to define new custom theme attributes in attrs.xml.

But how to do it in java code?

After calling setTheme in Activity, how to change or define attribute programmatically?

TestActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Apply "AppTheme" to this Activity
    super.setTheme(R.style.AppTheme);

    // How to change theme attribute "colorPrimary" or "android.R.attr.colorPrimary" in here?

    // How to define new custom theme attribute "customThemeAttribute" in here?
}

Thanks.

1 Answer 1

3

After doing some research, there is no way to set theme attribute programmatically.

Sign up to request clarification or add additional context in comments.

1 Comment

Well, I am glad you researched. This might go out of scope of OP's question, but I think it doesn't. But isn't there alternatives? I recently started to segregate/extract repeated attributes from my XML view files into <style> elements. I followed this guide . This removes duplication, that's fine... So, now I got a main MyStyle that I apply to the main layouts, and several children for other color settings. Isn't it possible to apply those <style>s to the implied objects programatically?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.