1

I am developing a quiz app where I have a HomeFragment containing a NavigationView with DrawerLayout. I have created a separate class "NavigationHelpline" for binding layouts and implemented OnClickListener on HomeFragment. However, onClick is not working, and I cannot see any logs even though I added them.

Whenever I click on one of four helpline layouts, it doesn't show the log, and instead, it closes the navigation drawer.

Here is the HomeFragment code:

public class HomeFragment extends Fragment implements View.OnClickListener{
private DrawerLayout drawerLayout;
    private NavigationView  navigationViewRight;

    private NavigationHelpline navigationHelpline;
public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {

        drawerLayout = binding.drawerLayout;

        navigationViewRight = binding.navViewRight;
//To remove default behaviour of navigation view
        navigationViewRight.setNavigationItemSelectedListener(null);
        navigationHelpline = new NavigationHelpline(navigationViewRight);
        // Set OnClickListener for Helpline buttons
        navigationHelpline.setHelplineButtonsOnClickListener(this);

        return root;
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_audience_poll:
                // Handle audience poll button click
                Toast.makeText(getContext(), "btn_audience_poll clicked", Toast.LENGTH_SHORT).show();
                Log.d("NavView", "btn_audience_poll clicked");
                break;
            case R.id.btn_fifty_fifty:
                Toast.makeText(getContext(), "btn_fifty clicked", Toast.LENGTH_SHORT).show();
                Log.d("NavView", "btn_fifty clicked");

                break;
            case R.id.btn_change_question:
                // Handle change question button click
                Toast.makeText(getContext(), "btn_change_question clicked", Toast.LENGTH_SHORT).show();
                Log.d("NavView", "change question clicked");
                break;
            case R.id.btn_ask_the_expert:
                // Handle ask the expert button click
                Toast.makeText(getContext(), "btn_ask the expert clicked", Toast.LENGTH_SHORT).show();
                Log.d("NavView", "btn_ask the expert clicked");
                break;
        }
    }
}

And here is the relevant code from the home_fragment.xml and nav_layout.xml files:

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="end"
    tools:context=".ui.home.HomeFragment">

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view_right"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="end"
        android:fitsSystemWindows="true">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <include layout="@layout/nav_layout" />

        </LinearLayout>
    </com.google.android.material.navigation.NavigationView>

and here is nav_laout.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/black"
    android:id="@+id/nav_view_right_layout">

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:orientation="vertical">

        <LinearLayout
            android:id="@+id/btn_audience_poll"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:clickable="true">

            <ImageView
            android:id="@+id/ivAudiencePoll"
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:background="@drawable/audience"
            android:layout_gravity="center"
            android:layout_marginTop="4dp"
            android:padding="8dp"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/white"
                android:text="Audience Poll"
                android:textStyle="bold"
                android:textSize="19sp"
                android:layout_gravity="center"/>

        </LinearLayout>

    <LinearLayout
        android:id="@+id/btn_fifty_fifty"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:clickable="true">

        <ImageView
            android:id="@+id/ivFiftyFifty"
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:background="@drawable/fifty"
            android:layout_gravity="center"
            android:layout_marginTop="4dp"
            android:padding="8dp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/white"
            android:text="Fifty Fifty"
            android:textStyle="bold"
            android:textSize="19sp"
            android:layout_gravity="center"/>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/btn_change_question"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:clickable="true">

        <ImageView
            android:id="@+id/ivChangeQuestion"
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:background="@drawable/change_ques"
            android:layout_gravity="center"
            android:layout_marginTop="4dp"
            android:padding="8dp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/white"
            android:text="Flip The Question"
            android:textStyle="bold"
            android:textSize="19sp"
            android:layout_gravity="center"/>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/btn_ask_the_expert"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:clickable="true">

        <ImageView
            android:id="@+id/ivAskTheExpert"
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:background="@drawable/ask_the_expert"
            android:layout_gravity="center"
            android:layout_marginTop="4dp"
            android:padding="8dp"/>

        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/white"
                android:text="Ask The Expert"
                android:textStyle="bold"
                android:textSize="19sp"
                android:layout_gravity="center"/>

        </LinearLayout>
        <TextView
            android:id="@+id/btn_back_to_home"
            android:layout_width="match_parent"
            android:layout_height="25sp"
            android:text="Back to Question"
            android:textSize="16sp"
            android:layout_marginTop="10sp"
            android:textColor="@color/white"
            android:textStyle="bold"
            android:background="@drawable/elliptical_border"
            android:gravity="center"/>

    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

and here is NavigationHelpline class

public class NavigationHelpline {
    private NavigationView navigationView;
    private LinearLayout audiencePollBtn, fiftyFiftyBtn, flipQuestionBtn, askTheExpertBtn;
    private TextView btnBackToHome;


    public NavigationHelpline(NavigationView navigationView) {
        this.navigationView = navigationView;
        View includedLayout = navigationView.findViewById(R.id.nav_view_right_layout);
        audiencePollBtn = includedLayout.findViewById(R.id.btn_audience_poll);
        fiftyFiftyBtn = includedLayout.findViewById(R.id.btn_fifty_fifty);
        flipQuestionBtn = includedLayout.findViewById(R.id.btn_change_question);
        askTheExpertBtn = includedLayout.findViewById(R.id.btn_ask_the_expert);
        btnBackToHome = includedLayout.findViewById(R.id.btn_back_to_home);

    }

    public void setHelplineButtonsOnClickListener(View.OnClickListener listener) {
        audiencePollBtn.setOnClickListener(listener);
        fiftyFiftyBtn.setOnClickListener(listener);
        flipQuestionBtn.setOnClickListener(listener);
        askTheExpertBtn.setOnClickListener(listener);
        btnBackToHome.setOnClickListener(listener);
    }
}

I have tried adding logs to debug the issue, but I cannot see any logs. I also tried removing the NavigationView's default behavior and set the OnClickListener for the Helpline buttons. Still, the onClick method is not working as expected.

Can someone please help me understand why the onClick method is not working and how to fix it?

1 Answers1

1

I think it is not possible to implement click listener as you are adding new layout in your home_fragment.xml using include tag, because default behaviour of navigation view is closing navigation view drawer on any view inside or outside of navigation view. So instead of using layout, use menu with item and then set each item as audience_poll, fifty_fifty, ask_the_expert, flip_the_question.

Joshit
  • 1,238
  • 16
  • 38
  • But I want to use layout because that looks better that way and if I used menu item then it will not give that look which I want. – Pawan yadav Mar 27 '23 at 10:44