Titlebar changes title(month and year) according to swipe

63 views Asked by At

My titlebar title for navigation drawer shows the current month and year and I want to change the title everytime the user swipes. Let say currently is June 2015; when user swipe left, it will appear as May 2015, when user swipe right, it will show as July 2015. Any idea how to do this? I've gotten the current date and time and shown below:

public class MenuDrawer extends ActionBarActivity implements FragmentDrawer.FragmentDrawerListener {

private static String TAG = MenuDrawer.class.getSimpleName();
private Calendar cal = Calendar.getInstance();
private SimpleDateFormat dateFormatter = new SimpleDateFormat("MMMM yyyy");

private Toolbar mToolbar;
private FragmentDrawer drawerFragment;

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

    mToolbar = (Toolbar) findViewById(R.id.toolbar);

    setSupportActionBar(mToolbar);
    getSupportActionBar().setDisplayShowHomeEnabled(true);

    drawerFragment = (FragmentDrawer) getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
    drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), mToolbar);
    drawerFragment.setDrawerListener(this);

    // display the first navigation drawer view on app launch
    displayView(0);
}

@Override
public void onDrawerItemSelected(View view, int position) {
    displayView(position);
}

private void displayView(int position) {
    Fragment fragment = null;
    String title = getString(R.string.app_name);

    switch (position) {
        case 0:
            fragment = new CalendarFragment();
            // get current month and year
            title = dateFormatter.format(cal.getTime());
            break;

        default: break;
    }

    if (fragment != null) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.container_body, fragment);
        fragmentTransaction.commit();

        // set the toolbar title
        getSupportActionBar().setTitle(title);
    }
}

}

0

There are 0 answers