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.
Related
I'm trying for last two days to add fragment next to my drawer activity to get navigation drawer visible across the whole application. I have tried several ways from stackoverflow and many others but still no success. and after that i have to move to 2nd fragment from 1st fragment and so on till the need for navigation drawer.
I want to replace entire view except drawer when i move from from my activity to any fragment. Each fragment have its own layout.xml like an activity(Linear/Relative layouts as parent in them).
Drawer avtivity:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
Button btnfragOne = (Button) findViewById(R.id.btnfrag_one);
btnfragOne.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
FragOne fragment = new FragOne();
getSupportFragmentManager().beginTransaction()
.replace(R.id.frag2, fragment, fragment.getClass().getSimpleName()).addToBackStack(null).commit();
}
});
}
1st Fragment class:
public class FragOne extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.frag_one, container, false);
}
// 2nd Fragment class:
public class FragTwo extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.frag_two, container, false);
}
Simply for this task you have to override onNavigationItemSelected method in your Activity, which return id of selected fragment on NavigationDrawer.
Try this,
#Override
public boolean onNavigationItemSelected(MenuItem item) {
//calling the method displayselectedscreen and passing the id of selected menu
displaySelectedFragment(item.getItemId());
return true;
}
Now displaySelectedFragment,
private void displaySelectedScreen(int itemId) {
//creating fragment object
Fragment fragment = null;
//initializing the fragment object which is selected
switch (itemId) {
case R.id.your_fragment_one_id:
fragment = new FragOne();
break;
case R.id.your_fragment_two_id:
fragment = new FragTwo();
break;
}
//replacing the fragment
if (fragment != null) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.main_layout_id_which_is_to_be_replace, fragment);
ft.commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.your_drawer_layout_id);
drawer.closeDrawer(GravityCompat.START);
}
Edit -- If you want navigate from FragOne to FragTwo. Try this,
Create a method in your Activity,
public void showFragTwo(){
FragmentManager manager = getSupportFragmentManager();
FragTwo frag = new FragTwo();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.your_layout_id_which_is_to_be_replace, frag);
transaction.commit();
}
Then in your FragOne when you want to start FragTwo, call startFragTwo method from Activity as,
((YourActivity) getActivity()).showFragTwo();
i have menu with two fragments. when i choose second fragment, i can move to third fragment with button.
MainActivity
drawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close);
mDrawerLayout.setDrawerListener(drawerToggle);
drawerToggle.syncState();
getSupportActionBar().setTitle("demo");
mSelectedId = savedInstanceState == null ? R.id.aboutConference : savedInstanceState.getInt("SELECTED_ID");
itemSelection(mSelectedId);
}
private void setToolbar() {
toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
}
}
private void initView() {
mDrawer = (NavigationView) findViewById(R.id.main_drawer);
mDrawer.setNavigationItemSelectedListener(this);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
}
private void itemSelection(int mSelectedId) {
switch (mSelectedId) {
case R.id.aboutConference:
mDrawerLayout.closeDrawer(GravityCompat.START);
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_container, new FirstFragment());
fragmentTransaction.commit();
break;
case R.id.aboutDeveloper:
mDrawerLayout.closeDrawer(GravityCompat.START);
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_container, new SecondFragment());
fragmentTransaction.commit();
break;
}
}
Second fragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view =
inflater.inflate(R.layout.second_fragment, container, false);
Button button = (Button)view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_container, new ThirdFragment());
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
return view;
}
When i press backbutton, it's working correctly.
second fragment
But if i move to third fragment, choose first fragment in menu and press backbutton, third fragment overlaps first fragment
problem
How i can resolve this problem?
UPDATE: i don't want that i can return from first fragment to third when i press back button.
As Shaishav and vishal told you need to put colorto your parent view of the fragment.
orelse you need to Override the onBackButton pressed method and make sure the remove the previous item from stack.
To check if item is there in the stack or not try the following code
fragmentManager1.getBackStackEntryCount()
Fragment 1 is opened by navigation drawer and fragment 2 is opened by fragment 1 now I want to go back from fragment 2 to fragment 1 with back button as shown in picture and wanna hide drawer icon from fragment 2
You can do this by adding Activity and then first call activity from previous fragment then call your desired fragment in activity and finally add back button on the activity.
public class DetailActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
/* ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
actionBar.setCustomView(R.layout.header);
actionBar.setDisplayHomeAsUpEnabled(true); */
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setNavigationIcon(R.drawable.back);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Smart Take Away");
// getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if (toolbar != null) {
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
}
// Bundle extras = getIntent().getExtras();
if (savedInstanceState == null) {
try {
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
DetailedListFragment fragment = new DetailedListFragment();
// fragment.setArguments(extras);
fragmentTransaction.replace(R.id.detailfragment, fragment);
fragmentTransaction.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
}
#Override
public void onBackPressed() {
super.onBackPressed();
}
}
You can use a click or swipe event
get the instance of your second fragment
SecondFragment fragment = new SecondFragment();
fragmentManager.beginTransaction().replace(R.id.first_container, fragment).commit();
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);
}
}
}
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.