Within my navigation drawer, I am trying to execute a code to start a new activity so it moves from one activity to another. However, When I use a DividerDrawerItem() it does not execute the last case statement (4). I've tried it without the DividerDrawerItem() and it works but when I add it in, it does not.
PrimaryDrawerItem item1 = new PrimaryDrawerItem().withIdentifier(1).withName("Home Page");
PrimaryDrawerItem item2 = new PrimaryDrawerItem().withIdentifier(2).withName("Rank Table");
PrimaryDrawerItem item3 = new PrimaryDrawerItem().withIdentifier(3).withName("Report");
PrimaryDrawerItem item4 = new PrimaryDrawerItem().withIdentifier(4).withName("Log out");
//create the drawer and remember the `Drawer` result object
Drawer result = new DrawerBuilder()
.withActivity(this)
.withAccountHeader(headerResult)
.withToolbar(toolbar)
.addDrawerItems(
item1, item2,item3,
new DividerDrawerItem(),
item4
)
.withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
#Override
public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
// do something with the clicked item :D
switch(position){
case 1: startActivity(new Intent(HomePage.this, HomePage.class));
break;
case 2: startActivity(new Intent(HomePage.this, RankT.class));
break;
case 3: startActivity(new Intent(HomePage.this,Report.class));
break;
case 4: firebaseAuth.signOut();
finish();
startActivity(new Intent(HomePage.this,MainActivity.class));
}
return true;
}
I think your code is using position where it should use identifier.
Change switch(position) to switch((int) drawerItem.getIdentifier())
Related
I am using a navigation drawer and want it to do different activities for different cases. I am using the switch method to implement the activities. I have implemented 3 switch cases but however, it keeps doing the case 3 instead of case 2. Here is my code that i am using:
PrimaryDrawerItem item1 = new PrimaryDrawerItem().withIdentifier(1).withName("Discover");
PrimaryDrawerItem item2 = new PrimaryDrawerItem().withIdentifier(2).withName("Rank Table");
PrimaryDrawerItem item3 = new PrimaryDrawerItem().withIdentifier(3).withName("Log out");
//create the drawer and remember the `Drawer` result object
Drawer result = new DrawerBuilder()
.withActivity(this)
.withAccountHeader(headerResult)
.withToolbar(toolbar)
.addDrawerItems(
item1, item2, item3
)
.withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
#Override
public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
switch(position){
case 1: break;
case 2: startActivity(new Intent(HomePage.this, RankT.class));
case 3: firebaseAuth.signOut();
finish();
startActivity(new Intent(HomePage.this, MainActivity.class));
}
return true;
})
.build();
You forgot to add break; at the end of each switch case.
switch(position){
case 1: break;
case 2: startActivity(new Intent(HomePage.this, RankT.class));
break;
case 3: firebaseAuth.signOut();
finish();
startActivity(new Intent(HomePage.this, MainActivity.class));
break;
}
Hope this will help!!
I am using a Navigation drawer in my Main screen and use the third party library com.mikepenz:materialdrawer:5.1.6#aar, to build my Material Drawer. My requirement is that I want to hide some menu items based on a condition. Is there a way to achieve it? This is how I create my drawer.
private Drawer result = null;
AccountHeader headerResult;
final PrimaryDrawerItem home = new PrimaryDrawerItem().withName("Home").withIdentifier(1).withIcon(GoogleMaterial.Icon.gmd_home);
final PrimaryDrawerItem profile = new PrimaryDrawerItem().withName("Profile").withIdentifier(2).withIcon(GoogleMaterial.Icon.gmd_account);
final PrimaryDrawerItem gallery = new PrimaryDrawerItem().withName("Gallery").withIdentifier(3).withIcon(R.drawable.ic_perm_media_black_24dp);
final PrimaryDrawerItem recognition = new PrimaryDrawerItem().withName("Recognition").withIdentifier(4).withIcon(GoogleMaterial.Icon.gmd_face);
final PrimaryDrawerItem maps = new PrimaryDrawerItem().withName("Maps").withIdentifier(5).withIcon(R.drawable.ic_place_black_24dp);
final PrimaryDrawerItem tagAndLocate = new PrimaryDrawerItem().withName("Tag & Locate").withIdentifier(6).withIcon(R.drawable.ic_remove_red_eye_black_24dp);
final PrimaryDrawerItem gamesAndPuzzle = new PrimaryDrawerItem().withName("Games & Puzzles").withIdentifier(7).withIcon(R.drawable.ic_casino_black_24dp);
final PrimaryDrawerItem backup = new PrimaryDrawerItem().withName("Backup").withIdentifier(8).withIcon(GoogleMaterial.Icon.gmd_save);
final PrimaryDrawerItem logout = new PrimaryDrawerItem().withName("Logout").withIdentifier(9).withIcon(FontAwesome.Icon.faw_sign_out);
headerResult = new AccountHeaderBuilder()
.withActivity(this)
.withHeaderBackground(R.drawable.header)
.withSelectionListEnabledForSingleProfile(false)
.addProfiles(userProfile)
.withOnAccountHeaderListener(new AccountHeader.OnAccountHeaderListener() {
#Override
public boolean onProfileChanged(View view, IProfile profile, boolean currentProfile) {
return false;
}
})
.build();
result = new DrawerBuilder()
.withActivity(this)
.withAccountHeader(headerResult)
.withToolbar(toolbar)
.withDisplayBelowStatusBar(false)
.withTranslucentStatusBar(true)
.withSavedInstance(savedInstanceState)
.withActionBarDrawerToggle(true)
.withActionBarDrawerToggleAnimated(true)
.addDrawerItems(home)
.addDrawerItems(profile)
.addDrawerItems(gallery)
.addDrawerItems(recognition)
.addDrawerItems(maps)
.addDrawerItems(tagAndLocate)
.addDrawerItems(gamesAndPuzzle)
.addDrawerItems(backup)
.addDrawerItems(new DividerDrawerItem())
.addDrawerItems(logout)
.buildForFragment();
resultStandard.setOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
#Override
public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
int drawItemId = (int) drawerItem.getIdentifier();
Intent intent;
Fragment fragment;
switch (drawItemId) {
case 1:
fragment = new HomeFragment();
gaFragmentStack.add(home);
break;
case 2:
fragment = new ProfileFragment();
gaFragmentStack.add(profile);
break;
case 3:
fragment = new GalleryFragment();
gaFragmentStack.add(gallery);
break;
case 4:
fragment = new RecognitionFragment();
gaFragmentStack.add(recognition);
break;
case 5:
fragment = new MapsFragment();
gaFragmentStack.add(maps);
break;
case 6:
fragment = new TagLocateFragment();
gaFragmentStack.add(tagAndLocate);
break;
case 7:
fragment = new GamesPuzzlesFragment();
gaFragmentStack.add(gamesAndPuzzle);
break;
case 8:
fragment = new BackupFragment();
gaFragmentStack.add(backup);
break;
default:
fragment = new HomeFragment();
break;
}
if (drawItemId == 9) {
FirebaseAuth.getInstance().signOut();
SharedPreferences.Editor editor = preferences.edit();
editor.clear();
editor.apply();
intent = new Intent(MainMenuActivity.this, SplashScreen.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
}
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.container_gaFragments, fragment);
transaction.commit();
return false;
}
});
I just want to hide some Drawer item based on a condition. Is there a way to do that? Any help is appreciated.
There is no way to hide a particular item from the drawer. You can resolve this by removing all items (removeAllItems() and removeAllStickyFooterItems()) and adding them selectively again. Below is the example code for Kotlin where you can handle this with IF statement inside the slider.apply {...}
fun refreshMenuItems() {
slider.apply {
removeAllItems()
removeAllStickyFooterItems()
if (isLoggedUser == false) {
addItems(
PrimaryDrawerItem().apply {
nameText = "Login"
iconicsIcon = GoogleMaterial.Icon.gmd_verified_user
identifier = 70
},
DividerDrawerItem(),
PrimaryDrawerItem().apply {
nameText = "Register"
iconicsIcon = GoogleMaterial.Icon.gmd_how_to_reg
identifier = 80
}
)
} else {
addItems(
PrimaryDrawerItem().apply {
nameText = "My profile"
iconicsIcon = GoogleMaterial.Icon.gmd_verified_user
identifier = 70
},
DividerDrawerItem(),
PrimaryDrawerItem().apply {
nameText = "My reservations"
iconicsIcon = GoogleMaterial.Icon.gmd_how_to_reg
identifier = 80
}
)
addStickyDrawerItems(
PrimaryDrawerItem().apply {
nameText = "Log out"
isSelectable = false
identifier = 100
}
)
}
}
}
For example, you can call this method from onResume() method
override fun onResume() {
super.onResume()
changeDrawerUserName()
refreshMenuItems()
}
I currently have an existing project in which I would like to implement an navigation drawer activity. Currently, if I add a new navigation drawer activity, it generates the following layout:
However, instead of the layout above I would like my navigation drawer to look exactly like the default template one which looks like this (consisting of a nav header, etc):
How can I go about achieving this? I'm new to Android development.
EDIT: I just needed to change my app theme to achieve what I wanted
Create a new Drawer Activity :
File->new->activity->Navigation Drawer Activity
private void setupNavigationDrawer() {
//if you want to update the items at a later time it is recommended to keep it in a variable
PrimaryDrawerItem item1 = new PrimaryDrawerItem().withIdentifier(0).withName(R.string.drawer_item_home).withIcon(getResources().getDrawable(R.drawable.ic_home_white_24dp));
PrimaryDrawerItem item2 = new PrimaryDrawerItem().withIdentifier(1).withName(R.string.drawer_item_login).withIcon(getResources().getDrawable(R.drawable.ic_account_circle_white_24dp));
PrimaryDrawerItem item3 = new PrimaryDrawerItem().withIdentifier(2).withName(R.string.drawer_item_movies).withIcon(getResources().getDrawable(R.drawable.ic_movie_white_24dp));
PrimaryDrawerItem item4 = new PrimaryDrawerItem().withIdentifier(3).withName(R.string.drawer_item_trailers).withIcon(getResources().getDrawable(R.drawable.ic_videocam_white_24dp));
PrimaryDrawerItem item5 = new PrimaryDrawerItem().withIdentifier(4).withName(R.string.drawer_item_theatres).withIcon(getResources().getDrawable(R.drawable.ic_theaters_white_24dp));
PrimaryDrawerItem item6 = new PrimaryDrawerItem().withIdentifier(5).withName(R.string.drawer_item_location).withIcon(getResources().getDrawable(R.drawable.ic_location_on_white_24dp));
SecondaryDrawerItem item7 = (SecondaryDrawerItem) new SecondaryDrawerItem().withIdentifier(6).withName(R.string.drawer_item_about_us).withIcon(FontAwesome.Icon.faw_info_circle);
SecondaryDrawerItem item8 = (SecondaryDrawerItem) new SecondaryDrawerItem().withIdentifier(7).withName(R.string.drawer_item_contact_us).withIcon(FontAwesome.Icon.faw_whatsapp);
SecondaryDrawerItem item9 = (SecondaryDrawerItem) new SecondaryDrawerItem().withIdentifier(8).withName(R.string.drawer_item_feedback).withIcon(FontAwesome.Icon.faw_commenting);
SecondaryDrawerItem item10 = (SecondaryDrawerItem) new SecondaryDrawerItem().withIdentifier(9).withName(R.string.drawer_item_settings).withIcon(FontAwesome.Icon.faw_wrench);
// Create the AccountHeader
AccountHeader headerResult = new AccountHeaderBuilder()
.withActivity(this)
.withHeaderBackground(R.color.colorMaterialDark)
.addProfiles(
new ProfileDrawerItem().withName(getResources().getString(R.string.app_name)).withEmail(getResources().getString(R.string.email_id)).withIcon(getResources().getDrawable(R.drawable.profile))
)
.withOnAccountHeaderListener(new AccountHeader.OnAccountHeaderListener() {
#Override
public boolean onProfileChanged(View view, IProfile profile, boolean currentProfile) {
//Any activity or Intent
}
})
.build();
//Create the drawer and remember the `Drawer` result object
Drawer result = new DrawerBuilder()
.withActivity(this)
.withAccountHeader(headerResult)
.withToolbar(toolbar)
.addDrawerItems(
item1, item2, item3, item4, item5, item6,
new SectionDrawerItem().withName("Extras"),
item7, item8, item9, item10
)
//Set onClick options for drawer item click
.withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
#Override
public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
// do something with the clicked item :D
switch (position) {
//Home
case 1: {
break;
}
//Login
case 2: {
//Login Activity
break;
}
case 3: {
//Third activity
break;
}
case 4: {
break;
}
case 5: {
break;
}
//Location
case 6: {
break;
}
//About us
case 8: {
break;
}
//Contact Us
case 9: {
break;
}
//Feedback
case 10: {
break;
}
//Settings
case 11:{
break;
}
}
return true;
}
})
.build();
result.addStickyFooterItem(new PrimaryDrawerItem().withName("Visit us again")); //Adding footer to nav drawer
}
You will have to add the following lines to build.gradle file in android studio
compile('com.mikepenz:materialdrawer:5.3.3#aar') {
transitive = true
}
compile 'com.mikepenz:fontawesome-typeface:4.6.0.2#aar'
I've got spinner navigation on my action bar and have a navigation listener for this.
When the activity is created the listener (below) picks up on the default spinner item which means case 0 is run on creation, opening another activity.
How do I stop it registering a navigation change when the activity is created?
ActionBar.OnNavigationListener navigationListener = new OnNavigationListener() {
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
switch(itemPosition) {
case 0:
Intent i = new Intent(Main.this, Example.class);
startActivity(i);
break;
case 1:
Intent i2 = new Intent(Main.this, Example.class);
startActivity(i2);
break;
case 2:
Intent i3 = new Intent(Main.this, Example.class);
startActivity(i3);
break;
case 3:
Intent i4 = new Intent(Main.this, Example.class);
startActivity(i4);
break;
}
return false;
}
};
Update:
Think I solved it with this, I declared a boolean flag, changed it to false oncreate.
ActionBar.OnNavigationListener navigationListener = new OnNavigationListener() {
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
switch(itemPosition) {
case 0:
if(flag == true) {
if(Main.class == Main.class) {
} else {
Intent i = new Intent(Main.this, Main.class);
startActivity(i);
}
}
if(flag == false) {
flag = true;
}
break;
case 1:
Intent i2 = new Intent(Main.this, Example.class);
startActivity(i2);
break;
case 2:
Intent i3 = new Intent(Main.this, Example.class);
startActivity(i3);
break;
case 3:
Intent i4 = new Intent(Main.this, Example.class);
startActivity(i4);
break;
}
return false;
}
};
There may be a simpler way but you could put a boolean flag in onCreate() to false and check for that in your listener then set it to true after the first time when it sets up so it won't run the Intent code when you first run it. Depending on what you need, you may want to put it in onResume() so it doesn't run if you back into this Activity
I'm trying to implement a drop down list as navigation for the action bar in Android.
I can see the drop down list and the items, but I can't get the clicking event.
I'm not sure what I'm missing since I was following the tutorial in http://developer.android.com/guide/topics/ui/actionbar.html
This is my code:
public void onCreate(Bundle savedInstanceState) {
OnNavigationListener mOnNavigationListener;
super.onCreate(savedInstanceState);
// setContentView(R.layout.info_layout);
// getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setNavigationMode(getSupportActionBar().NAVIGATION_MODE_LIST);
SpinnerAdapter mSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.navigation_array, android.R.layout.simple_dropdown_item_1line);
mOnNavigationListener = new OnNavigationListener() {
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
switch (itemPosition) {
case 1:
Intent i = new Intent();
i.setClass(getApplicationContext(), ZoekAndBoekActivity.class);
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
}
// return super.onOptionsItemSelected(itemPosition);
return true;
}
};
getSupportActionBar().setListNavigationCallbacks(mSpinnerAdapter, mOnNavigationListener);
}
Thanks a lot in advance!
Are you sure that you don't get click events? You're creating intent but doesn't do anything with it. Try something like this:
switch (itemPosition) {
case 1:
Intent i = new Intent();
i.setClass(getApplicationContext(), ZoekAndBoekActivity.class);
startActivity(i);
break;
...
}
or add writing to log to be sure:
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
Log.d("SomeTag", "Get click event at position: " + itemPosition);
switch (itemPosition) {
...
}
}
and see in the logcat output for message with "SomeTag" when you click on items.
I think the return statement must be false inside the switch case, and your case must have brackets.. Hope it helps :)))