I have created a tabbed activity to show various information in. However, when creating a layout for the fragment, it does not show up on the application when being tested. The tabs are showing and it allows me to change tabs but the layout below is just a blank screen. I have tried to figure this out for a while now, can anyone help me with this? I will include all the relevant code below.
Java code for the first tab fragment:
package com.example.infocarmation;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* A simple {@link Fragment} subclass.
* Use the {@link CarDetailsReport#newInstance} factory method to
* create an instance of this fragment.
*/
public class CarDetailsReport extends Fragment {
TextView car, reg, colour, engineSize, fuelType, transmission, carType,
doors, seats;
// 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 SharedPreferences sharedPreferences;
public CarDetailsReport() {
// 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 CarDetailsReport.
*/
// TODO: Rename and change types and number of parameters
public static CarDetailsReport newInstance(String param1, String param2)
{
CarDetailsReport fragment = new CarDetailsReport();
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);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
car = car.findViewById(R.id.tvCarReport);
reg = reg.findViewById(R.id.tvRegReport);
colour = colour.findViewById(R.id.tvColourReport);
engineSize = engineSize.findViewById(R.id.tvEngineSizeReport);
fuelType = fuelType.findViewById(R.id.tvFuelTypeReport);
transmission = fuelType.findViewById(R.id.tvTransmissionReport);
carType = carType.findViewById(R.id.tvCarTypeReport);
doors = doors.findViewById(R.id.tvDoorsReport);
seats = seats.findViewById(R.id.tvSeatsReport);
SharedPreferences sharedPreferences =
getActivity().getSharedPreferences("CarDetailsPrefs",
Context.MODE_PRIVATE);
String carStr = sharedPreferences.getString("Car", "");
String regStr = sharedPreferences.getString("Registration", "");
String colourStr = sharedPreferences.getString("Colour", "");
String engineSizeStr = sharedPreferences.getString("Engine Size",
"");
String fuelTypeStr = sharedPreferences.getString("Fuel Type", "");
String transmissionStr = sharedPreferences.getString("Transmission",
"");
String carTypeStr = sharedPreferences.getString("Car Type", "");
String doorsStr = sharedPreferences.getString("Doors", "");
String seatsStr = sharedPreferences.getString("Seats", "");
car.setText(carStr);
reg.setText(regStr);
colour.setText(colourStr);
engineSize.setText(engineSizeStr);
fuelType.setText(fuelTypeStr);
transmission.setText(transmissionStr);
carType.setText(carTypeStr);
doors.setText(doorsStr);
seats.setText(seatsStr);
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_car_details_report,
container, false);
}
}
Code for the first fragment 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=".CarDetailsReport">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="First Fragment" />
<EditText
android:id="@+id/tvCarReport"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:ems="10"
android:hint="Car"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/tvRegReport"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tvCarReport" />
<EditText
android:id="@+id/tvColourReport"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tvRegReport" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="24dp"
android:text="Button"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
This is the java code for the main activity:
package com.example.infocarmation;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.widget.ViewPager2;
import android.os.Bundle;
import com.google.android.material.tabs.TabLayout;
public class Report extends AppCompatActivity {
TabLayout tabLayout;
ViewPager2 pager2;
FragmentAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_report);
tabLayout = findViewById(R.id.tab_layout);
pager2 = findViewById(R.id.view_pager2);
FragmentManager fm = getSupportFragmentManager();
adapter = new FragmentAdapter(fm, getLifecycle());
pager2.setAdapter(adapter);
tabLayout.addTab(tabLayout.newTab().setText("Car Details"));
tabLayout.addTab(tabLayout.newTab().setText("Trip Monitor"));
tabLayout.addTab(tabLayout.newTab().setText("Logbook"));
tabLayout.addTab(tabLayout.newTab().setText("Other Details"));
tabLayout.addOnTabSelectedListener(new
TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
pager2.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
pager2.registerOnPageChangeCallback(new
ViewPager2.OnPageChangeCallback() {
@Override
public void onPageSelected(int position) {
tabLayout.selectTab(tabLayout.getTabAt(position));
}
});
}
}
The java code for the fragment adapter:
package com.example.infocarmation;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.Lifecycle;
import androidx.viewpager2.adapter.FragmentStateAdapter;
public class FragmentAdapter extends FragmentStateAdapter {
public FragmentAdapter(@NonNull FragmentManager fragmentManager, @NonNull
Lifecycle lifecycle) {
super(fragmentManager, lifecycle);
}
@NonNull
@Override
public Fragment createFragment(int position) {
switch(position) {
case 1:
return new TripMonitorReport();
case 2:
return new LogbookFragment();
case 3:
return new OtherDetailsFragment();
}
return new CarDetailsReport();
}
@Override
public int getItemCount() {
return 0;
}
}
And finally, this is the code for the main activity xml which the tabbed fragments are linked to:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".Report">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/view_pager2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/tab_layout" />
</RelativeLayout>
It doesn't really matter about what is in the layout for now, or the java code for the fragment (strings, etc), just how to get the layout to show when in the application.