Search code examples
androidbuttoncolorsstatepressed

Disable pressed background color of button


My button consists of only letters. Every time i press it, it has a black background. How do i disable the black background? I don´t want to change the color, i want to disable the pressed state.


Solution

  • You need to create selector under drawable folder and set color or image over there for different sates of the button.

    I have created one sample application for you and am pasting code snippet:

    Code for button_selector.xml that contains selector code for button which you will need to put under drawable folder:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item android:drawable="@drawable/no_box_selector" android:state_pressed="true"></item>
        <item android:drawable="@drawable/no_box"></item>
    
    </selector>
    

    You will need to put no_box_selector and no_box pngs in drawable-hdpi or any drawable folders

    Code for activity_main.xml that will be your main xml that contains only one button as of now:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/relRingtone"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#000000"
        android:orientation="vertical" >
    
        <Button
            android:id="@+id/btn2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:background="@drawable/button_selector"
            android:padding="5dp"
            android:text="Click Me"
            android:textColor="#FFF"
            android:textSize="20sp"
            android:textStyle="bold" />
    
    </RelativeLayout>
    

    Try implementing these things and let me know if you need any further assisstance.....

    All the best!!!