Problems in NavigationDrawer (Swap Activities) - android

Forgive my English :(
I have a problem with "NavigationDrawer". I have the Fragments "Home " that have no special activities, "Import, Gallery and SlideShow", all with activities running perfectly.
However, if I click on Import (HOME> IMPORT) the activity IMPORT opens, but if I click on (IMPORT> GALLERY / SLIDESHOW / HOME) the Import activity remains open.
I have to press the back button to go to HOME, to click on another activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
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) 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.home, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
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.
//here is the main place where we need to work on.
int id=item.getItemId();
switch (id){
case R.id.nav_home:
Intent h= new Intent(Home.this,Home.class);
startActivity(h);
break;
case R.id.nav_import:
Intent i= new Intent(Home.this,Import.class);
startActivity(i);
break;
case R.id.nav_gallery:
Intent g= new Intent(Home.this,Gallery.class);
startActivity(g);
break;
case R.id.nav_slideshow:
Intent s= new Intent(Home.this,Slideshow.class);
startActivity(s);
break;
// oh nightmare
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
IMPORT ACTIVITY
public class Import extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
DrawerLayout drawer;
NavigationView navigationView;
Toolbar toolbar=null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_import);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Button Button9 = (Button) findViewById(R.id.Button9);
Button Button11 = (Button) findViewById(R.id.Button11);
//We dont need this.
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) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
Button9.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent it = new Intent(Import.this, PHP5.class);
startActivity(it);
}
});
Button11.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent it = new Intent(Import.this, PHP7.class);
startActivity(it);
}
});
}
#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.home, 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.
//here is the main place where we need to work on.
int id=item.getItemId();
switch (id){
case R.id.nav_home:
Intent h= new Intent(Import.this,Home.class);
startActivity(h);
break;
case R.id.nav_import:
Intent i= new Intent(Import.this,Import.class);
startActivity(i);
break;
case R.id.nav_gallery:
Intent g= new Intent(Import.this,Gallery.class);
startActivity(g);
break;
case R.id.nav_slideshow:
Intent s= new Intent(Import.this,Slideshow.class);
startActivity(s);
break;
// after this lets start copying the above.
// FOLLOW MEEEEE>>>
//copy this now.
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}

Maybe you forgot to call finish() in the IMPORT Activity? If you call it after your Intent to another Activity, the current Activity will close, and your Home Activity will be shown again.

You need to remove all previous Activities from stack, it can be done trough a flag in the intent
Intent it = new Intent(Import.this, OtherAct.class);
it.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(it);
Else a combination of the keywors may work for you:
it.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
Check the doc about how it works:
https://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP
https://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_NEW_TASK
https://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TASK

Related

Why menu items in android studio don't work on click?

I'm developing a project on android and completely new to this. I had made a drawer menu with some of menu items. Now when i want to put another activity on click from menu items, the drawer closes automatically, it doesn't work. Here
private DrawerLayout mdrawerLayout;
private ActionBarDrawerToggle mdrawerToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mdrawerLayout = (DrawerLayout) findViewById(R.id.drawer);
mdrawerToggle = new ActionBarDrawerToggle(this, mdrawerLayout, R.string.open, R.string.close);
mdrawerLayout.addDrawerListener(mdrawerToggle);
mdrawerToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mdrawerToggle.onOptionsItemSelected(item)) {
return true;
}
int id = item.getItemId();
if(id == R.id.setup){
Intent csetup = new Intent(MainActivity.this, Subactivity.class);
startActivity(csetup);
return false;
}
return super.onOptionsItemSelected(item);
}
}
`
Try this:
Add below code in your onCreate method:
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);
Implement lister in your mainactivity class:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener
And after that in your onNavigationItemSelected method look like this:
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.setup) {
//call new activity
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
Hope this may help you
This is because you are overriding the wrong method. You have to override onNavigationItemSelected() for drawer, instead of onOptionsItemSelected() which is for top-right settings menu. See the following code for reference:
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.setup) {
Intent csetup = new Intent(MainActivity.this, Subactivity.class);
startActivity(csetup);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
First find the view for you NavigationView using findViewById method like below-NavigationView navView=(NavigationView)findViewById(R.id.navView)
then use this.
navView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
int id = item.getItemId();
if (id == R.id.setup) {
Intent csetup = new Intent(MainActivity.this, Subactivity.class);
startActivity(csetup);
}
return false;
}
});
then it will work.. onOptionsItemSelected(MenuItem item) is used to get the access to the MenuItems we have inflated on toolbar or on other layouts using onCreateOptionsMenu() method.

how to call single fragment from the second navigation drawer activity

I have a main navigation drawer using which I call signin fragment. Also there are other fragment which I can easily call. But when I log in, a second navigation drawer opens up and I am not able to call same fragments which I can easily call while using main navigation drawer.
This is my mainactivity where the navigation drawer being called
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private View.OnClickListener mOnClickListener;
SessionManager session;
private NavigationView navigationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
session = new SessionManager(getApplicationContext());
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) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
Fragment homeFragment;
if(!session.isLoggedIn()){
homeFragment = new SigninFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.content_main, homeFragment, String.valueOf(homeFragment.getClass())).addToBackStack(null).commit();
getSupportActionBar().setTitle(getResources().getString(R.string.signin_title));
}
}
#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) {
Fragment fr;
FragmentManager supportFragmentManager = getSupportFragmentManager();
ActionBar supportActionBar = getSupportActionBar();
supportActionBar.setTitle("productAga");
int id = item.getItemId();
switch (id){
case R.id.home_nv:
fr = new HomeFragment();
supportFragmentManager.beginTransaction().replace(R.id.content_main, fr, String.valueOf(fr.getClass())).addToBackStack(null).commit();
supportActionBar.setTitle(getResources().getString(R.string.home_title));
break;
case R.id.signup_nv:
fr = new SignupFragment();
supportFragmentManager.beginTransaction().replace(R.id.content_main, fr, String.valueOf(fr.getClass())).addToBackStack(null).commit();
supportActionBar.setTitle(getResources().getString(R.string.signup_title));
/*Intent it = new Intent(getApplicationContext(), SignupActivity.class);
startActivity(it);*/
break;
case R.id.signin_nv:
fr = new SigninFragment();
supportFragmentManager.beginTransaction().replace(R.id.content_main, fr, String.valueOf(fr.getClass())).addToBackStack(null).commit();
supportActionBar.setTitle(getResources().getString(R.string.signin_title));
break;
case R.id.about_nv:
fr = new AboutusFragment();
supportFragmentManager.beginTransaction().replace(R.id.content_main, fr, String.valueOf(fr.getClass())).addToBackStack(null).commit();
supportActionBar.setTitle(getResources().getString(R.string.about_title));
break;
case R.id.logout_nv:
session.logoutUser();
Intent it = new Intent(getApplicationContext(), MainActivity.class);
startActivity(it);
break;
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
Now when I logged in I am using this code and notice there is the same aboutus fragment
public class LoggedinMainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
SessionManager session;
private String gname;
private String gemail;
private static final String TAG = "TAG_LOG_IN" ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_loggedin_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
session = new SessionManager(getApplicationContext());
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawerlg_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_login_view);
navigationView.setNavigationItemSelectedListener(this);
/*Fragment homeFragment;
if(session.isLoggedIn()){
homeFragment = new DashboardFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.content_main, homeFragment, String.valueOf(homeFragment.getClass())).addToBackStack(null).commit();
getSupportActionBar().setTitle(getResources().getString(R.string.signin_title));
}*/
session.checkLogin();
HashMap<String, String> user = session.getUserDetails();
gname = user.get(SessionManager.KEY_NAME);
gemail = user.get(SessionManager.KEY_EMAIL);
}
#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.loggedin_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.
Fragment fr;
FragmentManager supportFragmentManager = getSupportFragmentManager();
ActionBar supportActionBar = getSupportActionBar();
supportActionBar.setTitle("productAga");
int id = item.getItemId();
switch (id){
case R.id.homelg_nv:
fr = new HomeFragment();
supportFragmentManager.beginTransaction().replace(R.id.content_main, fr, String.valueOf(fr.getClass())).addToBackStack(null).commit();
supportActionBar.setTitle(getResources().getString(R.string.home_title));
break;
case R.id.aboutlg_nv:
Log.d(TAG +"_ABOUT", ": about");
fr = new AboutusFragment();
supportFragmentManager.beginTransaction().replace(R.id.content_main, fr, String.valueOf(fr.getClass())).addToBackStack(null).commit();
supportActionBar.setTitle(getResources().getString(R.string.about_title));
break;
case R.id.logout_nv:
session.logoutUser();
Intent it = new Intent(getApplicationContext(), MainActivity.class);
startActivity(it);
break;
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawerlg_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
I am also using session management using sharedpreference in order to log in
Also, I got on the android monitor following error when I click on about us
java.lang.IllegalArgumentException: No view found for id 0x7f0d008a (com.roomi.productagaapplication:id/content_main) for fragment AboutusFragment{35e39e5d #0 id=0x7f0d008a}

How to check menu item in Android Navigation Drawer Template?

I'm using 'com.android.support:appcompat-v7:24.2.1' in my application; therefore, I generated a Navigation Drawer selecting it in the UI.
However, when I click in one item of the menu, it is not getting checked. Below 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);
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);
Fragment fragment = new PainelFragment();
replaceFragment(fragment);
navigationView.getMenu().getItem(0).setChecked(true);
}
#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();
Fragment fragment = null;
if (id == R.id.nav_painel) {
// Handle the painel action
fragment = new PainelFragment();
} else if (id == R.id.nav_modelos) {
// Handle the modelos action
fragment = new TabsFragment();
} else if (id == R.id.nav_cronograma) {
// Handle the cronograma action
fragment = new TabsFragment();
} else if (id == R.id.nav_faleConosco) {
// Handle the fale conosco action
fragment = new FaleConoscoFragment();
} else if (id == R.id.nav_perfil) {
// Handle the perfil action
fragment = new PerfilFragment();
} else if (id == R.id.nav_configuracoes) {
// Handle the configuracoes action
Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
startActivity(intent);
}
//replacing the fragment
if (fragment != null) {
replaceFragment(fragment);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
private void replaceFragment(Fragment f) {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.content_main, f)
.commit();
}
}

how to intent to another activity by making clickable TextView in drawable navigation

I have make the main part marked with bold (B) I am confused with, where to keep OnClickListner on the code that intent TextView to the new activity.
I want to make clickable menus in navigation drawer. I have login activity. So I want to make login clickable that intents to the login activity.
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
TextView t1;
#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_search_category) {
// Handle the camera action
} else if (id == R.id.nav_login) {
/*t1=(TextView)findViewById(R.id.nav_login);*/
t1.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent i=new Intent(MainActivity.this, LoginActivity.class);
startActivity(i);
}
});
} else if (id == R.id.nav_register) {
} else if (id == R.id.nav_test) {
} else if (id == R.id.nav_test1) {
} else if (id == R.id.nav_test2) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
You don't need the OnClickListener of the TextView. Just do:
else if (id == R.id.nav_login) {
Intent i=new Intent(MainActivity.this, LoginActivity.class);
startActivity(i);
}

How to change selected Item in the navigation drawer depending on the activity/view?

I have a Navigation Drawer Activity and different activities in which I get. I want the Items in the nav drawer being selected depending on the activity or view whatever.
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) {
Intent intent = new Intent(view.getContext(), MainAddMedActivity.class);
/*EditText editText = (EditText) findViewById(R.id.search_medicament);
String medicament_search = editText.getText().toString();*/
/*intent.putExtra(EXTRA_MESSAGE, medicament_search);*/
startActivity(intent);
}
});
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.getMenu().getItem(0).setChecked(true);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
/*navigationView.getMenu().getItem(0).setChecked(true);*/
}
#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_healthdiary) {
} else if (id == R.id.nav_appointment) {
} else if (id == R.id.nav_physician) {
} else if (id == R.id.nav_protocol) {
} 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;
For example, I get into the MainAddMedActivity and Press back. Then I want some code to check in which view or activity I am and select the item in the navigation drawer.
Seems like you have not yet implemented the switching of the content area. I suggest you use fragments for this.
So, if you use fragments, override onAttachFragment of your activity like:
#Override
public void onAttachFragment(Fragment fragment) {
super.onAttachFragment(fragment);
int id;
if(fragment instanceof HealthDiaryFragment) id = R.id.nav_healthdiary;
else
if(fragment instanceof AppointmentFragment) id = R.id.nav_appointment;
...
else return;
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setCheckedItem(id);
}
Also, modify your onBackPressed:
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
if(getFragmentManager().getBackStackEntryCount()>1) {
getFragmentManager().popBackStack();
}
else {
super.onBackPressed();
}
}
/*navigationView.getMenu().getItem(0).setChecked(true);*/
}
This is assuming that in your handling of drawer selection you replace fragments with pushing them on the back stack.

Categories

Resources