Translate

Wednesday, 6 February 2013

Get Id of Image clicked in a layout

A simple example of handling a click event on an image in a linear layout. You will need three images in your res/drawable folder of the Android app.

The main activity XML that defines the layout structure and the image view(s).

//mainactivity.xml

<
LinearLayout xmlns:"http://schemas.android.com/apk/res/android

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
            android:id="@+id/lView1"       
        >   
< ImageView
                    android:id="@+id/imageView1"            
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/re"
            android:tag="0"
            />

<ImageView
                    android:id="@+id/imageView2"            
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher"
            android:tag="1"           
            />

<ImageView
                    android:id="@+id/imageView3"            
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/m12"
            android:tag="2"
            android:onClick="onClick"           
            />
        </LinearLayout>



and the corresponding .java class. Note that the clickhandler is in code as well as in the xml file.
 
// MainActivity.java

import android.os.Build;
import android.os.Bundle;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.ClipData;
import android.content.ClipDescription;
import android.view.DragEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.DragShadowBuilder;
import android.view.View.OnClickListener;
import android.view.View.OnDragListener;
import android.view.View.OnLongClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener,OnDragListener,OnLongClickListener {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
        LinearLayout l = (LinearLayout) findViewById(R.id.lView1);    
        if (l == null) Toast.makeText(this, "Unable to find GridView",Toast.LENGTH_SHORT).show();
        else {
       
            ImageView v = (ImageView) l.findViewById(R.id.imageView1);
            ImageView v1 = (ImageView) l.findViewById(R.id.imageView2);
            v.setOnClickListener ((View.OnClickListener) this);
            v1.setOnClickListener ((View.OnClickListener) this);
            v.setOnLongClickListener((View.OnLongClickListener) this);
            v1.setOnLongClickListener((View.OnLongClickListener) this);
        }

 }
 public Integer[] mThumbIds = {
         R.drawable.ic_launcher, R.drawable.m12,         
 };
 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.activity_main, menu);
  return true;
 }
 @TargetApi(Build.VERSION_CODES.HONEYCOMB)
 @Override
 public void onClick(View v) {
  String msg=(String) v.getTag();
  Toast.makeText(this, "Clicked "+msg,Toast.LENGTH_SHORT).show();
 }
 @TargetApi(Build.VERSION_CODES.HONEYCOMB)
 @Override
 public boolean onDrag(View v, DragEvent dragEv) {
  // TODO Auto-generated method stub
  return false;
 }

No comments: