title: How use OnClickListener in Fragments
body: <p>This is my code. I'm still learning android studio. I want to show a toast when the button is clicked but when I run the application and click the button it does nothing. (this is a Fragment). Does anyone know how to fix this? I tried putting "onClick" method in the xml(<code>android:onClick="onClick"</code>) then the app crashed as soon as I clicked the button.</p>
  
  <p><strong>fragment_fragment_course.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;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"
      tools:context=".fragmentCourse"&gt;
  
      &lt;TextView
          android:id="@+id/textView2"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:text="Course"
          android:textSize="25sp"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toTopOf="parent" /&gt;
  
      &lt;Button
          android:id="@+id/btnCourse"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Button"
          app:layout_constraintBottom_toBottomOf="parent"
          app:layout_constraintEnd_toEndOf="@+id/textView2"
          app:layout_constraintStart_toStartOf="@+id/textView2"
          app:layout_constraintTop_toTopOf="parent" /&gt;
  
  &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  </code></pre>
  
  <p><strong>fragmentCourse.java</strong></p>
  
  <pre><code>package com.example.sma;
  
  import android.os.Bundle;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.Button;
  import android.widget.TextView;
  import android.widget.Toast;
  
  import androidx.fragment.app.Fragment;
  
  
  public class fragmentCourse extends Fragment implements View.OnClickListener {
  
      Button btn2;
  
      public fragmentCourse() {
          // Required empty public constructor
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
  
          View CView = inflater.inflate(R.layout.fragment_fragment_course, container, false);
  
          btn2 = (Button)CView.findViewById(R.id.btnCourse);
          final TextView txt = (TextView)CView.findViewById(R.id.textView2);
  
          btn2.setOnClickListener(this);
  
          return CView;
      }
  
      @Override
      public void onClick(View v) {
  
          switch (v.getId()) {
              case R.id.btnCourse:
                  Toast.makeText(getActivity(), "done", Toast.LENGTH_SHORT).show();
                  break;
  
          }
      }
  }
  </code></pre>