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.
Related
How do I remove the color of the selected section of the image within this function, thank you very much for the help, first code is the onnavigationitemselected, and the second the function which i want to remove the color of the selected
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_mapa)
{
// Handle the home action
MapFragment mapFragment = new MapFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, mapFragment);
fragmentTransaction.commit();
} else if (id == R.id.nav_anuncios)
{
AnunciosFragment anunciosFragment = new AnunciosFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, anunciosFragment);
fragmentTransaction.commit();
} else if (id == R.id.nav_adopciones)
{
AdopcionesFragment adopcionesFragment = new AdopcionesFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, adopcionesFragment);
fragmentTransaction.commit();
} else if (id == R.id.nav_protectoras)
{
ProtectorasFragment protectorasFragment = new ProtectorasFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, protectorasFragment);
fragmentTransaction.commit();
} else if (id == R.id.nav_ajustes)
{
Toast.makeText(this, "Ajustes clicked", Toast.LENGTH_SHORT).show();
} else if (id == R.id.nav_contacto)
{
Toast.makeText(this, "Contacto Clicked", Toast.LENGTH_SHORT).show();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
Perfil function:
public void Perfil(View view)
{
HomeFragment homeFragment = new HomeFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, homeFragment);
fragmentTransaction.commit();
//per tancar NAV al seleccionar
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
}
Image which i want to eliminate the selection :
you can do it like this:
public boolean onNavigationItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_camera:
break;
case R.id.nav_gallery:
break;
}
mDrawerLayout.closeDrawer(GravityCompat.START);
return false;
}
return false means unchecked state.
or else you can do it like :
drawer.getMenu().findItem(R.id.nav_camera).setChecked(false);
you need to se checked false unselect menu item
navigationView.getMenu().getItem(3).setChecked(false);
getItem(3) means you need to pass index of menu item
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));
}
Recently i'm working on my app to make it load faster and work better, i'm using navigation drawer in my MainActivity:
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camara) {
LoadJson asyncTask = (LoadJson) new LoadJson(new LoadJson.AsyncResponse() {
#Override
public void processFinish(JSONArray output) {
//Here you will receive the result fired from async class
//of onPostExecute(result) method.
//Set the fragment initially
MainFragment fragment = new MainFragment(output);
FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();
// Handle the camera action
}
}).execute();
} else if (id == R.id.nav_gallery) {
//Set the fragment initially
GalleryFragment fragment = new GalleryFragment();
FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
} else if (id == R.id.nav_search) {
//Set the fragment initially
FetchResualt fragment = new FetchResualt();
FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
// Handle the camera action
} else if (id == R.id.nav_manage) {//
Bundle bundle = new Bundle();//
bundle.putInt("someStr",ID_OF_BEACH);//
//Set the fragment initially//
FragmentBeach fragment = new FragmentBeach();//
fragment.setArguments(bundle);//
FragmentTransaction fragmentTransaction =//
getSupportFragmentManager().beginTransaction();//
fragmentTransaction.replace(R.id.fragment_container, fragment);//
fragmentTransaction.commit();//
// Handle the camera action
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
As you can see if we push the first menu if (id == R.id.nav_camara) it will pass a JSONArray to a Fragment class and it take like 4-5 seconds to load. So if the user navigates between menus every time he goes to nav_camara it make the app a 4 second freeze to load and as you can see everytime we choose a menu item it will recreate a new copy of fragment, so even if I make: setRetainInstance(true); in my fragment class it wont work.
What solution you suggest to prevent the app to recreate a new fragment each time we choose a menu item?
You will need to keep a SparseArray<Fragment> to keep the instances in memory.
Follow these steps:
create a field in your Activity:
SparseArray<Fragment> myFragments;
initialise it in the onCreate() like:
myFragments = new SparseArray<Fragment>();
update your onNavigationItemSelected():
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camara) {
// get cached instance of the fragment
fragment = myFragments.get(INT_CONSTANT_FOR_CAM_FRAGMENT);
// if fragment doesn't exist in myFragments, create one and add to it
if (fragment == null) {
fragment = new MainFragment();
myFragments.put(INT_CONSTANT_FOR_CAM_FRAGMENT, fragment);
}
// now load the fragment
FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
}
// do the rest with others as well.
}
move the AsyncTask thing inside your MainFragment's onActivityCreated() or a similar lifecycle method.
Above way will let you reuse the fragments and move the logic to their correct location (your Activity shouldn't know how MainFragment loads data to initiate itself, but of course you can still keep it there, though not recommended).
You can add a TAG while push a new fragment to the container. Then use FragmentManager#findFragmentByTag to find any fragment previously added with the same tag.
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
Fragment oldFragment = manager.findFragmentByTag(tag);
if (oldFragment != null)
transaction.replace(R.id.fragment_container, oldFragment, tag);
else
transaction.replace(R.id.fragment_container, newInstanceOfFragment, tag);
transaction.commit();
Declare them globally
GalleryFragment galleryFrag = new GalleryFragment();
FragmentTransaction ft;
FragmentManager fm;
#Override
protected void onCreate(Bundle savedInstanceState) {
...
fm = getSupportFragmentManager();
...
}
then on your navigation selection
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_search) {
ft = fm.beginTransaction();
ft.replace(R.id.fragment_container, galleryFrag).commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
I'm getting following error:
cannot find symbol variable fragment
How to fix this error?
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.toi) {
fragment= new FragmentOne();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.commit();
return true;
} else if ( id ==R.id.thehindu){
}else if (id == R.id.support) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
Change:
fragment= new FragmentOne();
To:
Fragment fragment= new FragmentOne();
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;
}