0

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);
    }
  • navigation drawer options do not work directly using click listeners. you have to find the menu file in menu folder in resources(maybe) and then follow the ids to implement the menu item listener in the drawer class – Jamshaid Sep 08 '20 at 11:47
  • or you can follow this link https://stackoverflow.com/a/42297548/11877320 – Jamshaid Sep 08 '20 at 11:48

3 Answers3

0

Firstly, try to always create an Onclicklistener instead of adding an onclick attribute in xml.

In your xml, add an ID attribute to your linear layout

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:id="@+id/linearlayout"

        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>

Now in your classfile inside the Oncreate Method,

LinearLayout LinearLayout = (LinearLayout )findViewById(R.id.linearlayout);
LinearLayout.setOnClickListener(new OnClickListener() {      
    @Override
    public void onClick(View v) {
        redirectActivity(this,TournamentInfo.class);
    }
});
Ankit Rath
  • 59
  • 7
0

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)

  • I will surely try this, But actually i did a lot of work creating this method, so trying to solve this error rather than changing evrything – Siddharth Sharma Sep 08 '20 at 18:21
0

You already have an onClick method. start navigating from that method only. Try like this,
enter image description here

G Ganesh
  • 103
  • 7