Search code examples
androidandroid-fragmentsandroid-tabsdayofweek

How to open another tab (fragment) if today a specific day?


I am developing an app with fragment tabs, and I want to open another tab, when the activity is started. If today is Monday - open first tab, if today is Tuesday open second tab and etc. How I can do that?

My fragmentactivity.java:

public class FragmentActivity extends AppCompatActivity {

    private Toolbar toolbar;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    SharedPreferences sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragmentactivity);

        sp = PreferenceManager.getDefaultSharedPreferences(this);
        String select_class = sp.getString("select_class", "");

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        //getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        viewPager = (ViewPager) findViewById(R.id.viewpager);
        setupViewPager(viewPager);

        tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);
    }

    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFragment(new MondayFragment(), "ПН");
        adapter.addFragment(new TuesdayFragment(), "ВТ");
        adapter.addFragment(new WednesdayFragment(), "СР");
        adapter.addFragment(new ThursdayFragment(), "ЧТ");
        adapter.addFragment(new FridayFragment(), "ПТ");
        viewPager.setAdapter(adapter);
    }

    class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        public ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        public void addFragment(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }
    }
}

Solution

  • First, get the day of the week using Calendar:

    Calendar c = Calendar.getInstance();
    int date = c.get(Calendar.DAY_OF_WEEK);
    

    Now, for determining which tab to open, it is easiest done with a switch. You can either say case Calendar.SUNDAY:, or case 1:. They are both the same, but Calendar.SUNDAY is easier because you don't need to know the numbers of the days

    switch(date){
    
    case Calendar.SUNDAY:
        //create tab
        break;
    
    case Calendar.MONDAY:
        //create tab
        break;
    and so on...
    
    }
    

    This is a simple way to get the day and create tabs based on input. The best way to call this code is to call it when the app is opened, and you should check if it has been called before if you only want it to call once. For that purpose, save the value that checks if you have opened the "tab of the day" before.

    Checking if you have opened the tab before

    There are 4 different options for saving values as of writing this answer:

    Internal/external

    SQLite

    Shared preferences

    Day numbers:

    Copied from the Calendar class. No need to add these lines to your project:

    /**
    * Value of the {@code DAY_OF_WEEK} field indicating Sunday.
    */
    public static final int SUNDAY = 1;
    
    
    
    /**
     * Value of the {@code DAY_OF_WEEK} field indicating Monday.
     */
    public static final int MONDAY = 2;
    
    /**
     * Value of the {@code DAY_OF_WEEK} field indicating Tuesday.
     */
    public static final int TUESDAY = 3;
    
    /**
     * Value of the {@code DAY_OF_WEEK} field indicating Wednesday.
     */
    public static final int WEDNESDAY = 4;
    
    /**
     * Value of the {@code DAY_OF_WEEK} field indicating Thursday.
     */
    public static final int THURSDAY = 5;
    
    /**
     * Value of the {@code DAY_OF_WEEK} field indicating Friday.
     */
    public static final int FRIDAY = 6;
    
    /**
     * Value of the {@code DAY_OF_WEEK} field indicating Saturday.
     */
    public static final int SATURDAY = 7;