I have this menu structure for my navigation view:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_clients"
android:icon="#drawable/ic_group"
android:title="#string/nav_clients"/>
<item
android:id="#+id/nav_products"
android:icon="#drawable/ic_box"
android:title="#string/nav_products" />
<item
android:id="#+id/nav_orders"
android:icon="#drawable/ic_buy"
android:title="#string/nav_orders" />
</group>
<item android:title="#string/nav_title_app">
<menu>
<item
android:id="#+id/nav_settings"
android:icon="#drawable/ic_settings_black"
android:title="#string/nav_settings" />
<item
android:id="#+id/nav_sync"
android:icon="#drawable/ic_sync_black"
android:title="#string/nav_sync" />
</menu>
</item>
I have succeeded to change the menu items text size, but only the item which contains a menu didn't changed the size?
Any ideas?
I haven't found any official way of doing so, neither by style, but you can achieve it programmatically, take a look below.
private void setMenuHeadTitleSize(int itemId) {
Menu menu = mNavigationView.getMenu();
MenuItem menuItem = menu.findItem(itemId);
MenuItem subMenuItem = menuItem.getSubMenu().getItem();
String title = String.valueOf(subMenuItem.getTitle());
SpannableString mNewTitle = new SpannableString(title);
mNewTitle.setSpan(new RelativeSizeSpan(1.1f), 0, title.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
subMenuItem.setTitle(mNewTitle);
}
Also give an Id to your SubMenu item
<item android:id="#+id/nav_top"
android:title="#string/nav_title_app">
<menu>
<item
android:id="#+id/nav_settings"
android:icon="#drawable/ic_settings_black"
android:title="#string/nav_settings" />
<item
android:id="#+id/nav_sync"
android:icon="#drawable/ic_sync_black"
android:title="#string/nav_sync" />
</menu>
</item>
and finally, once NavigationView is inflated, and the menu attached, call the function above
setMenuHeadTitleSize(R.id.nav_top);
Related
My menu file menu_main.xml, the "Create" item is just a title, so it should be disabled.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.amazon.device.controllermanager.docsuilocal.MainActivity">
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never" />
<item
android:icon="#drawable/ic_add"
android:title="#string/menu_create"
app:showAsAction="always">
<menu>
<group android:id="#+id/sorting" >
<item
android:id="#+id/title"
android:title="Create"
app:showAsAction="never" />
</group>
<item
android:id="#+id/action1"
android:title="Document"
app:showAsAction="never" />
<item
android:id="#+id/action2"
android:title="Spreadsheet"
app:showAsAction="never" />
<item
android:id="#+id/action3"
android:title="Presentation"
app:showAsAction="never" />
</menu>
</item>
</menu>
The id #id/title is just a title, hence it shouldn't be clickable, but it should be bold in color, so I do this in my java code
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem createItem = menu.findItem(R.id.title);
SpannableString spanString = new SpannableString(createItem.getTitle().toString());
spanString.setSpan(new StyleSpan(Typeface.BOLD), 0, spanString.length(), 0);
createItem.setTitle(spanString);
createItem.setCheckable(false);
createItem.setEnabled(false);
MenuCompat.setGroupDividerEnabled(menu, true);
return true;
}
The problem is it gets the grey disabled colour, but I want it have bold text and to not be clickable, as its purpose is to act like a title.
Anyway I could overwrite the grey colour in this particular menu item alone?
You can add one more span to the spanString to se the foreground color using ForegroundColorSpan:
spanString.setSpan(new ForegroundColorSpan(ResourcesCompat
.getColor(resources, R.color.gray, null)),
0, spanString.length, 0);
The color value is <color name="gray">#606060</color>
I want to change the color of the text of an item that groups a menu.
The xml file:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_mi_cuenta"
android:icon="#mipmap/ic_mi_cuenta"
android:title="#string/mi_cuenta" />
</group>
<item android:title="#string/herramientas">
<menu>
<item
android:id="#+id/nav_configuracion"
android:icon="#mipmap/ic_config"
android:title="#string/configuraci_n" />
</menu>
</item>
</menu>
I wnat to change text color of:
<item android:title="#string/herramientas">
As you can see, Herramientas comes out in black and as the background is also black it barely looks. I can not find any property to change it. The color of the item 'Mis datos' and 'Configuración' change it programmatically as follows from an external json file of a server:
colorElegido = getParseColor(json.getString("colorMenuLateral"));
navigationView.setBackgroundColor(colorElegido);
colorElegido = getParseColor(json.getString("colorFuenteMenuLateral"));
ColorStateList colorList = getColorList(colorElegido);
navigationView.setItemTextColor(colorList);
The following code will workout,
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_mi_cuenta"
android:icon="#mipmap/ic_mi_cuenta"
android:title="#string/mi_cuenta" />
</group>
<item
android:id="#+id/herramientas"
android:title="#string/herramientas">
<menu>
<item
android:id="#+id/nav_configuracion"
android:icon="#mipmap/ic_config"
android:title="#string/configuraci_n" />
</menu>
</item>
</menu>
Add this in your styles,
<style name="TextAppearance44">
<item name="android:textColor">#FF0000</item>
<item name="android:textSize">20sp</item>
</style>
<style name="NavigationDrawerStyle">
<item name="android:textSize">16sp</item><!-- text size in menu-->
<item name="android:textColor">#880ACE0A</item>
<item name="itemTextColor">#880ACE0A</item>
</style>
And in your activity,
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
Menu menu = navigationView.getMenu();
MenuItem tools= menu.findItem(R.id.herramientas);
SpannableString s = new SpannableString(tools.getTitle());
s.setSpan(new TextAppearanceSpan(this, R.style.TextAppearance44), 0, s.length(), 0);
tools.setTitle(s);
navigationView.setNavigationItemSelectedListener(this);
Change according to you,
And other text color change you can use like in your xml file,
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
style="#style/NavigationDrawerStyle"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
to make a custom popup make an ImageView in toolbar
private ImageView setting;
public void menuOptions() {
PopupMenu popup = new PopupMenu(MainActivity.this, setting);
// Inflating the Popup using xml file
popup.getMenuInflater()
.inflate(R.menu.menu_main, popup.getMenu());
//registering popup with OnMenuItemClickListener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.button1:
return (true);
case R.id.button2:
// block contacts
return (true);
}
return true;
}
});
popup.show(); //showing popup menu
}
on Button click call above function
menuOptions();
in the layout you inflate menu_main.xml, make your desired view.
I implemented the Navigation View (Support Library 24.0.0):
But a strange behavior occurred in which i have been trying to control since.
Here is my drawer.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="Categories">
<menu>
<group android:checkableBehavior="single">
<item
android:id="#+id/navigation_item_1"
android:icon="#drawable/ic_news"
android:title="Top Stories" />
<item
android:id="#+id/navigation_item_2"
android:icon="#drawable/ic_music"
android:title="Entertainment" />
<item
android:id="#+id/navigation_item_3"
android:icon="#drawable/ic_sport"
android:title="Sport" />
<item
android:id="#+id/navigation_item_4"
android:icon="#drawable/ic_lifestyle"
android:title="Lifestyle" />
<item
android:id="#+id/navigation_item_5"
android:icon="#drawable/ic_tech"
android:title="Technology" />
<item
android:id="#+id/navigation_item_6"
android:icon="#drawable/ic_world"
android:title="International" />
</group>
</menu>
</item>
</menu>
But the result is :
in which only one should be selected.
I solved it using :
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem item) {
for (int i = 0; i < 6; i++) {
MenuItem top = navigationView.getMenu().getItem(0).getSubMenu().getItem(i);
if (top.isChecked()) top.setChecked(false);
}
return true;
}
});
Unchecking all items in the menu
return true, that checks the clicked item.
I'm trying to declare the slider items as a Button but it keeps on crashing. I'm trying to access a different Activity when an item is pressed, and trying out the Log out button. Here's the xml of the activity_main_drawer.
<group android:checkableBehavior="single">
<item
android:id="#+id/home"
android:icon="#drawable/menu_home"
android:title="Home"
/>
<item
android:id="#+id/nav_medication"
android:icon="#drawable/menu_medication"
android:title="Medication" />
<item
android:id="#+id/notices"
android:icon="#drawable/menu_notices"
android:title="Doctor's Notices" />
<item
android:id="#+id/meds"
android:icon="#drawable/menu_buy"
android:title="Buy Medicines" />
<item
android:id="#+id/options"
android:icon="#drawable/ic_menu_manage"
android:title="Medic Options">
<menu>
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_add"
android:icon="#drawable/menu_add"
android:title="Add Medicines" />
<item
android:id="#+id/nav_dispense"
android:icon="#drawable/menu_dispense"
android:title="Dispense" />
</group>
</menu>
</item>
</group>
<item android:title="Settings">
<menu>
<item
android:id="#+id/account"
android:icon="#drawable/menu_account"
android:title="Account" />
<item
android:id="#+id/help"
android:icon="#drawable/menu_help"
android:title="Help" />
<item
android:id="#+id/bLogout"
android:icon="#drawable/menu_help"
android:title="Logout"
/>
</menu>
</item>
Tried using:
Button bLogout;
bLogout = (Button) findViewById(R.id.bLogout);
bLogout.setOnClickListener(this);
But it still crashes.
Use this NavigationView item clicked
navView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id. bLogout:
// your logic goes here
break;
}
}
});
I want to show icon in Option Menus. I tried many methods but I can't find success.In my toolbar some icon already put but i want icon in my Option menu.
I want same as below image:-
My menu.xml:-
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item
android:id="#+id/action_search"
android:title="#string/action_search"
android:orderInCategory="1"
android:icon="#drawable/search"
app:showAsAction="ifRoom" />
<item
android:id="#+id/action_cart"
android:title="#string/action_search"
android:orderInCategory="2"
android:icon="#drawable/shoppingcart"
android:actionLayout="#layout/feed_update_count"
app:showAsAction="ifRoom" />
<item
android:id="#+id/menu_overflow"
android:title="#string/my_cart"
android:icon="#drawable/ic"
android:orderInCategory="11111"
app:showAsAction="always">
<menu>
<item
android:id="#+id/login"
android:title="#string/login"
android:orderInCategory="1"
android:icon="#drawable/account"
app:showAsAction="always|withText"/>
<item
android:id="#+id/my_order"
android:title="#string/my_order"
android:orderInCategory="2"
android:icon="#drawable/track"
app:showAsAction="withText"/>
<item
android:id="#+id/wishlist"
android:title="#string/wishlist"
android:orderInCategory="3"
android:icon="#drawable/wishlist"
app:showAsAction="withText"/>
</menu>
</item>
</menu>
I am new in android developing anyone can help me how to show icon in option menu. Thanks in advance!
By default the overflow menu will disable the icon and only show text, you need some workaround.
1st way
You can enable it by adding the following codes into your class
#Override
public boolean onMenuOpened(int featureId, Menu menu)
{
if(featureId == Window.FEATURE_ACTION_BAR && menu != null){
if(menu.getClass().getSimpleName().equals("MenuBuilder")){
try{
Method m = menu.getClass().getDeclaredMethod(
"setOptionalIconsVisible", Boolean.TYPE);
m.setAccessible(true);
m.invoke(menu, true);
}
catch(NoSuchMethodException e){
Log.e(TAG, "onMenuOpened", e);
}
catch(Exception e){
throw new RuntimeException(e);
}
}
}
return super.onMenuOpened(featureId, menu);
}
2nd way
Create your own overflow item and a nested menu to show both the icon and the text.
Example,
<item
android:id="#+id/overflow"
android:icon="#drawable/ic_overflow_white"
android:orderInCategory="201"
android:title="#string/overflow"
app:showAsAction="always">
<menu>
<item
android:id="#+id/cab_menu_select_all"
android:icon="#drawable/ic_select_all_grey"
android:orderInCategory="100"
android:title="#string/cab_menu_select_all"
app:showAsAction="always|withText"></item>
</menu>
</item>
try this code its works for you
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:showAsAction="always"
android:title="#string/action_search"/>
<!-- Settings, should always be in the overflow -->
<item
android:id="#+id/action_location_found"
android:icon="#drawable/ic_action_location_found"
android:showAsAction="always"
android:title="#string/action_location_found"/>
<item
android:id="#+id/menu_overflow"
android:icon="#drawable/ic_action_menu"
android:orderInCategory="11111"
android:showAsAction="always">
<menu>
<!-- Refresh -->
<item
android:id="#+id/action_refresh"
android:icon="#drawable/ic_action_refresh"
android:showAsAction="ifRoom"
android:title="#string/action_refresh"/>
<!-- Help -->
<item
android:id="#+id/action_help"
android:icon="#drawable/ic_action_help"
android:showAsAction="never"
android:title="#string/action_help"/>
<!-- Check updates -->
<item
android:id="#+id/action_check_updates"
android:icon="#drawable/ic_action_refresh"
android:showAsAction="never"
android:title="#string/action_check_updates"/>
</menu>
</item>
</menu>
- See more at: http://www.theappguruz.com/blog/android-working-android-actionbar#sthash.ArNQTEBF.dpuf
Create your menu xml as follow
<item
android:id="#+id/menu_item_action_parameters"
android:title="#string/text_parameters"
android:icon="#drawable/ic_menu_parameter"
app:showAsAction="ifRoom|withText"/> >
<menu>
<item
android:id="#+id/action_dropdown1"
android:title="#string/dropdown_1" />
<item
android:id="#+id/action_dropdown2"
android:title="#string/dropdown2" />
<item
android:id="#+id/action_dropdown3"
android:title="#string/dropdown3" />
</menu>
</item>
<item
more item
</item>
Then
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.action_dropdown1:
.....
return true;
case R.id.action_dropdown2:
.....
return true;
.....
default:
return super.onOptionsItemSelected(item);
}
}
this is completely working code.Mark this answer up if it helps. :) Happy coding.
you Need to add tools:context="your class" to menu tag
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".activities.BaseActivity">
<item
android:id="#+id/action_notification1"
android:icon="#drawable/one"
android:title="action_notification"
app:showAsAction="always"/>
<item
android:id="#+id/action_notification2"
android:icon="#drawable/two"
android:title="action_notification"
app:showAsAction="always"/>
<item
android:id="#+id/action_notification3"
android:icon="#drawable/three"
android:title="action_notification"
app:showAsAction="always"/>
</menu>
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
you shold write tool:context to menu tag then run you will get icons to your text