Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 background I'm using ?attr/colorAccent, so it can pick the background color based on the selected theme.
If I use any of the above values it works fine. But I want to define a custom item value for my background color.
I tried like this below but it didn't worked. any ideas to make it work ?
My custom theme with custom value:

<style name = "customTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">#4285f4</item>
    <item name="colorPrimaryDark">#2C75F2</item>
    <item name="colorAccent">#E1FFC7</item>
    <item name="customBgColor">#d3d3d3</item>
</style>


And I want to use it in layout's style as

<style name="layoutStyle" >
    <item name="android:background">?attr/customBgColor</item>
</style>
like image 565
Shree Avatar asked Oct 03 '16 07:10

Shree


People also ask

How do I change the theme on my Android apps?

In the Project pane select Android, go to app > res > values > themes > themes.

How do I change the default background color in Android Studio?

Create background color. By default each activity in Android has a white background. To change the background color, first add a new color definition to the colors. xml file in the values resource folder like the following.

Where is Styles XML in Android Studio?

Styles and themes are declared in a style resource file in res/values/ , usually named styles. xml .


1 Answers

Create a attrs.xml file shown in image.

<?xml version="1.0" encoding="utf-8"?>
<resources>

   <!-- Other values-->
   <attr name="customBgColor" format="reference" />

</resources>

enter image description here

customTheme 1

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

customTheme 2

<style name = "customTheme2" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Other values-->
    <!-- Black Color in theme2-->
    <item name="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.

public class AboutUsActivity extends Activity {

    int theme = 1;
    // int theme = 2;  2nd theme.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        switch (theme) {
            default:
            case 1:
                this.setTheme(R.style.customTheme1);
                break;
            case 2:
                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.

like image 102
Sohail Zahid Avatar answered Oct 07 '22 00:10

Sohail Zahid