I made an android application with two fragments (LaneListFragment and SelectMenuFragment). I'm able to go between the two fragments with view pager. However, I made a button called saveButton and I want to be able to go from SelectMenuFragment to LaneListFragment when it is clicked. I used some code I found on StackOverflow but I don't think I'm using it right. I'm using R.id.viewPager for the replace when I just want to load the fragment_lane_list layout. Can anybody help?
I made an android application with two fragments (LaneListFragment and SelectMenuFragment). I'm able to go between the two fragments with view pager. However, I made a button called saveButton and I want to be able to go from SelectMenuFragment to LaneListFragment when it is clicked. I used some code I found on StackOverflow but I don't think I'm using it right. I'm using R.id.viewPager for the replace when I just want to load the fragment_lane_list layout. Can anybody help?
public class SelectMenuFragment extends Fragment {
private SelectMenuAdapter adapter;
private LaneListFragment laneListFragment;
private boolean[] topSelected = new boolean[5];
private boolean[] bottomSelected = new boolean[7];
private FrameLayout[] topButtons;
private FrameLayout[] bottomButtons;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_select_menu, container, false);
topButtons = new FrameLayout[5];
topButtons[0] = view.findViewById(R.id.icButton1);
bottomButtons = new FrameLayout[7];
bottomButtons[0] = view.findViewById(R.id.icButton6);
setButtonClickListeners();
Button cancelButton = view.findViewById(R.id.cancelButton);
Button saveButton = view.findViewById(R.id.saveButton);
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (topButtons[0].isSelected() && bottomButtons[0].isSelected()) {
Database.icons.add(new Icons("Shoulder","Closed", R.drawable.ic_shoulder, R.drawable.ic_closed, R.drawable.ic_trash));
if (laneListFragment != null) {
laneListFragment.updateListView();
}
}
else {
Toast.makeText(getActivity(), "Nothing Added!",Toast.LENGTH_SHORT).show();
}
Fragment fragment = new LaneListFragment();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.viewPager, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
return view;
}
//fragment_lane_list.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"
tools:context=".LaneListFragment">
<TextView
android:id="@+id/textView4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="74dp"
android:layout_marginBottom="56dp"
android:gravity="center_horizontal"
android:text="The list of reported lane closures"
android:textColor="@color/black"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="@+id/imageView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="29dp"
android:src="@drawable/ic_add_lane"
app:layout_constraintBottom_toTopOf="@+id/textView5"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/textView5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="256dp"
android:gravity="center_horizontal"
android:text="Currently, there is no lane\nclosure history available."
android:textColor="@color/black"
android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView6"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="42dp"
android:gravity="center_horizontal"
android:text="To add a lane closure,\nplease use the tab above."
android:textColor="@color/black"
android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView5" />
</androidx.constraintlayout.widget.ConstraintLayout>
<?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"
tools:context=".MainActivity">
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/my_tabs" />
<com.google.android.material.tabs.TabLayout
android:id="@+id/my_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_to...
</androidx.constraintlayout.widget.ConstraintLayout>
Step by step
Solved in 3 steps