Monday, 3 March 2014

How to Create ImageButton Click Function in Android


In this blog you will learn how to create ImageButton and perform click and onLongClick event on imageButton through click event.

main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
     >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Image Button"
        android:textColor="#336633"
        android:textStyle="bold"
        android:layout_marginTop="40dp"
        android:textSize="20sp" />


<--- Drag ImageButton from Paletee in xml editor from Images & Media -->

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="40dp"
        android:src="@drawable/ic_launcher" />

</LinearLayout>

ImageButtonActivity.java


package com.rakesh.tiwari.imagebutton;

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

public class ImageButtonActivity extends Activity {

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

final ImageButton imgbtn=(ImageButton)findViewById(R.id.imageButton1);

// Perform onclick event

imgbtn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

// Set different imageResource with ImageButton

imgbtn.setImageResource(R.drawable.yellowstar);

Toast.makeText(getApplicationContext(), "Image Button Clicked", Toast.LENGTH_LONG).show();
}
});

// Perform LongPress click event

imgbtn.setOnLongClickListener(new View.OnLongClickListener() {

@Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
imgbtn.setImageResource(R.drawable.greenstar);
Toast.makeText(getApplicationContext(), "Image Button Long Pressed", Toast.LENGTH_LONG).show();
return false;
}
});
}


}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.rakesh.tiwari.imagebutton"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:screenOrientation="portrait"
            android:name="com.rakesh.tiwari.imagebutton.ImageButtonActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>