You can put Imagebutton in android development. For that you need to remember following 3 steps.

  1. Add Image to Resouce
  2. Add ImageButton in Layout
  3. Set Onclick Listener in Activity




1. Add Image to Resouce

Add “loginbutton.jpg” or “loginbutton.png” file to your drawable resource.

2. Add ImageButton in Layout
<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:orientation=”vertical” >

<ImageButton
android:id=”@+id/ibtnLogin”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:src=”@drawable/loginbutton” />

</LinearLayout>




3. Set Onclick Listener in Activity
package com.example.android;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageButton;
import android.widget.Toast;
import android.view.View;
import android.view.View.OnClickListener;

public class LoginActivity extends Activity {

ImageButton imageButton;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

addListenerOnButton();

}

public void addListenerOnButton() {

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

imageButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View view) {

Toast.makeText(LoginActivity.this,
"Login Button is clicked!", Toast.LENGTH_SHORT).show();

}

});

}

}

Leave a Reply