Elnaz
Elnaz

Reputation: 511

Different Button color for different themes with Android Shape Drawables

I have multiple themes in my application and I've defined a shape for my buttons which I like to change the color by changing theme in my app. I've done it as below, however the problem is with the color for solid tag, I can not use colorButtonNormal attribute here,

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
    <corners android:radius="7dp"/>
    <solid android:color="?attr/colorButtonNormal" />
</shape>

in my layout I have defined the button tag:

<Button
    android:id="@+id/sendCode"
    android:layout_marginTop="30dp"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="@string/sendCode"
    android:textSize="@dimen/TextTwoSize"
    android:textStyle="bold"
    android:textColor="?android:textColorPrimary"
    android:background="@drawable/bot_shape"/>

and here is part of my style.xml file for defining theme:

  <style name="myGreen" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/colorPrimaryGreen</item>
    <item name="colorAccent">@color/colorAccentGreen</item>
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="android:colorBackground">@color/windowBackground</item>
    <item name="colorButtonNormal">@color/colorAccentGreen</item>

    <item name="colorPrimaryDark">@color/colorPrimaryDarkGreen</item>
    <item name="android:textColorPrimary">@color/textColorPrimary</item>
    <item name="android:textColorPrimaryInverse">@color/textColorPrimaryInverse</item>
    <item name="android:actionOverflowButtonStyle">@style/MyActionButtonOverflow</item>
    <item name="android:textColorSecondary">@android:color/white</item>
    <item name="android:windowBackground">@color/windowBackground</item>
    <item name="android:homeAsUpIndicator">@drawable/ic_keyboard_backspace_white_24dp</item>
    <item name="android:itemBackground">@android:color/white</item>
    <item name="android:textColor">@color/TextOneColor</item>
</style>
<style name="myRed" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/colorPrimaryRed</item>
    <item name="colorAccent">@color/colorAccentRed</item>
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDarkRed</item>
    <item name="android:textColorPrimary">@color/textColorPrimary</item>
    <item name="android:textColorPrimaryInverse">@color/textColorPrimaryInverse</item>
    <item name="android:actionOverflowButtonStyle">@style/MyActionButtonOverflow</item>
    <item name="android:textColorSecondary">@android:color/white</item>
    <item name="android:windowBackground">@color/windowBackground</item>
    <item name="android:colorBackground">@color/windowBackground</item>
    <item name="android:itemBackground">@android:color/white</item>
    <item name="android:textColor">@color/TextOneColor</item>
    <item name="colorButtonNormal">?attr/colorPrimary</item>

</style>

I get the error:

   Caused by: java.lang.UnsupportedOperationException: Can't convert to color: type=0x2
                                                             at android.content.res.TypedArray.getColor(TypedArray.java:326)
                                                             at android.graphics.drawable.GradientDrawable.inflate(GradientDrawable.java:951)
                                                             at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:881)
                                                             at android.graphics.drawable.Drawable.createFromXml(Drawable.java:818)
                                                             at android.content.res.Resources.loadDrawable(Resources.java:1948)
                                                             at android.content.res.TypedArray.getDrawable(TypedArray.java:601) 
                                                             at android.view.View.<init>(View.java:3359) 
                                                             at android.widget.TextView.<init>(TextView.java:477) 
                                                             at android.widget.Button.<init>(Button.java:107) 
                                                             at android.support.v7.widget.AppCompatButton.<init>(AppCompatButton.java:70) 
                                                             at android.support.v7.widget.AppCompatButton.<init>(AppCompatButton.java:66) 
                                                             at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:109) 
                                                             at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1016) 
                                                             at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1073) 
                                                             at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:675) 
                                                             at android.view.LayoutInflater.rInflate(LayoutInflater.java:746) 
                                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:489) 
                                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 
                                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 
                                                             at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287) 
                                                             at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139) 
                                                             at io.appogram.navigation.ConnectEmailActivity.onCreate(ConnectEmailActivity.java:30) 
                                                             at android.app.Activity.performCreate(Activity.java:5066) 
                                                             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1102) 
                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2288) 
                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2368) 
                                                             at android.app.ActivityThread.access$600(ActivityThread.java:151) 
                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1330) 
                                                             at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                             at android.os.Looper.loop(Looper.java:155) 
                                                             at android.app.ActivityThread.main(ActivityThread.java:5536) 
                                                             at java.lang.reflect.Method.invokeNative(Native Method) 
                                                             at java.lang.reflect.Method.invoke(Method.java:511) 
                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1074) 
                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:841) 
                                                             at dalvik.system.NativeStart.main(Native Method) 

Upvotes: 0

Views: 831

Answers (1)

Jyoti JK
Jyoti JK

Reputation: 2171

?attr/ will only work for api>=21 in drawables

For below versions , You can't use "?attr/" in drawables.

Try adding separate drawable for each themes and set it programmatically.

You can use ?attr/ in layout xml files. So you can use below line in button

android:background="?attr/colorButtonNormal"

If you want shape, using separate drawable for each theme is better one.

Upvotes: 3

Related Questions