0

I'm trying to launch a activity from a button inside my gridview item, meaning that when user click on gridview Item it should do one function and when the user click on the button inside the gridview item it should do another function, let me elaborate, here is my single item layout that is being populated inside of my gridview;

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:foreground="?android:attr/selectableItemBackground"
android:orientation="horizontal"
app:cardCornerRadius="5dp"
app:cardElevation="3dp"
app:cardPreventCornerOverlap="false"
app:cardUseCompatPadding="true">

<RelativeLayout
    android:id="@+id/single_row"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="10dp"
    android:paddingTop="10dp">

    <com.mikhaellopez.circularimageview.CircularImageView
        android:id="@+id/imgFood"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        app:civ_border_color="#ffffff"
        app:civ_border_width="2dp"
        app:civ_shadow="true"
        app:civ_shadow_color="#000000"
        app:civ_shadow_radius="10" />

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_toRightOf="@+id/imgFood">

    <TextView
        android:id="@+id/txtName"
        android:layout_marginLeft="5dp"
        android:layout_marginStart="5dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Name"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="16sp"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/currentid"
        android:layout_marginLeft="5dp"
        android:layout_marginStart="5dp"
        android:layout_below="@+id/txtName"
        android:text="Current ID : "
        android:visibility="visible"
        />

    <TextView
        android:id="@+id/studentid"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ID"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="16sp"
        android:layout_below="@+id/txtName"
        android:layout_toRightOf="@+id/currentid"
        android:layout_marginEnd="30dp"
        android:layout_marginRight="30dp"
        />

    </RelativeLayout>

    <Button
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_gravity="end"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:id="@+id/editit"
        android:background="@android:drawable/btn_dialog"/>


</RelativeLayout>


here is how the above layout looks:

here is a image of the above layout


and here is my adapter getview from where I try to do the intent/actions from;

@Override
public View getView(int position, View view, ViewGroup viewGroup) {

    View row = view;
    ViewHolder holder = new ViewHolder();


    if (row == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(layout, null);

        holder.txtName = (TextView) row.findViewById(R.id.txtName);
        holder.txtPrice = (TextView) row.findViewById(R.id.studentid);
        holder.imageView = (CircularImageView) row.findViewById(R.id.imgFood);
        holder.dit = (Button) row.findViewById(R.id.editit);

        final ViewHolder finalHolder = holder;
        holder.dit.setOnClickListener(new View.OnClickListener() {

            String getname = finalHolder.txtName.getText().toString();
            String gethandicap = finalHolder.txtPrice.getText().toString();

            @Override
            public void onClick(View v) {
                // Do something
                Intent editintent = new Intent(context, MainActivity.class);
                editintent.putExtra("studentname", getname);
                editintent.putExtra("studentid", getID);
                context.startActivity(editintent);
            }
        });

        row.setTag(holder);
    } else {
        holder = (ViewHolder) row.getTag();
    }

    Sudentdb student = studentsList.get(position);

    holder.txtName.setText(student.getName());
    holder.txtPrice.setText(student.getID());


    if (student.getImage() != null && student.getImage().length > 0) {
        Glide.with(context)
                .load(student.getImage())
                .into(holder.imageView);
    } else {
        holder.imageView.setImageBitmap(null);
    }

    return row;

}

The problem I'm having is that the button inside the gridview item is not performing any actions, in log it only says ACTION_DOWN and since I added a button inside the item the item onclick also doesn't work.


EDIT; ADDING GRIDVIEW ONCLICK AND LAYOUT

gridView = (GridView) findViewById(R.id.gridView);
    list = new ArrayList<>();
    adapter = new StudentListAdapter(this, R.layout.food_items, list);
    gridView.setAdapter(adapter);

    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {

            String dataFromGrid;
            dataFromGrid = ((TextView)(view.findViewById(R.id.txtName))).getText().toString();


            Intent i = new Intent(getApplicationContext(), Student.class);

            i.putExtra("unfromstudent",dataFromGrid);
            startActivity(i);

gridview layout (I have tried focusable and clickable)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true"
    android:focusable="true"
     >

    <GridView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:focusable="true"
        android:id="@+id/gridView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:columnWidth="120dp"
        android:gravity="center"
        android:layout_margin="2dp"
        android:numColumns="1"

        />

</RelativeLayout>
HB.
  • 4,116
  • 4
  • 29
  • 53

2 Answers2

2

Change you XML, i have tested below xml its works fine with GridView

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:foreground="?android:attr/selectableItemBackground"
    android:orientation="horizontal"
    app:cardCornerRadius="5dp"
    app:cardElevation="3dp"
    app:cardPreventCornerOverlap="false"
    app:cardUseCompatPadding="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:focusable="false"
        android:orientation="horizontal">


        <com.mikhaellopez.circularimageview.CircularImageView
            android:id="@+id/imgFood"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_margin="10dp"
            app:civ_border_color="#ffffff"
            app:civ_border_width="2dp"
            app:civ_shadow="true"
            app:civ_shadow_color="#000000"
            app:civ_shadow_radius="10" />


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal|center_vertical"
            android:layout_weight="1"
            android:gravity="center_horizontal|center_vertical"
            android:orientation="vertical">

            <TextView
                android:id="@+id/txtName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginStart="5dp"
                android:text="Name"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textSize="16sp" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/currentid"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/txtName"
                    android:layout_marginLeft="5dp"
                    android:layout_marginStart="5dp"
                    android:text="Current ID : "
                    android:visibility="visible" />

                <TextView
                    android:id="@+id/studentid"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/txtName"
                    android:layout_marginEnd="30dp"
                    android:layout_marginRight="30dp"
                    android:layout_toRightOf="@+id/currentid"
                    android:text="ID"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:textSize="16sp" />
            </LinearLayout>
        </LinearLayout>

        <LinearLayout
            android:layout_width="30dp"
            android:layout_height="30dp">

            <Button
                android:id="@+id/editit"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_gravity="end"
                android:background="@android:drawable/btn_dialog"
                android:focusable="false" />


        </LinearLayout>

    </LinearLayout>


</android.support.v7.widget.CardView>

Your Grid View XML will be Like this.

<GridView
        android:id="@+id/grid"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:focusable="true">

    </GridView>
HB.
  • 4,116
  • 4
  • 29
  • 53
Aniruddh Parihar
  • 3,072
  • 3
  • 21
  • 39
  • This fixes the onclick for the button inside the item but now onclick of the item itself doesn't work. – HB. Jun 07 '17 at 08:55
  • You posted the above, which is a item/cardview inside my gridview. It has a button inside which you now fixed, when I click the button it opens a new activity. That works. But now the click event of the item/cardview itself, not the button inside the cardview, but the cardview itself doesn't work. – HB. Jun 07 '17 at 09:02
  • please also look at my edit, I added the gridview onclick event – HB. Jun 07 '17 at 09:27
  • can you show me you code of Layout for both item and GridView? – Aniruddh Parihar Jun 07 '17 at 09:28
  • I fixed it, remove `android:focusable="false"` inside our answer inside cardview and I will accept your answer/ – HB. Jun 07 '17 at 10:02
  • it will be real Appreciation for me.. :) – Aniruddh Parihar Jun 07 '17 at 10:04
0

I don't have enough reputation so i can't comment your question, why don't you use a recyclerview with a gridLayoutManager? You can see a basic example here .

After that you can implement your recyclerview adapter this way:

public class StudentsAdapter extends RecyclerView.Adapter<StudentsAdapter.ViewHolder> {

private final OnStudentItemClickListener mListener;
private Context mContext;
private List<Student> mStudents;

public StudentsAdapter(Context context, List<Student> items, OnStudentItemClickListener listener) {
    mContext = context;
    mStudents =items;
    mListener = listener;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.list_item_student, parent, false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
    Student student = mStudents.get(position);

    holder.mTextName.setText(student.getName());
    holder.mTextPrice.setText(student.getID());

    if (student.getImage() != null && student.getImage().length > 0) {
        Glide.with(context)
                .load(student.getImage())
                .into(holder.imageView);
    } else {
        holder.imageView.setImageBitmap(null);
    }
}

@Override
public int getItemCount() {
    return mStudents.size();
}


public interface OnStudentItemClickListener {
    void onStudentItemClick(Student student);
    void onStudentButtonClick(Student student);
}

public class ViewHolder extends RecyclerView.ViewHolder
        implements View.OnClickListener {
    TextView mTextName;
    TextView mTextPrice;
    CircleImageView mImageView;
    Button mDit;

    public ViewHolder(View view) {
        super(view);

        mTextName = (TextView) view.findViewById(R.id.txtName);
        mTextPrice = (TextView) view.findViewById(R.id.studentid);
        mImageView = (CircleImageView) view.findViewById(R.id.imgFood);
        mDit = (Button) view.findViewById(R.id.editit);
        mDit.setOnClickListener(view1 -> {
            if (null != mListener) {
                mListener.onStudentButtonClick(mStudents.get(getAdapterPosition()));
            }
        });
        itemView.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if (null != mListener) {
            mListener.onStudentItemClick(mStudents.get(getAdapterPosition()));
        }
    }
}

}

Jameido
  • 1,344
  • 1
  • 11
  • 21