1

How does one create an ImageButton with transparent background that still clickable (still acts like a Button)?

This is the xml snippet:

 <ImageButton
            android:paddingRight="10dp"
            android:paddingLeft="10dp"
            android:paddingTop="7dp"
            android:paddingBottom="7dp"
            android:src="@drawable/serverschedule"
            android:background="@null"
            android:clickable="true"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="@string/predict"
            local:MvxBind="Click PredictCmd" />

I have also tried android:background="#00000000" and android:background="@android:color/transparent"and in all cases, I do get the desired visual effect but button no longer can be clicked.

I am using MvvmCross framework to binding to the Click event of the button, hence there is no code behind.

I am testing against API Level 15, if this matters.

EDIT Added entire axml for button. EDIT Adding MVVM framework as it may have something to do with problem.

TIA.

Klaus Nji
  • 18,107
  • 29
  • 105
  • 185
  • Provide desired width and height of button. – Siddharth_Vyas Nov 19 '13 at 13:16
  • your Button has no `layout_width` and `layout_height`. Also, are you shure that you apply a `View.OnCliclListener` to it? – Rafael T Nov 19 '13 at 13:17
  • @RafaelT, the only thing I am changing is the background color. I have another button besides this one that works fine with similar layout except I am not nullifying the background color. – Klaus Nji Nov 19 '13 at 13:24
  • Let me see if I understand your problem correctly. The button is correctly shown, the `OnClickListener` works but the button does not have "touch feedback" (http://developer.android.com/design/style/touch-feedback.html)? – rubenlop88 Nov 19 '13 at 13:26
  • @rubenlop88, the button is correctly shown with transparent background image but the OnClickListener is not invokved. Please note that I am using an MVVM framework so maybe the problem lies there. – Klaus Nji Nov 19 '13 at 13:29
  • Ok, then any of the answers below may work. Your `ImageButton` should have an `id` and you should attach a `OnClickListener` to it. – rubenlop88 Nov 19 '13 at 13:36

7 Answers7

2

Thanks for all of the suggestions.

This is what finally worked for me:

android:background="?android:attr/selectableItemBackground"

Based on responses from this thread.

Community
  • 1
  • 1
Klaus Nji
  • 18,107
  • 29
  • 105
  • 185
0

Please provide width and height of your button.

Also try this :

ImageButton theButton = (ImageButton )findViewById(R.id.theButton);
theButton.setVisibility(View.VISIBLE);
theButton.setBackgroundColor(Color.TRANSPARENT);

theButton.setOnClickListener(new OnClickListener()
{   
 @Override
 public void onClick(View v)
 {
    // DO STUFF
 }
 });

Hope this helps.

Siddharth_Vyas
  • 9,972
  • 10
  • 39
  • 69
0

Do certain Necessary changes

<ImageButton
                android:paddingRight="10dp"
                android:paddingLeft="10dp"
                android:paddingTop="7dp"
                android:paddingBottom="7dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/serverschedule"
                android:background="@null"
                android:id="@+id/mybutton"
</ImageButton

Into Your code:

mybutton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {
                //Your Stuff here
            }
    });
Nitesh Tiwari
  • 4,742
  • 3
  • 28
  • 46
0

try this...

<ImageButton
    android:id="@+id/imageButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/timeText"
    android:layout_centerHorizontal="true"
    android:background="@android:color/transparent"
    android:clickable="true"
    android:contentDescription="image button"
    android:src="@drawable/anchor" />

set clickable attribute to true, and handle onclick event in your activity like

    findViewById(R.id.imageButton).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Toast.makeText(getApplicationContext(), "text", Toast.LENGTH_SHORT).show();
        }
    });
Gopal Gopi
  • 11,101
  • 1
  • 30
  • 43
0

No change in your xml file.. even no need to add this android:clickable="true"

edit your java file as below code...

ImageButton imageButton;

    @Override
    public void onCreate(Bundle savedInstanceState) {

imageButton = (ImageButton) findViewById(R.id.imageButton1);

        imageButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

               Toast.makeText(MyAndroidAppActivity.this,
                "ImageButton is clicked!", Toast.LENGTH_SHORT).show();

            }

        });

    }
 }

If this doesn't work you might have problem with your parent layout. Please post that if so....

0

what's the meaning of the "acts as a button" ? when you use

android:background="@null" 


your ImageButton will have no background and you can use selector as the src ,you'll get the same acts as a button,also can click as usual selector like this

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

also see enter link description here

Community
  • 1
  • 1
Jerome
  • 1,749
  • 1
  • 14
  • 40
  • using android:background="@null" is one option to set a transparent background as I understand. Other options to make background transparent result to same effect. I do not intend to change the button's behavior, just the look. So do not expect to be changing selectors and such. – Klaus Nji Nov 19 '13 at 13:59
0
back.setBackgroundColor(Color.TRANSPARENT);

works for all android versions

Sam
  • 104
  • 3
  • 10