-1

I am working on app where the Imageview is nested in Linearlayout, which is used to create the Gridview.

when the user click imageview 1 and then imageview 2, I change the imageview 1's image to imageview 2 if all the conditions are ok. How to do that?

code below for your reference

activity_main.xml
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"    >
<GridView
    android:id="@+id/gridView1"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:layout_alignParentLeft="true"
    android:numColumns="8"
    android:padding="2dp"
    android:background="@drawable/chessboard"
    >

</GridView>

square.xml

  <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
   <ImageView
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:id="@+id/imageView1"
        android:layout_gravity="center"
       />

   </LinearLayout>

CustomAdapter.java

  package com.example.android.chess;
  import android.content.Context;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.view.ViewGroup;
  import android.widget.BaseAdapter;
  import android.widget.ImageView;
  import android.widget.Toast;

  public class CustomAdapter extends BaseAdapter{

  int [] result;
  Context context;
  int [] imageId;
  private static LayoutInflater inflater=null;
  public CustomAdapter(MainActivity mainActivity,int[] prgmImages) {
    // TODO Auto-generated constructor stub
    result=prgmImages;
    context=mainActivity;
    imageId=prgmImages;
    inflater = ( LayoutInflater )context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return result.length;
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

public class Holder
{
    //TextView tv;
    ImageView img;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    Holder holder=new Holder();
    View rowView;

   rowView = inflater.inflate(R.layout.square, null);
   //holder.img=(ImageView) rowView.findViewById(R.id.ImagesId);
   holder.img=(ImageView) rowView.findViewById(R.id.imageView1);


   // holder.tv.setText(result[position]);
    holder.img.setImageResource(imageId[position]);

    rowView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            //Toast.makeText(context, "You Clicked "+ v.getBackground(), Toast.LENGTH_LONG).show();

            Toast.makeText(context, "You Clicked "+v.getLayoutParams()., Toast.LENGTH_LONG).show();
            // TODO Auto-generated method stub
            //Toast.makeText(context, "You Clicked "+result[position], Toast.LENGTH_LONG).show();
        }
    });

    return rowView;
}


}
Murali
  • 51
  • 7

2 Answers2

1

@I want to get the Imageview if an imageview (inside Gridview) is clicked. Here below in the setOnClickListener method, the object v points to the linearlayout. How to get the ImageView nested inside that when it is clicked?

Try out the below code to set clickListener to your ImageView :

private ViewHolder holder;

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    holder = new Holder();
    View rowView;

    if(rowView==null)
    {
      rowView = inflater.inflate(R.layout.square, null);
      //holder.img=(ImageView) rowView.findViewById(R.id.ImagesId);
      holder.img=(ImageView) rowView.findViewById(R.id.imageView1);
      rowView.setTag(holder);
     }
    else {
      holder = (Holder)rowView.getTag();
    }
   // holder.tv.setText(result[position]);
    holder.img.setImageResource(imageId[position]);

    holder.img.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Toast.makeText(context, "You Clicked the imageView", Toast.LENGTH_LONG).show();
        }
    });
}

@Also, Could you please explain about getView method?

The method getView() is called each time whenever a row is created of your GridView. Here you will find a beautifully explained answer regarding this issue.

Hope this helps.

Community
  • 1
  • 1
tahsinRupam
  • 6,325
  • 1
  • 18
  • 34
  • 1
    Line of code - holder = (Holder)rowView.getTag(); pops up error value cannot assign to final variable "Holder" – Murali Apr 15 '17 at 16:20
  • Thanks tahsinRupam, I getting dizzy, dont understand this code. – Murali Apr 16 '17 at 18:12
  • here I want to click an image view and then click the second imageview if the conditions are ok then I replace the imageview2's image with imageview1s's image. clear the imageview1's image can you pls help – Murali Apr 16 '17 at 18:29
  • That's a little broad question to ask in a same thread I'm afraid. There is not much room to answer that. You can ask a separate question for that matter. – tahsinRupam Apr 16 '17 at 19:11
1

you don't need LinearLayout where there is just one element. Just ImageView will do. If you are using ListView, use setOnItemSelectedListener

UPDATE #2
This is how your square.xml should look like
<?xml version="1.0" encoding="utf-8"?> <ImageView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" />

Akshay More
  • 416
  • 4
  • 9