Titlebar changes title(month and year) according to swipe - android

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);
}
}
}

Related

How to upgrade old Navigation Drawer Fragment code to the code using toolbar instead of Action bar?

1.Toolbar is declared in XML and included in the concerned XML
restoreActionbar function given in the code (at the bottom) is midified as per the net instruction for Actionbar to Toolbar still it is not working.
This is the Standard Navigation Drawer Fragment code provided earlier for menu option. Is there an example code where it is upgraded to the toolbar from Action bar.
Firstly tried in OnCreate method (commented in following code) - not working, Tried to declare it globally(commented in the following code) not working.
Please help
public class MainActivity1 extends AppCompatActivity implements
NavigationDrawerFragment.NavigationDrawerCallbacks {
/**
* Fragment managing the behaviors, interactions and presentation of the
* navigation drawer.
*/
private NavigationDrawerFragment mNavigationDrawerFragment;
private ShareActionProvider mShareActionProvider;
/**
* Used to store the last screen title. For use in
* {#link #restoreActionBar()}.
*/
private CharSequence mTitle;
//private int mPosition = 0;
private String mMessage;
//private int mSectionNo =0;
private String mMenuType="";
private Random r1 = new Random();
private Random r2 = new Random();
Globals g = Globals.getInstance();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Globals g = Globals.getInstance();
//mSectionNo = g.getData();
mMenuType = g.getMenuType();
//Intent intent = getIntent();
//mMessage = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
setContentView(R.layout.activity_main_activity1);
mNavigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager()
.findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
/*if (mMessage.equals("1")){
mNavigationDrawerFragment.mSectionNo = 1;
}*/
// Set up the drawer.
mNavigationDrawerFragment.setUp(R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
int radioId=sharedPreferences.getInt("shapecheck", 0);
if(radioId>0){
g.mShapeOption = 1;
}
//Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
//setSupportActionBar(toolbar);
}
#Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
//mPosition = position;
Globals g = Globals.getInstance();
g.setSectionType(position);
if ((mMenuType.equals("2to10"))||(mMenuType.equals("11to20"))||(mMenuType.equals("21to30")))
{
if(position==10){
this.finish();} // to exit
}
if ((mMenuType.equals("randomizer")) ||(mMenuType.equals("sequential"))||(mMenuType.equals("quiz"))||(mMenuType.equals("query"))){
if(position==4){
this.finish();
}
}
if ((mMenuType.equals("about")) || (mMenuType.equals("shapeoption"))){
if(position==1){
this.finish();
}
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager
.beginTransaction()
.replace(R.id.container,
PlaceholderFragment.newInstance(position + 1)).commit();
//TextView txtView1 = (TextView) findViewById(R.id.TextView79);
//txtView1.setBackgroundResource(R.drawable.nine);
}
public void onSectionAttached(int number) {
switch (number) {
case 1:
case1Selection();
break;
case 2:
case2Selection();
break;
case 3:
case3Selection();
break;
case 4:
case4Selection();
break;
case 5:
case5Selection();
break;
case 6:
case6Selection();
break;
case 7:
case7Selection();
break;
case 8:
case8Selection();
break;
case 9:
case9Selection();
break;
case 10:
case10Selection();
break;
case 11:
case11Selection();
break;
}
}
public void restoreActionBar() {
//Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//Toolbar toolbar getSupportActionBar();
//toolbar.setnavigationMode(Toolbar.NAV)
//ActionBar actionBar = getSupportActionBar();
//actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
//actionBar.setDisplayShowTitleEnabled(true);
toolbar.setTitle(mTitle);
//actionBar.setTitle(mTitle);
}

Not able to call TablayoutActivity from AppcombatActivity

I want to call my tablayout activity , when user clicked on Item Menu from Action View Item ,
But I am getting error :
06-15 14:17:01.604: E/AndroidRuntime(1365): java.lang.NoClassDefFoundError: Failed resolution of: Linfo/muscle/reboot/activity/Wrk_plan_gain;
Here is my Main activity from where I am calling Tab layout Activity .
MainActivity:
public class MainActivity extends AppCompatActivity implements FragmentDrawer.FragmentDrawerListener {
public MainActivity() {
// Required empty public constructor
}
private static String TAG = MainActivity.class.getSimpleName();
private Toolbar mToolbar;
private FragmentDrawer drawerFragment;
SharedPreferences pref;
String diet_saved;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pref = this.getPreferences(1);
diet_saved=pref.getString("diettype", "main");
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 boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.workout_menu) {
startActivity(new Intent(MainActivity.this, Wrk_plan_gain.class));
finish();
return true;
}
/*if(id == R.id.action_search){
Toast.makeText(getApplicationContext(), "Search action is selected!", Toast.LENGTH_SHORT).show();
return true;
}
*/
return super.onOptionsItemSelected(item);
}
#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 HomeFragment();
title = getString(R.string.title_home);
break;
case 1:
fragment = new KnowYourBodyFragment();
title = getString(R.string.title_friends);
break;
case 2:
fragment = new YourResultFragment();
title = getString(R.string.title_messages);
break;
case 3:
fragment = new YourMacroResultFragment();
title = getString(R.string.title_macro);
break;
case 4:
fragment = new DietPlanFragment();
title = getString(R.string.title_Diet);
break;
case 5:
title = getString(R.string.title_Exit);
finish();
break;
default:
}
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);
}
}
}
and I am trying call this Activity
public class Wrk_plan_gain extends AppCompatActivity implements TabLayout.OnTabSelectedListener {
public Wrk_plan_gain() {
// Required empty public constructor
}
//This is our tablayout
private TabLayout tabLayout;
//This is our viewPager
private ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.wrk_plan_layout_gain);
//Adding toolbar to the activity
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//Initializing the tablayout
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
//Adding the tabs using addTab() method
tabLayout.addTab(tabLayout.newTab().setText("Tab1"));
tabLayout.addTab(tabLayout.newTab().setText("Tab2"));
tabLayout.addTab(tabLayout.newTab().setText("Tab3"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
//Initializing viewPager
viewPager = (ViewPager) findViewById(R.id.pager);
//Creating our pager adapter
Pager adapter = new Pager(getSupportFragmentManager(), tabLayout.getTabCount());
//Adding adapter to pager
viewPager.setAdapter(adapter);
//Adding onTabSelectedListener to swipe views
tabLayout.setOnTabSelectedListener(this);
}
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
}
Also it will every much help full I f I can call this activity from Nav drawer Item ..
I am not able to get it work , in both cases .

Fragment does not display when select from navigation drawer

I have three fragments in my activity. I am using Recyclerview to display them. I wanted to make groupings of my fragment, like when A is selected in navigation drawer fragment 1 and 2 will display, using viewpager. And this works fine.
But when I select fragment B it does not display. And fragment B is not in the grouping.
Please help me out.
public class MainActivity extends AppCompatActivity implements FragmentDrawer.FragmentDrawerListener{
Toolbar toolbar;
SharedPreferences savedPreferences;
private static Context mContext;
private FragmentDrawer drawerFragment;
#TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
#Override
protected void onCreate(Bundle savedInstanceState) {
toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
toolbar.setTitle("");
toolbar.setSubtitle("");
setSupportActionBar(toolbar);
}
drawerFragment = (FragmentDrawer)
getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), toolbar);
drawerFragment.setDrawerListener(this);
displayView(0);
}
#Override
public void onDrawerItemSelected(View view, int position) {
displayView(position);
}
private void displayView(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = new TabFragment();
break;
case 1:
fragment = new MoviesFragment();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, fragment);
fragmentTransaction.commit();
}
}}
In this case MoviesFragment is not displaying. When i select MoviesFragment from navigation drawer, only a blank white screen appears.
Thanks!
Solved it!
The problem was not on MainActivity, it was the MoviesFragment itself. I just added an empty constructor and it worked.

How to navigate to a specific fragment from all other fragment?

When i press back button from my Home_Fragment the control will go to a empty page ,i want to exit my application when i press back button from Home_Fragment,at the same time when i press back button from all other fragment i want to navigate to Home_Fragment. My MainActivity page is below.
public class MainActivity extends ActionBarActivity implements FragmentDrawer.FragmentDrawerListener {
private static String TAG = MainActivity.class.getSimpleName();
private Toolbar mToolbar;
private FragmentDrawer drawerFragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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 boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
if(id == R.id.action_search){
Toast.makeText(getApplicationContext(), "Search action is selected!", Toast.LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item);
}
#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 HomeFragment();
title = getString(R.string.title_home);
break;
case 1:
fragment = new FriendsFragment();
title = getString(R.string.title_friends);
break;
case 2:
fragment = new MessagesFragment();
title = getString(R.string.title_messages);
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
// set the toolbar title
getSupportActionBar().setTitle(title);
}
}
}
you can write code for onBackButton to check for count of BackStackEntry. if atleast one fragement is in it, Count will be more than 0.
if count==0 then do finish()
#Override
public void onBackPressed() {
FragmentManager fm = getSupportFragmentManager();
if (fm.getBackStackEntryCount() == 1) {
//no fragments left
finish();
} else {
super.onBackPressed();
}
}
You can try this, It works:
Create a method in main Activity to call fragment:
public void replaceFragment(Fragment fragmentName){
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, fragmentName);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
And call this method by any fragment by the following lines of code:-
((MainActivity)getActivity).replaceFragment(FragmentName.class)

How to call a Fragment Activity as a Fragment?

I have a Navigation Drawer in my Main Activity. Now i need to call all the fragments from onNavigationDrawerItemSelected method. But the odd part is that I don't have fragments.All I have activities. So i extended it with Fragment Activity.
MainActivity is the navigation Drawer Activity.
FeedListActivity is the Fragment activity.
Now i need to add the Feedlistactivity as a Fragment in my Main Activity. How can i achieve this ?
I saw many questions Related to this. But i can't relate it with me.
Main Activity
public class MainActivity extends ActionBarActivity implements NavigationDrawerCallbacks{
private Toolbar mToolbar;
private NavigationDrawerFragment mNavigationDrawerFragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
mNavigationDrawerFragment = (NavigationDrawerFragment) getFragmentManager().findFragmentById(R.id.fragment_drawer);
mNavigationDrawerFragment.setup(R.id.fragment_drawer, (DrawerLayout) findViewById(R.id.drawer), mToolbar);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public void onNavigationDrawerItemSelected(int position) {
if (position == 1){
Fragment newFragment = new Fragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.container, newFragment);
transaction.commit();
}
}
}
FeedListActivity
public class FeedListActivity extends FragmentActivity{
private Toolbar mToolbar;
private NavigationDrawerFragment mNavigationDrawerFragment;
private static final String TAG = "FeedListActivity";
private ListView NewsView;
private FeedListAdapter listAdapter;
private List<FeedItem> feedItems;
ProgressBarCircularIndetermininate progressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_feed_list);
mToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
progressBar = (ProgressBarCircularIndetermininate) findViewById(R.id.progressBar);
progressBar.setVisibility(View.VISIBLE);
mNavigationDrawerFragment = (NavigationDrawerFragment) getFragmentManager().findFragmentById(R.id.fragment_drawer);
mNavigationDrawerFragment.setup(R.id.fragment_drawer, (DrawerLayout) findViewById(R.id.drawer), mToolbar);
feedItems = new ArrayList<FeedItem>();
listAdapter = new FeedListAdapter(this, feedItems);
NewsView = (ListView) findViewById(R.id.feed_list);
NewsView.setAdapter(listAdapter);
// making fresh volley request and getting json
// Adding request to volley request queue
AppController.getInstance().addRequest(gsonRequest, TAG);
getimg();
}
}
FeedListActivity is the Fragment activity.
Now i need to add the Feedlistactivity as a Fragment in my Main
Activity. How can i achieve this ?
A fragmentActivity is not a fragment. It's an activity with support for fragments, it was introduced to make older android builds backward compatible with fragments -- if you're targeting newer API's only you can ignore it. To make make FeedListActivity() a fragment...you must extend from fragment.
Fragment newFragment = new Fragment();
This line here should be creating a new instance of FeedList after you make it a fragment.

Categories

Resources