Skip to content Skip to sidebar Skip to footer

How To Add Custom Item In Android Theme Declaration?

I'm having few custom themes in my styles.xml Now whenever the activity takes the theme, it uses the colorPrimary, colorPrimaryDark and colorAccent values. For my layout's backgr

Solution 1:

Create a attrs.xml file shown in image.

<?xml version="1.0" encoding="utf-8"?><resources><!-- Other values--><attrname="customBgColor"format="reference" /></resources>

enter image description here

customTheme 1

<stylename = "customTheme1"parent="Theme.AppCompat.Light.NoActionBar"><!-- Other values--><itemname="customBgColor">#d3d3d3</item></style>

customTheme 2

<stylename = "customTheme2"parent="Theme.AppCompat.Light.NoActionBar"><!-- Other values--><!-- Black Color in theme2--><itemname="customBgColor">#111111</item></style>

Setting Color to TextView as example.

You can use it in similar way in any widget anywhere.

This TextView is used in below activity.

<TextView
    android:id="@+id/txt_rate_us_about"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="Rate us on Play Store!"
    android:textColor="?attr/customBgColor"
    android:textSize="20dp" />

Want to set theme dynamically.

publicclassAboutUsActivityextendsActivity {

    inttheme=1;
    // int theme = 2;  2nd theme.@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stubsuper.onCreate(savedInstanceState);
        switch (theme) {
            default:
            case1:
                this.setTheme(R.style.customTheme1);
                break;
            case2:
                this.setTheme(R.style.customTheme2);
                break;

        }
        // you must call `setTheme()` before `setContentView()`
        setContentView(R.layout.activity_about);

    }

For multiple activities you have set theme for each of them separately.

Post a Comment for "How To Add Custom Item In Android Theme Declaration?"