I am following this answer on SO, which is titled as:
Change NavigationView items when user is logged
The code works fine but the content of NavigationView change when I restart the app. I want the content to be changed after I click Login or LogOut item menus
This is my code in 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);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
if(islogin())
{
navigationView.getMenu().clear();
navigationView.inflateMenu(R.menu.activity_main_drawer1);
} else
{
navigationView.getMenu().clear();
navigationView.inflateMenu(R.menu.activity_main_drawer2);
}
navigationView.setNavigationItemSelectedListener(this);
toggle.syncState();
and here is the islogin() method:
public boolean islogin(){
// Retrieve data from preference:
prefs = getSharedPreferences("UserLoginData", MODE_PRIVATE);
String username = prefs.getString("username", null);
if (username == null) {
return false;
}
else{
return true;
}
}
Any help would greatly appreciated! Thank You
Note: Though this question seems duplicate of some, but its just the title, contents are entirely different.
Though I didn't get answer, I am posting my solution here.
I did solve it a very simple logic and its working very great.
Step 1:
I first initialize Toolbar toolbar; globally above class.
Step 2:
Then I just create a simple method called: myDrawer() and wrap all my Drawer code inside it. Like this:
public void myDrawer(){
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);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
if(islogin())
{
navigationView.getMenu().clear();
navigationView.inflateMenu(R.menu.activity_main_drawer2);
} else
{
navigationView.getMenu().clear();
navigationView.inflateMenu(R.menu.activity_main_drawer);
}
navigationView.setNavigationItemSelectedListener(this);
toggle.syncState();
}
Now I have hands over the Navigation and I can do anything with it, Like Refreshing and Calling again etc.
Step 3:
I am calling the method in Main Activity i-e: OnCreate
myDrawer();
Step 4:
And I am calling it every time I do logic of Signin and SignOut.
Wow! It's working like a charm.
PS: Just for the info purpose, Here is my onNavigationItemSelected, where I can handle Click Events:
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
Intent intent;
if (id == R.id.nav_item_item1) {
intent = new Intent(MainActivity.this, SomeClass1.class);
startActivity(intent);
} else if (id == R.id.nav_item_item2) {
intent = new Intent(getApplicationContext(), SomeClass2.class);
startActivity(intent);
} else if (id == R.id.nav_item_logout) {
// my other logic for signout
myDrawer();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
Credits:
User: manish jain Answer
Related
I have an app that connects my activities with a navigation drawer.The problem is that some activities load more difficult than others(when i press the icon on drawer,instead of opens the activity immediately,it stays for 1-2 seconds and then loads the activity). It might be a dummy question but is it possible if you could give me some advice on how to fix it?Here is one of the activities that load more difficult
public class ImportAPI extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
public TextView fullnameside, emailside;
public static String stravaToken;
public ImageButton btnStrava;
public ImageView tickStrava;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.importapi);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
toolbar.setTitle("");
toolbar.setSubtitle("");
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);
View headerView = navigationView.getHeaderView(0);
fullnameside = (TextView) headerView.findViewById(R.id.fullnameside);
emailside = (TextView) headerView.findViewById(R.id.emailside);
fullnameside.setText(""+GetInfo.fullname);
emailside.setText(""+GetInfo.email);
navigationView.setNavigationItemSelectedListener(this);
navigationView.getMenu().getItem(3).setChecked(true);
//STRAVA
tickStrava=(ImageView) findViewById(R.id.tickStrava);
btnStrava=(ImageButton) findViewById(R.id.stravaBtn);
connectStrava();
}
public void connectStrava(){
btnStrava.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent getStravaApi= new Intent(ImportAPI.this,StravaSetupApi.class);
startActivity(getStravaApi);
}
});
//GET ACCESS TOKEN FROM STRAVAS AUTHORIZE ACCOUNT
String accessToken = StravaAuthenticateActivity.getStravaAccessToken(this);
stravaToken=accessToken; //make static var so i can use it anywhere i want
//Get athletes activities from GetStravaAthleteActivities.java
new GetStravaAthleteActivities.AthleteActivities();
//check if token is null so i can display the tick and also disable the button press
if(stravaToken!=null)
{
btnStrava.setEnabled(false);
tickStrava.setVisibility(View.VISIBLE);
}
else {
tickStrava.setVisibility(View.INVISIBLE);
}
}
Try checking whether it's the Activity's fault and not the drawer menu's. You could time your onnectStrava() method, which could be causing the delay. Add log messages at the beginning and the end of the method.
public void connectStrava(){
Log.d(“TAG”, “STRAVA Entry point);
…
Log.d(“TAG”, “STRAVA Exit point);
}
then check the timestamps on your IDE Logcat and see how long it takes for the method to run. if it's indeed 1-2 seconds, then you know what causes the delay. If that's the case you could try to start the method on a separate thread or as an AsyncTask, so as to not have the method block your UI
I am new to java and android development, have only 3 apps under my belt.
The Problem:
When I click a navigation tab to take me to another activity, it says unfortunately app has stopped and brings me back to the launcher page..
Things I have already looked at fixing:
Grandle Console doesn't reference any errors in the code.
Grandle Console doesn't reference any missing dependancies.
I have ensured all versioning is correct.
I am not using depreciated methods.
The app works if I create my own navigation with buttons, this simple functionality will really streamline the app. It is an app for hiking to log gps with journal entries while mapping a hike using fine-location.
This is the first I have built with the navigation drawer. I have spent days on this problem alone and have finally decided to ask for help, hopefully someone can help out.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
String myurl="file:///android_asset/index.html";
WebView view=(WebView) this.findViewById(R.id.webView);
view.getSettings().setJavaScriptEnabled(true);
view.loadUrl(myurl);
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);
}
#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.activity_main) {
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
switch (id){
case R.id.activity_main:
Intent h= new Intent(MainActivity.this,MainActivity.class);
startActivity(h);
break;
case R.id.activity_entry:
Intent e= new Intent(MainActivity.this,EntryActivity.class);
startActivity(e);
break;
case R.id.activity_list:
Intent l= new Intent(MainActivity.this,ListActivity.class);
startActivity(l);
break;
case R.id.activity_bluetooth:
Intent b= new Intent(MainActivity.this,BluetoothActivity.class);
startActivity(b);
break;
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
First of All, If you put the error log, then that would be very helpful to get quick help. However, as a new developer,
Gradle is a build system, it actually build your project according to
the configuration (managing versions, artifacts, dependencies, etc).
Gradle Console won't show the Errors, you'll find that from Android Monitor/Logcat.
You may mistakenly do some common mistakes
Declaration of activities in the manifest.
looking for an Id of Resources that not belongs to the current layout.
Null Value Exception.
So i have a drawer layout inside Homepage of my activity it goes like this.
public void settingDrawer() {
if (drawer == null) {
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.setScrimColor(Color.TRANSPARENT);
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);
int width = getResources().getDisplayMetrics().widthPixels;
DrawerLayout.LayoutParams params = (android.support.v4.widget.DrawerLayout.LayoutParams) navigationView.getLayoutParams();
params.width = width;
navigationView.setLayoutParams(params);
}
}
it works just fine, but when i change activity and came back (using back button, or home button) the Drawer Layout stays open, i tried to close it when i use startActivity() method but it just doesn't right. i think there is a mistake that i made but i don't know where.
When you are going to Next Activity then first close Drawer like this
drawer.closeDrawer(GravityCompat.START);
so the above code will be close your drawer before navigating to next page and when you back to Activity then drawer is already closed.
I am using Google Design Support Library and DrawerLayout.
Setup:
final DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
NavigationView drawer = (NavigationView) findViewById(R.id.drawer);
if(mDrawerToggle == null) {
mDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, mToolbar, R.string.open, R.string.feather_close);
drawerLayout.addDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
}
When I start my app, the ripple effect of the hamburger icon is shown but the drawer is not opened. When I open the drawer at least one time by sliding from the left, the hamburger icon works for the entire runtime.
I don't have a special listener on the toggle button or the drawer itself and the onOptionsItemSelected method is not called.
Please help me to find out what happens.
Thank you.
I found it: I accidentally had android:visibility="gone" in my NavigationView.
What a freaky side effect.
I solved it using it :
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);
I want to disable the left swipe gesture for opening the navigation drawer as its messing with my seekbar. But setting drawer to LOCK_MODE_LOCKED_CLOSED is also disabling my hamburger icon.
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerToggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(drawerToggle);
drawerToggle.syncState();
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
Can someone please tell me what am I doing wrong?
You're not doing anything wrong. They recently changed the behavior of ActionBarDrawerToggle to disable opening/closing the drawer if it's locked.
Since your Toolbar is the support ActionBar, a workaround is to remove the Toolbar argument from the ActionBarDrawerToggle constructor call. This will cause the Activity's onOptionsItemSelected() method to be called upon clicking the toggle, and there you can check the MenuItem's item ID, and unlock the drawer before calling the toggle's method.
The ActionBarDrawerToggle class works a little differently with an ActionBar than a Toolbar, so you'll need to add the following call to show the toggle.
getSupportActionBar.setDisplayHomeAsUpEnabled(true);
Then change your ActionBarDrawerToggle constructor call as follows:
drawerToggle = new ActionBarDrawerToggle(this,
drawer,
R.string.navigation_drawer_open,
R.string.navigation_drawer_close) {
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
}
};
And override the Activity's onOptionsItemSelected() method like so:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == android.R.id.home) {
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
drawerToggle.onOptionsItemSelected(item);
return true;
}
...
return super.onOptionsItemSelected(item);
}