I'm trying to make an app in android studio that has different tabs using fragments. I want one of these tabs to show a calendar so that when that fragment is opened (using a navigation bar), the calendar appears.
I'm having trouble integrating my calendar activity into the fragment activity file. I'm also new to android studio so I'm a bit lost on what to do.
CalendarActivity:
public class CalendarActivity extends AppCompatActivity implements CalendarAdapter.OnItemListener{
private TextView monthYearText;
private RecyclerView calendarRecyclerView;
private LocalDate selectedDate;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_calendar);
initWidgets();
selectedDate = LocalDate.now();
setMonthView();
// new
CalendarFragment calendarFragment = new CalendarFragment();
getSupportFragmentManager().beginTransaction().add(R.id.bottomNavigationView, calendarFragment).commit();
}
private void initWidgets(){
calendarRecyclerView = findViewById(R.id.calendarRecyclerView);
monthYearText = findViewById(R.id.monthYearTV);
}
private void setMonthView(){
monthYearText.setText(monthYearFromDate(selectedDate));
ArrayList<String> daysInMonth = daysInMonthArray(selectedDate);
CalendarAdapter calendarAdapter = new CalendarAdapter(daysInMonth, this);
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getApplicationContext(), 7);
calendarRecyclerView.setLayoutManager(layoutManager);
calendarRecyclerView.setAdapter(calendarAdapter);
}
private ArrayList<String> daysInMonthArray(LocalDate date){
ArrayList<String> daysInMonthArray = new ArrayList<>();
YearMonth yearMonth = YearMonth.from(date);
int daysInMonth = yearMonth.lengthOfMonth();
LocalDate firstOfMonth = selectedDate.withDayOfMonth(1);
int dayOfWeek = firstOfMonth.getDayOfWeek().getValue();
for(int i = 1; i <= 42; i++){
if(i <= dayOfWeek || i > daysInMonth + dayOfWeek){
daysInMonthArray.add("");
}
else{
daysInMonthArray.add(String.valueOf(i - dayOfWeek));
}
}
return daysInMonthArray;
}
@Override
public void onItemClick(int position, String dayText){
if(!dayText.equals(""))
{
String message = "Selected Date " + dayText + " " + monthYearFromDate(selectedDate);
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}
}
}
CalendarFragment:
public class CalendarFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_calendar, container, false);
}
}
MainActivity:
public class MainActivity extends AppCompatActivity {
ActivityMainBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
replaceFragment(new HomeFragment());
// making home the default highlighted item
BottomNavigationView mBottomNavigationView=(BottomNavigationView)findViewById(R.id.bottomNavigationView);
mBottomNavigationView.getMenu().findItem(R.id.home).setChecked(true);
binding.bottomNavigationView.setOnItemSelectedListener(item -> {
switch (item.getItemId()){
case R.id.home:
replaceFragment(new HomeFragment());
break;
case R.id.scan:
replaceFragment(new ScanFragment());
break;
case R.id.calendar:
replaceFragment(new CalendarFragment());
break;}
return true;
}); }
private void replaceFragment (Fragment fragment){
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frame_layout, fragment);
fragmentTransaction.commit();
}}