0

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

RobbieP14383
  • 145
  • 4
  • 15
  • You can still achieve this with the xml provided. Given that you have separate images for your pause and Play in the draw able folder. like pauseglow.png and playglow.png which gets sets to the image button as soon as u click on it.and in your onTouchListener with MotionEvent.ACTION_DOWN: you can change it to pp_btn1.setBackgroundResource(R.drawable.clickedstate); – khubaib Sep 08 '13 at 16:55
  • Oh ok, great, I shall try this out tonight when I get in. Thanks – RobbieP14383 Sep 09 '13 at 10:14

1 Answers1

1

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> 
khubaib
  • 535
  • 4
  • 12
  • Does this mean I need 5 different ImageButton backgounds for each ImageButton? Seems like a lot of work for one simple aspect of the app. :S – RobbieP14383 Sep 08 '13 at 16:23
  • Have ust tried this but it only works on the first click of the button, click it again and its back to normal, I dont think this is compatible with my button code. :S – RobbieP14383 Sep 08 '13 at 16:30
  • You wanted a solution to have the image button to have null background and when it is clicked/pressed it should change color.well the above xml is your solution.instead of havin 5 items in the xml you can just use one like state pressed or selected or focussed or anything what you want to do when the button is clicked and by default you can have one item with no android:state defined have a default background which in your case is null. and finall add this to your image button in the your main xml. android:background="@drawable/clickedstates" – khubaib Sep 08 '13 at 16:36
  • I guess I have not explained my question fully, I shall edit it . . . – RobbieP14383 Sep 08 '13 at 16:39