Disable pressed background color of button

4.6k Views Asked by At

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.

2

There are 2 best solutions below

1
nidhi On BEST ANSWER

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!!!

1
Jatin Malwal On

Make a selector in drarable folder. It will be look like

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

Set this selector as a backgroung to the button. Now you can change in selector for all the states of button.