12

So I'm new to android development...How can I create an image that acts like button, so when I press that image, image starts a specific activity. So I want this to show as image:

 <Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="33dp"
    android:text="Button" />
Mohsen Kamrani
  • 7,177
  • 5
  • 42
  • 66
user1414682
  • 147
  • 3
  • 3
  • 10
  • you mean an ImageButton http://developer.android.com/reference/android/widget/ImageButton.html – MikeIsrael Jun 24 '12 at 10:36
  • 1
    There is a control specifically for this in Android. Do a google search before posting your question.. – Ron Jun 24 '12 at 12:19

3 Answers3

25

Create ImageButton as:

In main.xml:

<ImageButton android:id="@+id/ib"
    android:src="@drawable/bookmark" <-- SET BUTTON IMAGE HERE -->
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
/>

In Code part:

ImageButton ib=(ImageButton)findViewById(R.id.ib);
ib.setOnClickListener(ibLis);
    }
    private OnClickListener ibLis=new OnClickListener(){

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            //START YOUR ACTIVITY HERE AS
             Intent intent = new Intent(YOUR_CURRENT_ACTIVITY.this,NextActivity.class);
             startActivity(intent, 0);
        }
    };

EDIT:

and second option if you want to create an image like button using Button View then Create an Custom button as:

First put your all images like for pressed,focused and default in res/drawable folder and then add an newbtn.xml in drawable/newbtn.xml as:

<?xml version="1.0" encoding="utf-8"?>  
<selector xmlns:android="http://schemas.android.com/apk/res/android">  
    <item android:state_pressed="true"  
          android:drawable="@drawable/button_pressed" /> <!-- pressed -->  
    <item android:state_focused="true"  
          android:drawable="@drawable/button_focused" /> <!-- focused -->  
    <item android:drawable="@drawable/button_normal" /> <!-- default -->  
</selector>

Finally in button XML set android:background as :

<Button    
    android:id ="@+id/btn"  
    android:layout_width="wrap_content"   
    android:layout_height="wrap_content"   
    android:text="Hello"  
    android:textColor="#ffffffff"  
    android:background="@drawable/newbtn"   <-- get button background to selector -->
    /> 

See this tutorial for Creating Custom Button with images

Creating custom, fancy buttons in Android

Kevin Michael
  • 118
  • 1
  • 7
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
1

With the ImageView element, attaching a click listener to it.

The XML:

<ImageView
    android:id="@+id/myImageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/myPic" 
    />

The code:

ImageView imageView = (ImageView) findViewById(R.id.myImageView);
imageView.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(ThisActivity.this, MyOtherActivity.class);
        startActivity(intent);
    }
});

You also could use a ImageButton (in the same way). It makes almost no difference. You can see more details here. Difference between a clickable ImageView and ImageButton

Community
  • 1
  • 1
User
  • 31,811
  • 40
  • 131
  • 232
0

It's an overcomplication to use "setOnClickListener()". Instead, use the 'onClick' property in XML:

<ImageButton
    android:id="@+id/button19"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent"
    android:onClick="yourCallback"
    android:src="@drawable/your_image"
/>

public void yourCallback(View view) 
{
    ...
}
Tim Cooper
  • 10,023
  • 5
  • 61
  • 77