user3718930
user3718930

Reputation: 381

android-button background drawable doesn't work

this is my xml drawbale code with the name of btntheme.xml :

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/customactionbartheme_btn_default_normal_holo_light" android:state_enabled="true" android:state_window_focused="false"/>
    <item android:drawable="@drawable/customactionbartheme_btn_default_disabled_holo_light" android:state_enabled="false" android:state_window_focused="false"/>
    <item android:drawable="@drawable/customactionbartheme_btn_default_pressed_holo_light" android:state_pressed="true"/>
    <item android:drawable="@drawable/customactionbartheme_btn_default_focused_holo_light" android:state_enabled="true" android:state_focused="true"/>
    <item android:drawable="@drawable/customactionbartheme_btn_default_normal_holo_light" android:state_enabled="true"/>
    <item android:drawable="@drawable/customactionbartheme_btn_default_disabled_focused_holo_light" android:state_focused="true"/>
    <item android:drawable="@drawable/customactionbartheme_btn_default_disabled_holo_light"/>

</selector>

I've put all those images with the these names in the drawable-hdpi folder .

this is my layout and the button :

<Button
                android:id="@+id/button1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="50"
                android:background="@drawable/btntheme"
                android:textSize="@dimen/font_size_smallerButton" />

it's not working , it doesn't change the button theme at all ?

could you help me to solve it ?

thank you

Upvotes: 2

Views: 7478

Answers (7)

Asad
Asad

Reputation: 1439

navigate to res/values/styles/styles.xml or res/values/themes/themes.xml and change Theme.MaterialComponents.Light.NoActionBar to Theme.AppCompat.Light.NoActionBar

or just replace MaterialComponents with AppCompat

Upvotes: 2

Muhammad Rizwan
Muhammad Rizwan

Reputation: 37

issue is with material design that is mentioned in style simple replace it with

<style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">

Upvotes: 1

Satyam
Satyam

Reputation: 41

Ok I also suffered form this problem, remove android:state_focused="true" line form your code and run. hope get the answer :), (i think focus is only for input type views in my knowledge if i wrong plz guide me) for testing focus state press TAB key this is sample:-

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!--When pressed-->
    <item
        android:state_pressed="true"
        android:drawable="@drawable/btn_bg" />
    <!--focus state -->
    <item
        android:state_focused="true"
        android:state_pressed="false"
        android:drawable="@color/colorAccent"/>
    <!--Default State-->
    <item
        android:state_focused="false"
        android:state_pressed="false"
        android:drawable="@color/transparent_black" />
<!-- how confirm focus state of button -->
</selector>

Upvotes: -1

Somil
Somil

Reputation: 481

The problem might be that the screen you are testing on does not use drawable-hdpi, so try to move the images and xml file itself to the drawable folder, if it does not exist then create it.

Upvotes: 1

Shyam
Shyam

Reputation: 6444

Create drawable folder in res and put btntheme.xml in that folder .

Upvotes: -1

bhavesh kaila
bhavesh kaila

Reputation: 761

try to add <?xml version="1.0" encoding="utf-8"?> in xml file and xml file will like

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


<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Non focused states -->
    <item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/custom_tab_indicator_unselected" />
    <item android:state_focused="false" android:state_selected="true"  android:state_pressed="false" android:drawable="@drawable/custom_tab_indicator_selected" />

    <!-- Focused states -->
    <item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/custom_tab_indicator_unselected_focused" />
    <item android:state_focused="true" android:state_selected="true"  android:state_pressed="false" android:drawable="@drawable/custom_tab_indicator_selected_focused" />

    <!-- Pressed -->
    <!--    Non focused states -->
    <item android:state_focused="false" android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/custom_tab_indicator_unselected_pressed" />
    <item android:state_focused="false" android:state_selected="true"  android:state_pressed="true" android:drawable="@drawable/custom_tab_indicator_selected_pressed" />

    <!--    Focused states -->
    <item android:state_focused="true" android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/custom_tab_indicator_unselected_pressed" />
    <item android:state_focused="true" android:state_selected="true"  android:state_pressed="true" android:drawable="@drawable/custom_tab_indicator_selected_pressed" />
</selector>

after using this if it will not work then if possible try to use image view instead of button i think that will be work. thanks;-)

Upvotes: 2

GrIsHu
GrIsHu

Reputation: 23638

Try out selector as below:

<!-- Active tab -->
<item android:drawable="@drawable/customactionbartheme_btn_default_normal_holo_light" android:state_focused="false" android:state_pressed="false" android:state_selected="true"/>
<!-- Inactive tab -->
<item android:drawable="@drawable/customactionbartheme_btn_default_pressed_holo_light" android:state_focused="false" android:state_pressed="false" android:state_selected="false"/>
<!-- Pressed tab -->
<item android:drawable="@drawable/customactionbartheme_btn_default_pressed_holo_light" android:state_pressed="true"/>
<!-- Selected tab (using d-pad) -->
<item android:drawable="@drawable/customactionbartheme_btn_default_disabled_holo_light" android:state_focused="true" android:state_pressed="false" android:state_selected="true"/>

</selector>

For more detail guidenc check out Custom Selector in Android

Upvotes: 1

Related Questions