Skip to content
Advertisement

onClick method not working in android studio

I made a side navigation drawer and it works fine but whenever I click any option in navigation drawer nothing happens.It’s like it is not taking any input. Onclicking any option I want to redirect user to a new activity but unfortunately it doesn’t happens.

XML code is as follows

 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center_vertical"
        android:clickable="true"
        android:onClick="ClickTournament_info"
        android:focusable="true">

    <TextView 
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Tournament Info"
        android:padding="12dp"
        android:layout_marginStart="16dp"/>

    <ImageView
        android:layout_marginTop="-10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="48dp"
        android:src="@drawable/tournament_info"/>

</LinearLayout>

Java code is as follows

public void ClickTournament_info(View view){
        redirectActivity(this,TournamentInfo.class);

    }



public static void redirectActivity(Activity activity,Class aClass) {
        //Initialize intent
        Intent intent = new Intent(activity,aClass);
        //set flag
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        //start activity
        activity.startActivity(intent);
    }

Advertisement

Answer

I think you need to use the onNavigationItemSelected method when you want to handle click events on the navigation drawer.

Learn more: https://developer.android.com/reference/com/google/android/material/navigation/NavigationView.OnNavigationItemSelectedListener#onNavigationItemSelected(android.view.MenuItem)

Advertisement