2

I have set up CardView in my app.The card has ImageView on it.I have set up onClickListener on ImageView. But action is performed after clicking twice the ImageView.
This is code for CardView:

<LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical">
            <ImageView
                android:id="@+id/assign"
                android:layout_margin="7dp"
                android:padding="15dp"
                android:layout_width="70dp"
                android:background="@drawable/circle"
                android:layout_height="70dp"
                android:src="@drawable/assign" />
                <TextView
                    android:text="Assign Table"
                    android:focusableInTouchMode="true"
                    android:id="@+id/textAssignTable"
                    android:layout_gravity="center"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </LinearLayout>
        </LinearLayout>

        <ImageView
            android:layout_marginTop="70dp"
            android:layout_gravity="center"
            android:id="@+id/clear"
            android:src="@drawable/clear"
            android:layout_width="25dp"
            android:layout_height="25dp" />
    </LinearLayout>  

This is code for setting OnCLickListener for ImageView.

assign.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String tag = "";
                    fm = getFragmentManager();
                    tx = fm.beginTransaction();
                    AssignTable table=new AssignTable();
                    tx.replace(R.id.frame, table, tag);
                    //tx.addToBackStack(tag);
                    tx.commit();
                }
            });
            clear.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String tag = "";
                    fm = getFragmentManager();
                    fm.popBackStack();
                    tx = fm.beginTransaction();
                    tx.replace(R.id.frame, chooseTab, tag);
                    //tx.addToBackStack(tag);
                    tx.commit();
                }
            });  

This is code for OnClickListener for SwipeCardOnClick

  @Override
            public void onItemClick(View view, int i)
            {
                clear=(ImageView)view.findViewById(R.id.clear);
                phone=(TextView)view.findViewById(R.id.phone);
                assignTable=(ImageView)view.findViewById(R.id.assignTable);
                textAssignTable=(TextView)view.findViewById(R.id.textAssignTable);
                textAssignTable.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String tag = "";
                        fm = getFragmentManager();
                        tx = fm.beginTransaction();
                        AssignTable table=new AssignTable();
                        tx.replace(R.id.frame, table, tag);
                        //tx.addToBackStack(tag);
                        tx.commit();
                    }
                });
                assignTable.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String tag = "";
                        fm = getFragmentManager();
                        tx = fm.beginTransaction();
                        AssignTable table=new AssignTable();
                        tx.replace(R.id.frame, table, tag);
                        //tx.addToBackStack(tag);
                        tx.commit();
                    }
                });
                clear.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String tag = "";
                        fm = getFragmentManager();
                        fm.popBackStack();
                        tx = fm.beginTransaction();
                        tx.replace(R.id.frame, chooseTab, tag);
                        //tx.addToBackStack(tag);
                        tx.commit();
                    }
                });
Rumit Patel
  • 8,830
  • 18
  • 51
  • 70
Sam
  • 39
  • 7

2 Answers2

2

I suggest you remove android:focusableInTouchMode="true" or make it false.

This seems to consume the first click so not passing it on to the click listener. The second click then succeeds as the focus is now obtained. The other option is to set RequestFocus to true which will then set the focus so the first click will then not be consumed. I had a similar issue trying to keep button selected history using android:focusableInTouchMode="true". RequestFocus() then removed the 1st click consumption issue.

I hope this makes sense.

Gary Robottom
  • 101
  • 1
  • 5
0

You can overwrite In first you can src blank image For second you can src with color image For both set on click listener so it will look like favourite button

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 19 '23 at 09:03