3

I had a button with a drawable selector set as its background.

button_sign_in_background.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:drawable="@drawable/button_sign_in_background_selected" android:state_focused="true" />
<item android:drawable="@drawable/button_sign_in_background_selected" android:state_pressed="true" />
<item android:drawable="@drawable/button_sign_in_background_selected" android:state_selected="true" />
<item android:drawable="@drawable/button_sign_in_background_normal" />

</selector>

button_sign_in_background_normal.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:visible="true">

<corners android:radius="@dimen/abc_control_corner_material" />
<solid android:color="@color/color_primary" />
</shape>

button_sign_background_selected.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">

<corners android:radius="@dimen/abc_control_corner_material" />
<solid android:color="@color/color_primary_dark" />
</shape>

I tested in two different devices, one with API 21 and another is API 10. The background button_sign_in_background.xml can't show in API 10, but it works in API 21 device.

If I use color directly in button_sign_in_background.xml as following, both devices work. But this is not the effect I need, what I want is a small radius corner surround the button.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:drawable="@color/color_primary_dark" android:state_focused="true" />
<item android:drawable="@color/color_primary_dark" android:state_pressed="true" />
<item android:drawable="@color/color_primary_dark" android:state_selected="true" />
<item android:drawable="@color/color_primary" />

</selector>

Is that a compatibility problem in old android device?

How can i solve it? Any kind of comment and answer are welcomed.

alijandro
  • 11,627
  • 2
  • 58
  • 74

2 Answers2

1

I use the following code to solve the compatibility issue. Only tested with API level 10 (Android 2.3.3). After show the background drawable manually, it works fine. If there are more versions have this compatibility issue, comment will be welcomed.

    if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD_MR1) {

        mEmailSignInButton.setBackgroundDrawable(
                getResources().getDrawable(R.drawable.button_sign_in_background));
    }
alijandro
  • 11,627
  • 2
  • 58
  • 74
0

If you want small radius corner button, below code has no problem from API 10 to Lollipop.

btn_done_background.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="false" android:drawable="@drawable/shape_round_normal"/>
    <item android:state_pressed="true" android:drawable="@drawable/shape_round_pressed"/>
</selector>

shape_round_normal.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
   android:shape="rectangle">
    <solid android:color="@color/color_56b8e9"/>
    <corners android:radius="4dp"/>
</shape>

shape_round_pressed.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
   android:shape="rectangle">
    <solid android:color="@color/color_3ba2e1"/>
    <corners android:radius="4dp"/>
</shape>

A button to apply

I use LinearLayout as a button.

<LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:id="@+id/linearLayoutButtonReply"
            android:layout_centerVertical="true"
            android:background="@drawable/btn_done_background"
            android:clickable="true"
            android:gravity="center_vertical"
            android:paddingLeft="10dp"
            android:paddingRight="10dp">
    <ImageView
                android:layout_width="15dp"
                android:layout_height="15dp"
                android:id="@+id/imageViewReply"
                android:layout_marginRight="5dp"
                android:layout_marginEnd="5dp"
                android:duplicateParentState="true"
                android:src="@drawable/ic_card_btn_answer"/>

    <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/textViewReplyCount"
                android:duplicateParentState="true"
                android:textColor="@android:color/white"
                android:textSize="13sp"
                android:text="@string/ReplyEmpty"
                />
</LinearLayout>
Youngjae
  • 24,352
  • 18
  • 113
  • 198
  • Thanks for the answer. But I want to apply the drawable selector to the background of button directly, not a `LinearLayout` or other view group. – alijandro Apr 13 '15 at 00:05