I used following segment to replace FrameLayout with Fragment inside onNavigationItemSelected method,
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
Fragment fragment;
if (id == R.id.nav_camara) {
fragment = new MyFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.mainFrame, fragment);
ft.commit();
}
else if (id == R.id.nav_gallery) {
}
else if (id == R.id.nav_slideshow) {
}
else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
}
I wanted to replace FrameLayout with FragmentActivity, then I did
FragmentActivity fragmentAc;
if (id == R.id.nav_camara) {
fragmentAc= new MyFragmentActivity();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.mainFrame, fragmentAc);
ft.commit();
}
but getting Wrong 2nd argument type. Found: 'android.support.v4.app.FragmentActivity', required: 'android.app.Fragment' on line ft.replace(R.id.mainFrame, fragmentAc);
What should I change now, Help me
You are trying to pass FragmentActivity where it requires Fragment, that's what error says.
If you want to open another activity rather than replacing fragment, you have to call startActivity(...)
replace:
if (id == R.id.nav_camara) {
fragmentAc= new MyFragmentActivity();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.mainFrame, fragmentAc);
ft.commit();
}
with:
if (id == R.id.nav_camara) {
startActivity(new Intent(getActivity(), MyFragmentActivity.class));
}
Related
I am working on Navigation Drawer Layout. Another layout must load in place of the
drawer_layout onClick of the navigation items.
if (id == R.id.nav_camera) {
//code to inflate another layout inplace of current layout
//setContentView(R.layout.another_layout1);
} else if (id == R.id.nav_gallery) {
//code to inflate another layout inplace of current layout
//setContentView(R.layout.another_layout2);
}
How to load another layout in the mainlayout.?
You have to create fragment
(Right click on your project -> New -> Fragment).then load inside Activity below this..
if (id == R.id.nav_camera) {
CameraFragment camera = new CameraFragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fragment_container, camera, "HELLO");
fragmentTransaction.commit();
} else if (id == R.id.nav_gallery) {
GalleryFragment gallery = new GalleryFragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fragment_container, gallery, "HELLO");
fragmentTransaction.commit();
}
In my android application, I had one main activity(i.e.Home) and two full-screen fragments. I can access theses three using Navigation Drawer. These two fragments act as the separate screen but actually its top on main activity.
In my case, I clicked the first fragment then I clicked the second fragment. During this time, I closed the first fragment and displayed the second fragment. But, main activity screen was now displayed one second during fragment transition.
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.slide_in, R.anim.slide_out);
if(!item.isChecked())
getSupportFragmentManager().popBackStackImmediate(null, POP_BACK_STACK_INCLUSIVE);
item.setChecked(true);
if (previousMenuItem != item.getItemId()) {
if (item.getItemId() == R.id.fragment_two) {
initialiseFragmentTwo();
} else if (item.getItemId() == R.id.nav_main_activity) {
getSupportFragmentManager(). popBackStackImmediate(null, POP_BACK_STACK_INCLUSIVE);
getSupportActionBar().setTitle(R.string.app_name);
}
else if(item.getItemId() == R.id.nav_fragment_one){
initialiseFragmentOne();
}
}
previousMenuItem = item.getItemId();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer != null) {
drawer.closeDrawer(GravityCompat.START);
}
return true;
}
Instead of using POP_BACK_STACK_INCLUSIVE try to use replace
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
Fragment fragment = null;
Class fragmentClass = null;
if (id == R.id.nav_camera) {
fragmentClass = FragmentOne.class;
} else if (id == R.id.nav_gallery) {
fragmentClass = FragmentTwo.class;
} else if (id == R.id.nav_slideshow) {
fragmentClass = FragmentOne.class;
} else if (id == R.id.nav_manage) {
fragmentClass = FragmentTwo.class;
} else if (id == R.id.nav_share) {
fragmentClass = FragmentOne.class;
} else if (id == R.id.nav_send) {
fragmentClass = FragmentTwo.class;
}
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
See here below links
http://chrisrisner.com/Using-Fragments-with-the-Navigation-Drawer-Activity
https://www.simplifiedcoding.net/android-navigation-drawer-example-using-fragments/
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_downloads) {
Intent i = new Intent(this,DownloadPage.class);
startActivity(i);
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_settings) {
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) { // here i want to add fragment
Manage manage = new Manage();
FragmentTransaction transaction = fm.beginTransaction();
transaction.add(R.id.downfragment, manage, "Settings");
transaction.commit();
}
};
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
}
You don't need another View.OnClickListener for this, the onNavigationItemSelected() is already invoked when you click on the drawer item. So simply remove your listener code, like the following:
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_downloads) {
Intent i = new Intent(this,DownloadPage.class);
startActivity(i);
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_settings) {
Manage manage = new Manage();
FragmentTransaction transaction = fm.beginTransaction();
transaction.add(R.id.downfragment, manage, "Settings");
transaction.commit();
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
}
I'm a noob in app building but i'm trying!
I've build the base of an app, first try in android studio.
Build a menu with fragments in it.
However i would like to have one menu-item linked to an activity (because I would like it to look different than the rest.)
Tried a lot but it doesn't work.
could u help me?
this is my menu code:
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.nav_camera) {
MainFragment fragment = new MainFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
} else if (id == R.id.nav_gallery) {
Zakkaartjes fragment = new Zakkaartjes();
android.support.v4.app.FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
} else if (id == R.id.nav_slideshow) {
Rekenen fragment = new Rekenen();
android.support.v4.app.FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
} else if (id == R.id.nav_manage) {
//THIS IS WHERE I WOULD LIKE TO HAVE A LINK TO MY "notities activity"
} else if (id == R.id.nav_share) {
Protocollen fragment = new Protocollen();
android.support.v4.app.FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
} else if (id == R.id.nav_send) {
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
"mailto","info#verpleegkundigzakboek.nl", null));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Email van verpleegkundig zakboek app");
startActivity(Intent.createChooser(emailIntent, "Verzend e-mail"));
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
Made Notities.java (with the other layout in it) and made activity_notities.xml
Hope anyone can help me?!
If I understand you correctly, this should work :
else if (id == R.id.nav_manage) {
Intent intent = new Intent(this, YourNextActivity.class);
startActivity(intent);
finish();
}
Also, you could consider use a switch instead of lots of else if :
public boolean onNavigationItemSelected(MenuItem item) {
switch (item.getId()){
case R.id.nav_camera:
//your code
break;
case R.id.nav_gallery:
//your code
break;
// add case blocks for each id you want to manage
}
}
More on switch here.
I created an Navigation Drawer Activity in Android Studio 1.4. My Main Activity is already setted up and now I want to switch to another Activity via the Navigation Drawer. The second Activity (AddDataActivity) should collect some User Unputs to create string which should be displayed in my Main Activities ListView.
My problem is, that I dont know how to open the AddDataActivity without "loosing" my navigation Drawer.
Intent AddData = new Intent(MainActivity.this, AddDataActivity.class);
MainActivity.this.startActivity(AddData);
Do I have to copy the whole Drawer Code for each Activity? Or would it be better to use Fragments?
I use Fragments now I avoid this Situation.
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
Fragment fragment;
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
fragment = new ListViewFragment();
int id = item.getItemId();
if (id == R.id.nav_listview) {
fragment= new ListViewFragment();
} else if (id == R.id.nav_add_data) {
fragment= new AddDataFragment();
} else if (id == R.id.nav_settings) {
fragment= new SettingsFragment();
} else if (id == R.id.nav_rooms) {
fragment= new SavedRoomsFragment();
} else if (id == R.id.nav_legal_information) {
fragment = new LegalInformationFragment();
}
ft.replace(R.id.container, fragment);
ft.addToBackStack(null);
ft.commit();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}