Sam
Sam

Reputation: 49

ImageView onClick need to click twice to perform an action

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();
                    }
                });

Upvotes: 3

Views: 1050

Answers (2)

user21921105
user21921105

Reputation: 1

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

Upvotes: 0

Gary Robottom
Gary Robottom

Reputation: 121

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.

Upvotes: 3

Related Questions