RobbieP14383
RobbieP14383

Reputation: 145

Android, Set ImageButton Background To Null and Set onClick Highlight Color

Is it possible to set the background of an ImageButton to "@null" and still choose an onClick highlight color?

Many thanks

EDIT

I am using the following xml on my ImageButton so that the android background is null but my custom background is:

<ImageButton
    android:id="@+id/m1_btn"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@null"
    android:src="@drawable/pause"
    tools:ignore="ContentDescription" />

Then my buttons background is controlled in my Java like so:

    pp_btn1.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        i +=1;
        if (i % 2 == 0) {
            pp_btn1.setImageResource(R.drawable.pause);
        } else {
            pp_btn1.setImageResource(R.drawable.play);
        }
    }
});

With this explained, is there a way of still achieving the button highlight onClick?

Many thanks

Upvotes: 0

Views: 2473

Answers (1)

khubaib
khubaib

Reputation: 535

Yes have a new xml clickedstates.xml in drawable folder which defines what happens when you press on the image button and what should be the default background.and give this xml as background to your image button like @drawable/clickedstates...For eg:

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

<item     android:state_enabled="false"     
android:drawable="@drawable/default_bgnd" />

<item     android:state_focused="true"        
android:drawable="@drawable/new_green" />

<item     android:state_pressed="true"        
android:drawable="@drawable/new_green" />

<item     android:state_checked="true"        
android:drawable="@drawable/new_green"/>

<item     android:state_selected="true"        
android:drawable="@drawable/new_green" /> 
</selector> 

Upvotes: 1

Related Questions