Navigation drawer listview fragment - android

I want to make a navigation drawer which will populate by listview. Now i want that by clicking each item of the list, open a fragment as per my choice. How do I do this?

The NavigationDrawer doesn't use a ListView in the menu, NavigationDrawer uses directly Menu Items it means that in your source code you can add programmatically items to your menu as you need.
Your NavigationDrawer activity or whatever you named
you can add items to the menu using the
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// To Inflate the menu:
// this adds items to the navigation drawer menu if it is present.
getMenuInflater().inflate(R.menu.dashboard, menu);
return true;
}
by default this method create a static menu using a XML layout in the res/menu/menu_file.xml
but if you follow the next source code you will be able to add more items to your navigation drawer programmatically
the Method add() overloads:
add (int titleRes)
add (CharSequence title)
add (int groupId, int itemId, int order, int titleRes);
add (int groupId, int itemId, int order, CharSequence title);
if you already have all the names of your new menu items in an array like itemsName
you can use something like this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
for(String itemName : itemsName){
int itemId = Arrays.asList(itemsName).indexOf(itemName);
menu.add(Menu.NONE, itemId, Menu.NONE, itemName);
}
return true;
}
and to manage the click event, on every one, you need to solve it depending on your array of actions to take, in your dynamic array, because you need to know deterministic every action that your menu item need to do or behave, as a menu option.
But in general your click listener for those menu items you use something like this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemId = item.getItemId();
//here you need the logic of what action
//your item is going to take
//but that is up to you
//how do you want to know what to do
//with a dynamic unknown array of options.
if (itemId == 0){
doSomething();
return true;
}
return false;
}
}
enjoy!!!

Related

Android: Change menu item from Log in to Log out

My menu has a item to Log in, but when you are logged in I want it to say Log out.
How?
If I'm going to change the item after its created, its probably through this method
#Override
public boolean onCreateOptionsMenu( Menu menu ) {
MenuInflater inflater = getMenuInflater();
inflater.inflate( R.menu.menu_main, menu );
return true;
}
Afaik the onCreateOptionsMenu() happens after the onCreate so putting any getItemId() for the menu there will give me a NullPointerException right away.
I want the app to find out if its supposed to use the string R.string.Logout if its logged in.
I dont even know what to search for for this issue. All I found was how to make a string implement names, like this answer https://stackoverflow.com/a/7646689/3064486
You should use onPrepareOptionsMenu(Menu menu) instead to update menu items
#Override
public boolean onPrepareOptionsMenu(Menu menu){
super.onPrepareOptionsMenu(menu);
MenuItem someMenuItem = menu.findItem(R.id.some_menu_item);
someMenuItem.setTitle("Log out");
return super.onPrepareOptionsMenu(menu);
}
To refresh Menu items call invalidateOptionsMenu();from Activity
From Android API guides: "If you want to modify the options menu based on events that occur during the activity lifecycle, you can do so in the onPrepareOptionsMenu() method. This method passes you the Menu object as it currently exists so you can modify it, such as add, remove, or disable items. (Fragments also provide an onPrepareOptionsMenu() callback.)"
After you inflate a menu, you can customize its items. To get each one, you must call findItem() with the item's id. In particular, you can use setTitle() to change the displayed string.
For example:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
if (mIsLoggedIn)
menu.findItem(R.id.action_login).setTitle("Log out");
return true;
}
where action_login is the id you set for this particular menu item in the menu's xml file.
private void updateUI() {
runOnUiThread(new Runnable() {
#Override
public void run() {
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
Menu menu = navigationView.getMenu();
MenuItem nav_signin = menu.findItem(R.id.nav_signin);
nav_signin.setTitle(MyApp.signedIn ? "Sign out" : "Sign in");
}
});
}

How to get all menu items id Android

I can get all the items from the actionbar with getItem() but I need to compare their id's with a particular id. How can I do this?
You can get actionBar menu items id by calling pMenu.getItem();
You can use changing methods of these menu items by overriding the method:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action buttons
switch(item.getItemId()) {
case R.id.action_stats:
break;
}
}

Hide menu options in action bar

I have one activity with 3 fragments (not tabs). I have several action bar items and I would like to hide them when a certain fragment is present. How can i go about this?
#Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
MenuItem item3 = menu.findItem(R.id.ID OF MENU);
item3.setVisible(false);
}
If you wish to hide ALL menu items, just do:
#Override
public void onAttach(final Activity activity) {
super.onAttach(activity);
setHasOptionsMenu(true);
}
#Override
public void onPrepareOptionsMenu(final Menu menu) {
super.onPrepareOptionsMenu(menu);
menu.clear();//This removes all menu items (no need to know the id of each of them)
}
What i understand with your post is::
You have 3 fragments
You have 3 different set of actionbar buttons for 3 fragments
My preferred approach::You can also find the menu items which you dont want to display in your current fragment and set their visibility
MenuItem item = menu.findItem(<id>);
item.setVisible(<true/false>);
ex::
MenuItem item1 = menu.findItem(R.id.searchID);
item1.setVisible(false);
You can also use this post for a different approach to handle this problem

How to distinguish two menu item clicks in ActionBarSherlock?

I have been working with ActionBarSherlock recently, and follwing various tutorials, I wrote this code to add items to Action bar
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("Refresh")
.setIcon(R.drawable.ic_action_refresh)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add("Search")// Search
.setIcon(R.drawable.ic_action_search)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
return true;
}
However, I dont know how to distinguish the two clicks.
Although I did find out that you have to Override onOptionsItemSelected to handle the clicks and also that a switch statement can be used to distinguish between clicks, but most tutorials use item ids from thier xml menus. Since I am not creating menus in xml how can i distinguish the clicks without ids.
private static final int REFRESH = 1;
private static final int SEARCH = 2;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, REFRESH, 0, "Refresh")
.setIcon(R.drawable.ic_action_refresh)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add(0, SEARCH, 0, "Search")
.setIcon(R.drawable.ic_action_search)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case REFRESH:
// Do refresh
return true;
case SEARCH:
// Do search
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Just check following
http://developer.android.com/guide/topics/ui/actionbar.html
which contains
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) { <--- here you can get it
case android.R.id.home:
// app icon in action bar clicked; go home
Intent intent = new Intent(this, HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
you can do ti by there Id in onOptionsItemSelected ................which can be set here also
http://thedevelopersinfo.wordpress.com/2009/10/29/handling-options-menu-item-selections-in-android/
http://developer.android.com/reference/android/view/Menu.html#add(int, int, int, java.lang.CharSequence)
use
public abstract MenuItem add (int groupId, int itemId, int order, CharSequence title)
Since: API Level 1
Add a new item to the menu. This item displays the given title for its label.
Parameters
groupId The group identifier that this item should be part of. This can be used to define groups of items for batch state changes. Normally use NONE if an item should not be in a group.
itemId Unique item ID. Use NONE if you do not need a unique ID.
order The order for the item. Use NONE if you do not care about the order. See getOrder().
title The text to display for the item.
Returns
The newly added menu item.

How can I programmatically disable all menu item in my activity

In Android, I create my menu item like this?
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, 0, 0, "Menu1");
menu.add(0, 1, 0, "Menu2");
return true;
}
How can I set all the menu item disabled programmatically (in another part of the code in my activity, not in onCreateOptionsMenu() method itself)?
You can use the groupId you set to disable/enable all the menu items at once, using menu.setGroupEnabled(). So for example, since you added the items to group 0, you'd do:
menu.setGroupEnabled(0, false);
Also, if you want to dynamically modify the menu, you should hook into onPrepareOptionsMenu(). onCreateOptionsMenu() is only called once per Activity, so it's good for setting up the initial menu structure, but onPrepareOptionsMenu() should be used to enable/disable menus as necessary later in the Activity.
add returns a MenuItem (which you can also retrieve from the Menu object), that you can store away into a variable.
MenuItem menu1;
public boolean onCreateOptionsMenu(Menu menu) {
menu1 = menu.add(0, 0, 0, "Menu1");
}
public void someOtherMethod() {
if(menu1 != null) {
// if it's null, options menu hasn't been created, so nevermind
menu1.setEnabled(false);
}
}
I prefer to hide them completely if they should not be used (instead of disabling them).
For that I do:
menu.clear();
and to recreate it:
invalidateOptionsMenu();
This also works for menu items added from fragments.
If still relevant:
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
for(int i=0; i<menu.size(); i++){
menu.getItem(i).setEnabled(isMenuEnabled);
}
}
and call invalidateOptionsMenu() then isMenuEnabled changed
If you have multiple occasions where you want to do something with all items of a menu (for example changing the 'checked' state) there is an elegant solution using Kotlin Extension Properties:
(Building on top of the answer of Valery)
In one place define the property 'items' of android.view.Menu:
import android.view.Menu
import android.view.MenuItem
val Menu.items: List<MenuItem>
get() {
val items = mutableListOf<MenuItem>()
for (i in 0 until this.size()) {
items.add(this.getItem(i))
}
return items
}
Now you can use it on any menu:
anyMenu.items.forEach { it.isEnabled = false }

Categories

Resources