title: How to make CardView onClick open video from one fragment without making multiple fragment
body: <p>I want to make a page using <strong>fragment</strong> that has a <strong>CardView</strong>. Let say I have 4 cardviews. I want the cardviews to be clickable and when clicked, they will open a new fragment with a <strong>VideoView</strong>. Can I only make one fragment that can play different video based on which cardview is clicked.</p>
  <p>Meaning, let say, I click on cardview1, it will open a fragment with video1, then i click cardview 2, it will open from the same fragment but with video2. Is it possible to use only one fragment to have more than one videoView?</p>
  <p>If possible how can I do that? someone please help.</p>
  <p><strong>this is my code
  fragment_exercise.xml    (fragment with cardviews)</strong></p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  
  &lt;ScrollView xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      xmlns:tools=&quot;http://schemas.android.com/tools&quot;
      xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
      android:fillViewport=&quot;true&quot;
      tools:context=&quot;.ExerciseFragment&quot;&gt;
  
      &lt;RelativeLayout
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;match_parent&quot;
          android:padding=&quot;5dp&quot;
          android:orientation=&quot;vertical&quot;
          android:background=&quot;#FFF8DD&quot;&gt;
  
          &lt;ImageView
              android:id=&quot;@+id/logo2&quot;
              android:layout_width=&quot;149dp&quot;
              android:layout_height=&quot;48dp&quot;
              android:layout_marginLeft=&quot;10dp&quot;
              android:layout_marginTop=&quot;30dp&quot;
              android:layout_marginRight=&quot;50dp&quot;
              android:src=&quot;@drawable/logobg&quot; /&gt;
  
          &lt;androidx.cardview.widget.CardView
              android:id=&quot;@+id/c1&quot;
              android:layout_width=&quot;130dp&quot;
              android:layout_height=&quot;130dp&quot;
              android:layout_marginTop=&quot;100dp&quot;
              android:layout_marginLeft=&quot;50dp&quot;
              app:cardCornerRadius=&quot;26dp&quot;
              app:cardElevation=&quot;10dp&quot;&gt;
  
              &lt;RelativeLayout
                  android:layout_width=&quot;wrap_content&quot;
                  android:layout_height=&quot;match_parent&quot;
                  android:background=&quot;@drawable/gradient16&quot;
                  android:gravity=&quot;center&quot;
                  android:orientation=&quot;vertical&quot; &gt;
  
                  &lt;TextView
                      android:layout_width=&quot;wrap_content&quot;
                      android:layout_height=&quot;wrap_content&quot;
                      android:layout_gravity=&quot;center&quot;
                      android:layout_marginLeft=&quot;4dp&quot;
                      android:layout_marginTop=&quot;80dp&quot;
                      android:fontFamily=&quot;@font/poppins&quot;
                      android:text=&quot;Running&quot;
                      android:textAlignment=&quot;center&quot;
                      android:textColor=&quot;@color/white&quot;
                      android:textSize=&quot;18dp&quot;
                      android:textStyle=&quot;bold&quot; /&gt;
              &lt;/RelativeLayout&gt;
  
              &lt;ImageView
                  android:layout_width=&quot;102dp&quot;
                  android:layout_height=&quot;88dp&quot;
                  android:layout_marginTop=&quot;10dp&quot;
                  android:layout_marginLeft=&quot;10dp&quot;
                  android:src=&quot;@drawable/running&quot; /&gt;
  
          &lt;/androidx.cardview.widget.CardView&gt;
  
          &lt;androidx.cardview.widget.CardView
              android:id=&quot;@+id/c2&quot;
              android:layout_width=&quot;130dp&quot;
              android:layout_height=&quot;130dp&quot;
              android:layout_marginTop=&quot;100dp&quot;
              android:layout_marginLeft=&quot;220dp&quot;
              app:cardCornerRadius=&quot;26dp&quot;
              app:cardElevation=&quot;10dp&quot;&gt;
  
              &lt;RelativeLayout
                  android:layout_width=&quot;wrap_content&quot;
                  android:layout_height=&quot;match_parent&quot;
                  android:background=&quot;@drawable/gradient3&quot;
                  android:gravity=&quot;center&quot;
                  android:orientation=&quot;vertical&quot; &gt;
  
                  &lt;TextView
                      android:layout_width=&quot;wrap_content&quot;
                      android:layout_height=&quot;wrap_content&quot;
                      android:layout_gravity=&quot;center&quot;
                      android:layout_marginLeft=&quot;4dp&quot;
                      android:layout_marginTop=&quot;80dp&quot;
                      android:fontFamily=&quot;@font/poppins&quot;
                      android:text=&quot;Walking&quot;
                      android:textAlignment=&quot;center&quot;
                      android:textColor=&quot;@color/white&quot;
                      android:textSize=&quot;18dp&quot;
                      android:textStyle=&quot;bold&quot; /&gt;
              &lt;/RelativeLayout&gt;
  
              &lt;ImageView
                  android:layout_width=&quot;102dp&quot;
                  android:layout_height=&quot;88dp&quot;
                  android:layout_marginTop=&quot;10dp&quot;
                  android:layout_marginLeft=&quot;10dp&quot;
                  android:src=&quot;@drawable/walking&quot; /&gt;
  
          &lt;/androidx.cardview.widget.CardView&gt;
  
          &lt;androidx.cardview.widget.CardView
              android:id=&quot;@+id/c3&quot;
              android:layout_width=&quot;130dp&quot;
              android:layout_height=&quot;130dp&quot;
              android:layout_marginTop=&quot;250dp&quot;
              android:layout_marginLeft=&quot;50dp&quot;
              app:cardCornerRadius=&quot;26dp&quot;
              app:cardElevation=&quot;10dp&quot;&gt;
  
              &lt;RelativeLayout
                  android:layout_width=&quot;wrap_content&quot;
                  android:layout_height=&quot;match_parent&quot;
                  android:background=&quot;@drawable/gradient22&quot;
                  android:gravity=&quot;center&quot;
                  android:orientation=&quot;vertical&quot; &gt;
  
                  &lt;TextView
                      android:layout_width=&quot;wrap_content&quot;
                      android:layout_height=&quot;wrap_content&quot;
                      android:layout_gravity=&quot;center&quot;
                      android:layout_marginLeft=&quot;4dp&quot;
                      android:layout_marginTop=&quot;80dp&quot;
                      android:fontFamily=&quot;@font/poppins&quot;
                      android:text=&quot;Aerobics&quot;
                      android:textAlignment=&quot;center&quot;
                      android:textColor=&quot;@color/white&quot;
                      android:textSize=&quot;18dp&quot;
                      android:textStyle=&quot;bold&quot; /&gt;
              &lt;/RelativeLayout&gt;
  
              &lt;ImageView
                  android:layout_width=&quot;102dp&quot;
                  android:layout_height=&quot;88dp&quot;
                  android:layout_marginTop=&quot;10dp&quot;
                  android:layout_marginLeft=&quot;10dp&quot;
                  android:src=&quot;@drawable/aerobatic&quot; /&gt;
  
          &lt;/androidx.cardview.widget.CardView&gt;
  
          &lt;androidx.cardview.widget.CardView
              android:id=&quot;@+id/c4&quot;
              android:layout_width=&quot;130dp&quot;
              android:layout_height=&quot;130dp&quot;
              android:layout_marginTop=&quot;250dp&quot;
              android:layout_marginLeft=&quot;220dp&quot;
              app:cardCornerRadius=&quot;26dp&quot;
              app:cardElevation=&quot;10dp&quot;&gt;
  
              &lt;RelativeLayout
                  android:layout_width=&quot;wrap_content&quot;
                  android:layout_height=&quot;match_parent&quot;
                  android:background=&quot;@drawable/gradient13&quot;
                  android:gravity=&quot;center&quot;
                  android:orientation=&quot;vertical&quot; &gt;
  
                  &lt;TextView
                      android:layout_width=&quot;wrap_content&quot;
                      android:layout_height=&quot;wrap_content&quot;
                      android:layout_gravity=&quot;center&quot;
                      android:layout_marginLeft=&quot;4dp&quot;
                      android:layout_marginTop=&quot;80dp&quot;
                      android:fontFamily=&quot;@font/poppins&quot;
                      android:text=&quot;Yoga&quot;
                      android:textAlignment=&quot;center&quot;
                      android:textColor=&quot;@color/white&quot;
                      android:textSize=&quot;18dp&quot;
                      android:textStyle=&quot;bold&quot; /&gt;
              &lt;/RelativeLayout&gt;
  
              &lt;ImageView
                  android:layout_width=&quot;102dp&quot;
                  android:layout_height=&quot;88dp&quot;
                  android:layout_marginTop=&quot;10dp&quot;
                  android:layout_marginLeft=&quot;10dp&quot;
                  android:src=&quot;@drawable/yoga2&quot; /&gt;
  
          &lt;/androidx.cardview.widget.CardView&gt;
  
          &lt;androidx.cardview.widget.CardView
              android:id=&quot;@+id/c5&quot;
              android:layout_width=&quot;130dp&quot;
              android:layout_height=&quot;130dp&quot;
              android:layout_marginTop=&quot;400dp&quot;
              android:layout_marginLeft=&quot;50dp&quot;
              app:cardCornerRadius=&quot;26dp&quot;
              app:cardElevation=&quot;10dp&quot;&gt;
  
              &lt;RelativeLayout
                  android:layout_width=&quot;wrap_content&quot;
                  android:layout_height=&quot;match_parent&quot;
                  android:background=&quot;@drawable/gradient29&quot;
                  android:gravity=&quot;center&quot;
                  android:orientation=&quot;vertical&quot; &gt;
  
                  &lt;TextView
                      android:layout_width=&quot;wrap_content&quot;
                      android:layout_height=&quot;wrap_content&quot;
                      android:layout_gravity=&quot;center&quot;
                      android:layout_marginLeft=&quot;4dp&quot;
                      android:layout_marginTop=&quot;80dp&quot;
                      android:fontFamily=&quot;@font/poppins&quot;
                      android:text=&quot;Boxing&quot;
                      android:textAlignment=&quot;center&quot;
                      android:textColor=&quot;@color/white&quot;
                      android:textSize=&quot;18dp&quot;
                      android:textStyle=&quot;bold&quot; /&gt;
              &lt;/RelativeLayout&gt;
  
              &lt;ImageView
                  android:layout_width=&quot;102dp&quot;
                  android:layout_height=&quot;88dp&quot;
                  android:layout_marginTop=&quot;10dp&quot;
                  android:layout_marginLeft=&quot;10dp&quot;
                  android:src=&quot;@drawable/boxing&quot; /&gt;
  
          &lt;/androidx.cardview.widget.CardView&gt;
  
          &lt;androidx.cardview.widget.CardView
              android:id=&quot;@+id/c6&quot;
              android:layout_width=&quot;130dp&quot;
              android:layout_height=&quot;130dp&quot;
              android:layout_marginTop=&quot;400dp&quot;
              android:layout_marginLeft=&quot;220dp&quot;
              app:cardCornerRadius=&quot;26dp&quot;
              app:cardElevation=&quot;10dp&quot;&gt;
  
              &lt;RelativeLayout
                  android:layout_width=&quot;wrap_content&quot;
                  android:layout_height=&quot;match_parent&quot;
                  android:background=&quot;@drawable/gradient10&quot;
                  android:gravity=&quot;center&quot;
                  android:orientation=&quot;vertical&quot; &gt;
  
                  &lt;TextView
                      android:layout_width=&quot;wrap_content&quot;
                      android:layout_height=&quot;wrap_content&quot;
                      android:layout_gravity=&quot;center&quot;
                      android:layout_marginLeft=&quot;4dp&quot;
                      android:layout_marginTop=&quot;80dp&quot;
                      android:fontFamily=&quot;@font/poppins&quot;
                      android:text=&quot;Cycling&quot;
                      android:textAlignment=&quot;center&quot;
                      android:textColor=&quot;@color/white&quot;
                      android:textSize=&quot;18dp&quot;
                      android:textStyle=&quot;bold&quot; /&gt;
              &lt;/RelativeLayout&gt;
  
              &lt;ImageView
                  android:layout_width=&quot;102dp&quot;
                  android:layout_height=&quot;88dp&quot;
                  android:layout_marginTop=&quot;10dp&quot;
                  android:layout_marginLeft=&quot;10dp&quot;
                  android:src=&quot;@drawable/biking&quot; /&gt;
  
          &lt;/androidx.cardview.widget.CardView&gt;
  
          &lt;androidx.cardview.widget.CardView
              android:id=&quot;@+id/c7&quot;
              android:layout_width=&quot;130dp&quot;
              android:layout_height=&quot;130dp&quot;
              android:layout_marginTop=&quot;550dp&quot;
              android:layout_marginLeft=&quot;50dp&quot;
              app:cardCornerRadius=&quot;26dp&quot;
              app:cardElevation=&quot;10dp&quot;&gt;
  
              &lt;RelativeLayout
                  android:layout_width=&quot;wrap_content&quot;
                  android:layout_height=&quot;match_parent&quot;
                  android:background=&quot;@drawable/gradient24&quot;
                  android:gravity=&quot;center&quot;
                  android:orientation=&quot;vertical&quot; &gt;
  
                  &lt;TextView
                      android:layout_width=&quot;wrap_content&quot;
                      android:layout_height=&quot;wrap_content&quot;
                      android:layout_gravity=&quot;center&quot;
                      android:layout_marginLeft=&quot;4dp&quot;
                      android:layout_marginTop=&quot;80dp&quot;
                      android:fontFamily=&quot;@font/poppins&quot;
                      android:text=&quot;Dancing&quot;
                      android:textAlignment=&quot;center&quot;
                      android:textColor=&quot;@color/white&quot;
                      android:textSize=&quot;18dp&quot;
                      android:textStyle=&quot;bold&quot; /&gt;
              &lt;/RelativeLayout&gt;
  
              &lt;ImageView
                  android:layout_width=&quot;102dp&quot;
                  android:layout_height=&quot;88dp&quot;
                  android:layout_marginTop=&quot;10dp&quot;
                  android:layout_marginLeft=&quot;10dp&quot;
                  android:src=&quot;@drawable/dance&quot; /&gt;
  
          &lt;/androidx.cardview.widget.CardView&gt;
  
          &lt;androidx.cardview.widget.CardView
              android:id=&quot;@+id/c8&quot;
              android:layout_width=&quot;130dp&quot;
              android:layout_height=&quot;130dp&quot;
              android:layout_marginTop=&quot;550dp&quot;
              android:layout_marginLeft=&quot;220dp&quot;
              app:cardCornerRadius=&quot;26dp&quot;
              app:cardElevation=&quot;10dp&quot;&gt;
  
              &lt;RelativeLayout
                  android:layout_width=&quot;wrap_content&quot;
                  android:layout_height=&quot;match_parent&quot;
                  android:background=&quot;@drawable/gradient30&quot;
                  android:gravity=&quot;center&quot;
                  android:orientation=&quot;vertical&quot; &gt;
  
                  &lt;TextView
                      android:layout_width=&quot;wrap_content&quot;
                      android:layout_height=&quot;wrap_content&quot;
                      android:layout_gravity=&quot;center&quot;
                      android:layout_marginLeft=&quot;4dp&quot;
                      android:layout_marginTop=&quot;80dp&quot;
                      android:fontFamily=&quot;@font/poppins&quot;
                      android:text=&quot;Swimming&quot;
                      android:textAlignment=&quot;center&quot;
                      android:textColor=&quot;@color/white&quot;
                      android:textSize=&quot;18dp&quot;
                      android:textStyle=&quot;bold&quot; /&gt;
              &lt;/RelativeLayout&gt;
  
              &lt;ImageView
                  android:layout_width=&quot;102dp&quot;
                  android:layout_height=&quot;88dp&quot;
                  android:layout_marginTop=&quot;10dp&quot;
                  android:layout_marginLeft=&quot;10dp&quot;
                  android:src=&quot;@drawable/m2&quot; /&gt;
  
          &lt;/androidx.cardview.widget.CardView&gt;
  
      &lt;/RelativeLayout&gt;
  
  
  &lt;/ScrollView&gt;
  </code></pre>
  <p><strong>ExerciseFragment.java</strong></p>
  <pre><code>public class ExerciseFragment extends Fragment implements View.OnClickListener{
  
      View v;
      CardView cv1, cv2, cv3, cv4, cv5, cv6, cv7, cv8;
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
  
  
          View v = inflater.inflate(R.layout.fragment_exercise, container, false);
  
          cv1 = (CardView) v.findViewById(R.id.c1);
          cv1.setOnClickListener(this);
          cv2 = (CardView) v.findViewById(R.id.c2);
          cv2.setOnClickListener(this);
          cv3 = (CardView) v.findViewById(R.id.c3);
          cv3.setOnClickListener(this);
          cv4 = (CardView) v.findViewById(R.id.c4);
          cv4.setOnClickListener(this);
          cv5 = (CardView) v.findViewById(R.id.c5);
          cv5.setOnClickListener(this);
          cv6 = (CardView) v.findViewById(R.id.c6);
          cv6.setOnClickListener(this);
          cv7 = (CardView) v.findViewById(R.id.c7);
          cv7.setOnClickListener(this);
          cv8 = (CardView) v.findViewById(R.id.c8);
          cv8.setOnClickListener(this);
  
  
          // Inflate the layout for this fragment
          return v;
      }
  
      @Override
      public void onClick(View v) {
          Fragment fragment1, fragment2, fragment3, fragment4, fragment5, fragment6, fragment7, fragment8;
          switch (v.getId()) {
              case R.id.c1:
                  fragment1 = new VideoFragment();
                  FragmentTransaction transaction1 = getParentFragmentManager().beginTransaction();
                  transaction1.replace(R.id.container,fragment1);
                  transaction1.commit();
                  break;
              case R.id.c2:
                  fragment2 = new VideoFragment();
                  FragmentTransaction transaction2 = getParentFragmentManager().beginTransaction();
                  transaction2.replace(R.id.container,fragment2);
                  transaction2.commit();
                  break;
              case R.id.c3:
                  fragment3 = new VideoFragment();
                  FragmentTransaction transaction3 = getParentFragmentManager().beginTransaction();
                  transaction3.replace(R.id.container,fragment3);
                  transaction3.commit();
                  break;
              case R.id.c4:
                  fragment4 = new VideoFragment();
                  FragmentTransaction transaction4 = getParentFragmentManager().beginTransaction();
                  transaction4.replace(R.id.container,fragment4);
                  transaction4.commit();
                  break;
              case R.id.c5:
                  fragment5 = new VideoFragment();
                  FragmentTransaction transaction5 = getParentFragmentManager().beginTransaction();
                  transaction5.replace(R.id.container,fragment5);
                  transaction5.commit();
                  break;
              case R.id.c6:
                  fragment6 = new VideoFragment();
                  FragmentTransaction transaction6 = getParentFragmentManager().beginTransaction();
                  transaction6.replace(R.id.container,fragment6);
                  transaction6.commit();
                  break;
              case R.id.c7:
                  fragment7 = new VideoFragment();
                  FragmentTransaction transaction7 = getParentFragmentManager().beginTransaction();
                  transaction7.replace(R.id.container,fragment7);
                  transaction7.commit();
                  break;
              case R.id.c8:
                  fragment8 = new VideoFragment();
                  FragmentTransaction transaction8 = getParentFragmentManager().beginTransaction();
                  transaction8.replace(R.id.container,fragment8);
                  transaction8.commit();
                  break;
          }
      }
  }
  </code></pre>
  <p><strong>fragment_video.xml    (fragment with videoview)</strong></p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;FrameLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:tools=&quot;http://schemas.android.com/tools&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      tools:context=&quot;.VideoFragment&quot;&gt;
  
      &lt;VideoView
          android:id=&quot;@+id/videoView1&quot;
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:layout_alignParentLeft=&quot;true&quot;
          android:layout_centerVertical=&quot;true&quot; /&gt;
  
  &lt;/FrameLayout&gt;
  </code></pre>
  <p><strong>VideoFragment.java
  this is what i have for now</strong></p>
  <pre><code>public class VideoFragment extends Fragment {
  
       View v;
       VideoView view;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          view = (VideoView) v.findViewById(R.id.videoView1);
  
          MediaController mediaController = new MediaController(this);
          mediaController.setAnchorView(view);
  
          Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath()+&quot;/raw/boxing.mp4&quot;);
          view.setMediaController(mediaController);
          view.setVideoURI(uri);
          view.requestFocus();
          view.start();
  
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
          // Inflate the layout for this fragment
          View v = inflater.inflate(R.layout.fragment_exercise, container, false);
  
          return v;
      }
  }
  </code></pre>
  <p><strong>this code has error at line:</strong></p>
  <pre><code>MediaController mediaController = new MediaController(this);
  </code></pre>
  <p>please help, how do i make one fragment that will play different videos based on the clicked cardviews without having to make multiple fragments. Is it possible, if yes please help write the code for me. Thank you so much.</p>