[FIXED] Giving the same id to two different components in an xml file Android

Issue

I am using constraint layout in my xml file. I have a view like in the example. Imageview and textview. I want both of these to have the same action after clicking. How can I group the two together and give them an id?

xml :

<ImageView
                    android:id="@+id/menu_drawerLogout"
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:layout_marginStart="20dp"
                    android:layout_marginTop="8dp"
                    android:src="@drawable/ic_menu_exit"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/view6" />

                <TextView
                    android:id="@+id/menu_drawerLogout"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginStart="16dp"
                    android:layout_marginTop="17dp"
                    android:text="@string/sign_out_text"/>

Solution

My layout was inside a constraintlayout and in this case I used the constraintlayout group component.

xml :

<ImageView
                    android:id="@+id/img_logout"
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:layout_marginStart="20dp"
                    android:layout_marginTop="8dp"
                    android:src="@drawable/ic_menu_exit"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/view6" />

                <TextView
                    android:id="@+id/menu_drawerLogout"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginStart="16dp"
                    android:layout_marginTop="17dp"
                    android:text="@string/sign_out_text"
                    android:textColor="@color/black_choice"
                    android:textSize="15sp"
                    android:textStyle="bold"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintHorizontal_bias="0.0"
                    app:layout_constraintStart_toEndOf="@+id/img_logout"
                    app:layout_constraintTop_toBottomOf="@+id/img_star" />


                <androidx.constraintlayout.widget.Group
                    android:id="@+id/logout_group"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    app:constraint_referenced_ids="img_logout,menu_drawerLogout" />

Then I created the Group.setAllOnClickListener field in MainActivity :

 private fun Group.setAllOnClickListener(listener: View.OnClickListener?) {
        referencedIds.forEach { id ->
            rootView.findViewById<View>(id).setOnClickListener(listener)
        }
    }

and

 logoutGroup.setAllOnClickListener()
         logOutDialog()
 }

Answered By – Nisa Efendioglu

Answer Checked By – Clifford M. (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published