Question: Android Studio help with ViewPager and ListView. I created a quiz4 button in work fragment. I want to click on the button and open Quiz4Activity.

Android Studio help with ViewPager and ListView. I created a quiz4 button in work fragment. I want to click on the button and open Quiz4Activity. Link to a demo video below. Thanks

https://www.youtube.com/watch?v=B7csDJQqte8

My WorkFragment.java code below

public class WorkFragment extends DialogFragment { Button button9; // TODO: Rename parameter arguments, choose names that match  // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER  private static final String ARG_PARAM1 = "param1"; private static final String ARG_PARAM2 = "param2"; // TODO: Rename and change types of parameters  private String mParam1; private String mParam2; public WorkFragment() { // Required empty public constructor  } /**  * Use this factory method to create a new instance of  * this fragment using the provided parameters.  *  * @param param1 Parameter 1.  * @param param2 Parameter 2.  * @return A new instance of fragment WorkFragment.  */  // TODO: Rename and change types and number of parameters  public static WorkFragment newInstance(String param1, String param2) { WorkFragment fragment = new WorkFragment(); Bundle args = new Bundle(); args.putString(ARG_PARAM1, param1); args.putString(ARG_PARAM2, param2); fragment.setArguments(args); return fragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { mParam1 = getArguments().getString(ARG_PARAM1); mParam2 = getArguments().getString(ARG_PARAM2); } } @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_work, container, false); button9 = (Button)view.findViewById(R.id.button9); button9.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v){ final Dialog dialog = new Dialog(getActivity()); dialog.setContentView(R.layout.custom_dialog_quiz3); dialog.setTitle("Quiz"); dialog.show(); Button button_cancel = (Button) dialog.findViewById(R.id.button_cancel); button_cancel.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v){ dialog.dismiss(); } }); final RadioButton rbutton_yes = (RadioButton)dialog.findViewById(R.id.button_yes); final RadioButton rbutton_no = (RadioButton)dialog.findViewById(R.id.button_no); final RadioButton rbutton_exit = (RadioButton)dialog.findViewById(R.id.button_exit); final RadioGroup radioGroup = (RadioGroup)dialog.findViewById(R.id.rgroup); Button button_ok = (Button)dialog.findViewById(R.id.button_ok); button_ok.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v){ if (radioGroup.getCheckedRadioButtonId()==-1){ }else{ if (rbutton_yes.isChecked()) { Toast.makeText(getActivity(),"Yes",Toast.LENGTH_SHORT).show(); dialog.dismiss(); } else if (rbutton_no.isChecked()){ Toast.makeText(getActivity(),"No",Toast.LENGTH_SHORT).show(); dialog.dismiss(); } else if (rbutton_exit.isChecked()) { Toast.makeText(getActivity(),"Exit",Toast.LENGTH_SHORT).show(); dialog.dismiss(); getActivity().finish(); } } } }); } }); return view; // Inflate the layout for this fragment // return inflater.inflate(R.layout.fragment_work, container, false);  } } 

My fragment_work.xml code below

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  xmlns:tools="http://schemas.android.com/tools"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:background="@android:color/black"    <LinearLayout  android:orientation="vertical"  android:layout_width="match_parent"  android:layout_height="match_parent"> <TextView  android:layout_width="match_parent"  android:layout_height="35dp"  android:textAlignment="center"  android:text="This fragment is used for quiz!"  android:textColor="@color/white"  android:background="@android:color/holo_blue_dark"  android:layout_alignParentTop="true"  android:layout_alignParentStart="true" /> <LinearLayout  android:layout_width="match_parent"  android:layout_height="match_parent"> <Button  android:id="@+id/button7"  android:layout_width="90dp"  android:layout_height="wrap_content"  android:text="Quiz1"  android:layout_alignParentTop="true" /> <Button  android:id="@+id/button8"  android:layout_width="90dp"  android:layout_height="wrap_content"  android:layout_alignTop="@+id/button7"  android:layout_centerHorizontal="true"  android:text="Quiz2" /> <Button  android:id="@+id/button9"  android:layout_width="90dp"  android:layout_height="wrap_content"  android:layout_alignParentEnd="true"  android:layout_alignTop="@+id/button8"  android:text="Quiz3" /> <Button  android:id="@+id/quiz4_button"  android:layout_width="90dp"  android:layout_height="wrap_content"  android:layout_alignParentEnd="true"  android:layout_alignTop="@+id/button8"  android:text="Quiz4" /> LinearLayout> <Button  android:id="@+id/button10"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:layout_alignParentEnd="true"  android:layout_alignParentStart="true"  android:layout_below="@+id/button8"  android:layout_marginTop="13dp"  android:background="@android:color/holo_blue_bright"  android:backgroundTint="@android:color/holo_blue_dark"  android:text="Submit"  android:textSize="15sp" /> LinearLayout> <RelativeLayout  android:layout_width="match_parent"  android:layout_height="match_parent"> <TextView  android:id="@+id/textView6"  android:layout_width="match_parent"  android:layout_height="35dp"  android:layout_centerHorizontal="true"  android:text="You can do it!"  android:textColor="@android:color/white"  android:gravity="center"  android:background="@android:color/holo_blue_dark"  android:layout_alignParentBottom="true" /> RelativeLayout> FrameLayout> 

My Quiz4Activity.java code below

public class Quiz4Activity extends BaseActivity { @BindView(R.id.activity_quiz4_advance) ListView lv; private ViewPager viewPager; private ArrayList fragmentlist = new ArrayList(); // private RedFragment mFragment;   @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_quiz4); ButterKnife.bind(this); ArrayList list = new ArrayList<>(); list.add("Hello"); list.add("Hello, how are you?"); list.add("I am fine, thank you!"); list.add("Hello"); list.add("Hello"); list.add("Hello"); list.add("Hello"); list.add("Hello"); list.add("Hello"); list.add("Hello"); list.add("Hello"); list.add("Hello"); list.add("Hello"); AdvanceListViewAdapter adapter = new AdvanceListViewAdapter(this, list); lv.setAdapter(adapter); viewPager = (ViewPager) findViewById(R.id.activity_quiz4_view_pager); fragmentlist.add(new RedFragment()); fragmentlist.add(new GreenFragment()); fragmentlist.add(new BlueFragment()); BaseViewPagerAdapter pagerAdapter = new BaseViewPagerAdapter(getSupportFragmentManager(),fragmentlist); viewPager.setAdapter(pagerAdapter); viewPager.setCurrentItem(0); TextView tv1 = new TextView(this); tv1.setText("FooterView"); tv1.setTextSize(50); lv.addFooterView(tv1); lv.setOnItemClickListener(new AdapterView.OnItemClickListener(){ @Override public void onItemClick(AdapterView parent, View view, int position, long id){ showToast(String.valueOf(position)); } }); } } 

My activity_quiz4.xml code below

xml version="1.0" encoding="utf-8"?> <LinearLayout 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:orientation="vertical" <LinearLayout  android:layout_width="match_parent"  android:layout_height="200dp"> <android.support.v4.view.ViewPager  android:id="@+id/activity_quiz4_view_pager"  android:layout_width="match_parent"  android:layout_height="match_parent">android.support.v4.view.ViewPager> LinearLayout> <ListView  android:id="@+id/activity_quiz4_advance"  android:layout_width="match_parent"  android:layout_height="match_parent"> ListView> LinearLayout> 

My BaseActivity.java code below

public class BaseActivity extends AppCompatActivity { public void showToast(String s){ Toast.makeText(this, s, Toast.LENGTH_SHORT).show(); } public void shortToast(String s){ Toast.makeText(this, s, Toast.LENGTH_SHORT).show(); } public void longToast(String s){ Toast.makeText(this, s, Toast.LENGTH_SHORT).show(); } public void goToActivity(Class c){ Intent intent = new Intent(this, c); startActivity(intent); } } 

My BlueFragment.java code below

public class BlueFragment extends Fragment { // TODO: Rename parameter arguments, choose names that match  // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER  private static final String ARG_PARAM1 = "param1"; private static final String ARG_PARAM2 = "param2"; // TODO: Rename and change types of parameters  private String mParam1; private String mParam2; public BlueFragment() { // Required empty public constructor  } /**  * Use this factory method to create a new instance of  * this fragment using the provided parameters.  *  * @param param1 Parameter 1.  * @param param2 Parameter 2.  * @return A new instance of fragment BlueFragment.  */  // TODO: Rename and change types and number of parameters  public static BlueFragment newInstance(String param1, String param2) { BlueFragment fragment = new BlueFragment(); Bundle args = new Bundle(); args.putString(ARG_PARAM1, param1); args.putString(ARG_PARAM2, param2); fragment.setArguments(args); return fragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { mParam1 = getArguments().getString(ARG_PARAM1); mParam2 = getArguments().getString(ARG_PARAM2); } UtilLog.d("Fragment", "BlueOnCreate"); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment  return inflater.inflate(R.layout.fragment_blue, container, false); } @Override public void onStart() { super.onStart(); UtilLog.d("Fragment", "Blue:onStart"); } @Override public void onResume() { super.onResume(); UtilLog.d("Fragment", "Blue:onResume"); } @Override public void onPause() { super.onPause(); UtilLog.d("Fragment", "Blue:onPause"); } @Override public void onStop() { super.onStop(); UtilLog.d("Fragment", "Blue:onStop"); } @Override public void onDestroy() { super.onDestroy(); UtilLog.d("Fragment", "Blue:onDestroy"); } } 

My UtilLog.java code below

public class UtilLog { private static boolean isLog = false; //static means one instance or value   //create method to control log variable  public static void setIsLog(boolean b){ isLog = b; } //static while using class log   public static void d(String tag, String msg){ if(isLog){ Log.d(tag, msg); } } public static void v(String tag, String msg){ if(isLog){ Log.v(tag, msg); //to create more than one method  } } } 

My fragment_blue.xml code below

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  xmlns:tools="http://schemas.android.com/tools"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:background="@android:color/holo_blue_light"   <TextView  android:layout_width="match_parent"  android:layout_height="match_parent"  android:text="@string/hello_blank_fragment" /> FrameLayout> 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!