Naviation Drawer calling an activity - android

How to call an activity from the navigation drawer.
thia ia my navigation drawer:
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_Home) {
Toast.makeText(this, "Home", Toast.LENGTH_SHORT).show();
HomeFragment homeFragment = new HomeFragment();
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.RelativeLayout, homeFragment, homeFragment.getTag()).commit();
} else if (id == R.id.nav_Profile) {
Toast.makeText(this, "Profile", Toast.LENGTH_SHORT).show();
} else if (id == R.id.nav_manage) {
Toast.makeText(this, "Settings", Toast.LENGTH_SHORT).show();
} else if (id == R.id.nav_share) {
Toast.makeText(this, "Share", Toast.LENGTH_SHORT).show();
} else if (id == R.id.nav_send) {
Toast.makeText(this, "Send", Toast.LENGTH_SHORT).show();}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
I have an activity called ProfileActivity.java but i dont know how to call an activity through navigation drawer...help

Hello try this hope it may help you
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_Home) {
Toast.makeText(this, "Home", Toast.LENGTH_SHORT).show();
HomeFragment homeFragment = new HomeFragment();
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.RelativeLayout, homeFragment, homeFragment.getTag()).commit();
} else if (id == R.id.nav_Profile) {
//Toast.makeText(this, "Profile", Toast.LENGTH_SHORT).show();
startActivity(new Intent(YOUR_ACTIVITY,ProfileActivity.class));
} else if (id == R.id.nav_manage) {
Toast.makeText(this, "Settings", Toast.LENGTH_SHORT).show();
} else if (id == R.id.nav_share) {
Toast.makeText(this, "Share", Toast.LENGTH_SHORT).show();
} else if (id == R.id.nav_send) {
Toast.makeText(this, "Send", Toast.LENGTH_SHORT).show();}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}

Related

How to change activity when click drawer menu item?

I have a drawer menu which by Android Studio.
My menu's code:
//Menü kodları//
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.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
navigationView.setItemIconTintList(null);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.menu_fbdown) {
} else if (id == R.id.menu_instadown) {
} else if (id == R.id.menu_twitdown) {
} else if (id == R.id.menu_privacy) {
} else if (id == R.id.menu_about) {
} else if (id == R.id.menu_contact) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
I want to make clicktable this menu. I tired this code:
Intent i = new Intent(FBDownActivity.this, FBDownActivity.class);
startActivity(i);
Like this:
#Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.menu_fbdown) {
Intent i = new Intent(FBDownActivity.this, FBDownActivity.class);
startActivity(i);
} else if (id == R.id.menu_instadown) {
Intent i = new Intent(FBDownActivity.this, FBDownActivity.class);
startActivity(i);
} else if (id == R.id.menu_twitdown) {
} else if (id == R.id.menu_privacy) {
} else if (id == R.id.menu_about) {
} else if (id == R.id.menu_contact) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
But it's didn't work. Still can't click to item.
What Can I do about it?
Thanks.
You are trying to start the same activity as you are. Change the code bellow:
#Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.menu_fbdown) {
Intent i = new Intent(YourCurrentActivty.this, ActivityToOpen.class);
startActivity(i);
} else if (id == R.id.menu_instadown) {
Intent i = new Intent(YourCurrentActivty.this, ActivityToOpen.class);
startActivity(i);
} else if (id == R.id.menu_twitdown) {
} else if (id == R.id.menu_privacy) {
} else if (id == R.id.menu_about) {
} else if (id == R.id.menu_contact) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}

Full-screen fragment to fragment transition displayed main activity during fragment transition

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/

I want to add Fragment in Navigation Drawer in android,i tried the below code but its not happening

#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) {
}
}

Switching and clicking on fragments inside navigation drawer

I have made navigation drawer in Android Studio.
Right now each fragment from drawer is leading to nowhere.
I need to know how can I switch between fragments and click on each one
I know i need to make new fragment, but what method to use so I can click on Settings from navigation drawer so it lead me to fragment Share?
Here is my code;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(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.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
// Handle the camera action
} 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) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
You are in the right direction. Just add the Fragment class in each of the Navigation items like below
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
Fragment fragment = null;
FragmentManager fragmentManager = getSupportFragmentManager();
if (id == R.id.nav_camera) {
// Handle the camera action
fragment = new DefaultFragment();
fragmentManager.beginTransaction().replace(R.id.first_container, fragment).commit();
} else if (id == R.id.nav_gallery) {
fragment = new ItemListFragment();
fragmentManager.beginTransaction().replace(R.id.first_container, fragment).commit();
}
}
at the end of onCreate(Bundle savedInstanceState)method, you can set the default Fragment to open on Activity load.
FragmentManager fragmentManager = getSupportFragmentManager();
DefaultFragment fragment = new DefaultFragment();
fragmentManager.beginTransaction().replace(R.id.first_container, fragment).commit();
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.`enter code here`
int id = item.getItemId();
FragmentManager fragmentManager =getSupportFragmentManager();
GoogleMap googleMap =getSupportFragmentManager();
if (id == R.id.alarme){
} else if (id == R.id.map) {
fragmentManager.beginTransaction().replace(R.id.contenedor,new mapposta()).commit();
} else if (id == R.id.parametre) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;

How to add fragments to Navigation Drawer template in Android Studio

I've searched all over Google trying to find the answer but haven't been able to find an answer. I'm using the "Create Navigation Drawer Activity" template in Android Studio. How do I add my fragments to the Navigation Drawer?
Here's where I assume I add the fragments:
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camara) {
// Handle the camera action
} 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) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
Based on tutorials I've seen, I've tried adding the following in the if statements with no success:
fragment = new ItemFragment();

Categories

Resources