how to align menu items horizontally [duplicate] - android

This question already has answers here:
How to make an overflow menu like in Chrome?
(2 answers)
Closed 4 years ago.
How to create menu items aligned horizontally in the first row like google chrome
I created the first item with a menu child but by this way i got a row that send me to the other menu, So how to do that using menu file ? i don't want to use Dialog
Here is my menu file :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<group android:id="#+id/toolbar_menu"
android:visible="true">
<item
android:id="#+id/item_tabs"
android:icon="#drawable/ic_tabs_24dp"
android:title="#null"
app:showAsAction="always"/>
<item
android:id="#+id/icon_row_menu_id"
android:title="#null"
android:visible="true">
<menu>
<item
android:id="#+id/item_back"
android:icon="#drawable/ic_arrow_back_24dp"
android:title="#string/go_back" />
<item
android:id="#+id/item_forward"
android:icon="#drawable/ic_arrow_forward_24dp"
android:title="#string/go_forward" />
<item
android:id="#+id/item_bookmark_page"
android:icon="#drawable/ic_star_border_24dp"
android:title="#string/bookmark_this_page" />
<item
android:id="#+id/item_download_page"
android:icon="#drawable/ic_download_24dp"
android:title="#string/download_page" />
<item
android:id="#+id/item_info"
android:icon="#drawable/ic_info_outline_24dp"
android:title="#string/view_site_information" />
</menu>
</item>
<item
android:id="#+id/item_new_tab"
android:title="#string/new_tab" />
<item
android:id="#+id/item_new_incognito_tab"
android:title="#string/new_incognito_tab" />
<item
android:id="#+id/item_bookmarks"
android:title="#string/bookmarks" />
<item
android:id="#+id/item_history"
android:title="#string/history" />
<item
android:id="#+id/item_downloads"
android:title="#string/download" />
<item
android:id="#+id/item_settings"
android:title="#string/settings" />
</group>
</menu>
And here how i use it :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
mainMenu = menu;
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}

You can use app:actionLayout="#layout/layout_name" property of item tag like
<item
android:id="#+id/menu_id"
android:title="#string/title"
app:showAsAction="never"
app:actionLayout="#layout/layout_name"/>
and your layout_name is any layout file like
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
// add your horizontal menu item here
</LineearLayout>
and you can access this item
#Override 
public boolean onPrepareOptionsMenu(Menu menu)
{
MenuItem item = menu.findItem(R.id.menu_id);
Linearlayout rootView = (LinearLayout)item.getActionView();
YourControlClass control = (YourControlClass)
rootView.findViewById(R.id.control_id); 
return true; 
} 

you can also try with PopUp window this may help you check above codes
popup.xml layout file not menu,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#color/cardview_light_background"
android:layout_margin="20dp">
<LinearLayout
android:layout_margin="5dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
//your images with horizontal
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_margin="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
// your vertical list view
</LinearLayout>
</LinearLayout>
and you can Add the following code to the onClick() method:
LayoutInflater layoutInflater
= (LayoutInflater) context
.getSystemService(LAYOUT_INFLATER_SERVICE);
final View popupView = layoutInflater.inflate(R.layout.popup, null);
final PopupWindow popupWindow = new PopupWindow(
popupView,
ActionMenuView.LayoutParams.WRAP_CONTENT,
ActionMenuView.LayoutParams.WRAP_CONTENT, true);
popupWindow.setOutsideTouchable(true);
popupWindow.setFocusable(true);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
View parent = view.getRootView();
popupWindow.showAtLocation(parent, Gravity.TOP|Gravity.END,0,0);

Related

Android: Custom option menu

I am developing an application in which i want to show a one custom menu item which shows count and rest menu items are default type of item as shown in image below.
My code of menu.xml is as below:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/menu_1"
android:icon="#drawable/icon_1"
android:orderInCategory="101"
android:title="#string/option_1"
myapp:showAsAction="always"/>
<item
android:id="#+id/menu_2"
android:icon="#drawable/icon_2"
android:orderInCategory="102"
android:title="#string/option_2"
myapp:showAsAction="always"/>
<item
android:id="#+id/menu_3"
android:icon="#drawable/icon_3"
android:orderInCategory="103"
android:title="#string/option_3"
myapp:showAsAction="always"/>
<item
android:id="#+id/menu_4"
android:icon="#drawable/icon_4"
android:orderInCategory="104"
android:title="#string/option_4"
myapp:showAsAction="always"/>
<item
android:id="#+id/menu_5"
android:orderInCategory="105"
android:title="#string/option_5"
myapp:showAsAction="never"
android:actionLayout="#layout/actionbar_badge_layout" />
</menu>
My code of actionbar_badge_layout.xml is :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="right"
android:id="#+id/actionbar_badge_layout" >
<TextView
android:id="#+id/actionbar_notifcation_textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:padding="10dp"
android:text="#string/option_1"
android:textColor="#android:color/black"
android:layout_toLeftOf="#+id/actionbar_notifcation_count_textview"/>
<TextView
android:id="#+id/actionbar_notifcation_count_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:padding="10dp"
android:text="99"
android:textColor="#android:color/white"
android:background="#drawable/badge_circle" />
</RelativeLayout>
In my fragment what i done is :
#Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_myfragment, container,false);
setHasOptionsMenu(true);
((SlidingBaseActivity) getActivity()).setActionBarTitle("My Fragment");
((SlidingBaseActivity) getActivity()).keepMenuClosed(false);
((SlidingBaseActivity) getActivity()).makeActionOverflowMenuShown();
initView(view);
dialog = Helper.showPleaseWaitDialog(getActivity());
getdata();
return view;
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.findItem(R.id.option_1).setVisible(true);
menu.findItem(R.id.option_1).setOnMenuItemClickListener(this);
menu.findItem(R.id.option_2).setVisible(true);
menu.findItem(R.id.option_2).setOnMenuItemClickListener(this);
menu.findItem(R.id.option_3).setVisible(true);
menu.findItem(R.id.option_3).setOnMenuItemClickListener(this);
menu.findItem(R.id.option_4).setVisible(true);
menu.findItem(R.id.option_4).setOnMenuItemClickListener(this);
menu.findItem(R.id.option_5).setVisible(true);
menu.findItem(R.id.option_5).setOnMenuItemClickListener(this);
RelativeLayout badgeLayout = (RelativeLayout) menu.findItem(R.id.option_5).getActionView();
TextView textViewMenuText = (TextView) badgeLayout.findViewById(R.id.actionbar_notifcation_textview);
textViewMenuText.setText("Notification ");
TextView textViewCount = (TextView) badgeLayout.findViewById(R.id.actionbar_notifcation_count_textview);
textViewCount.setText("99");
super.onCreateOptionsMenu(menu, inflater);
}
But i am getting null pointer exception when i navigate to the respective fragment.
I follow the max voted answer from the this link.
What i am missing or doing wrong? Please guide me.
In your onCreateOptionsMenu you are trying this:
RelativeLayout badgeLayout = (RelativeLayout) menu.findItem(R.id.option_5).getActionView();
The 'id' to the item is wrong here. You have set the id as 'menu_5' and trying to fetch with 'option_5'. Which I see is exactly your 'title' to the menu item.

how to show menu item under overflow icon in android api 8+

How I can force menu item to show under overflow icon. I am providing app support from api level 8 and above. for now it is showing as actionview .
My menuLayout is as follows.
//main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/action_menu_setting"
android:title="#string/menuItemSetting"
app:showAsAction="ifRoom"/>
<item
android:id="#+id/action_signout"
android:title="#string/menuItemLogout"
app:showAsAction="ifRoom"/>
</menu>
in Java class
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
I have tried with other options of showAsAction but none of them worked. Please suggest me how I can show above two menu itme under overflow icon(when i click on over these two will appearer as actionlist.)
layout for custom action bar
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:background="#drawable/header"
android:orientation="vertical" >
<TextView
android:id="#+id/tvActionBarTitle"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:gravity="center"
android:textColor="#FFFFFF"
android:textSize="30sp" />
<Button.../>
<!-- your code for button -->
</LinearLayout>
You can call below function in onCreate and define button in this function as I have defined appName text view and its OnClick event
private void createCutomActionBarTitle()
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH)
{
this.getActionBar().setHomeButtonEnabled(true);
}
this.getActionBar().setDisplayShowCustomEnabled(true);
this.getActionBar().setDisplayShowTitleEnabled(false);
this.getActionBar().setBackgroundDrawable(drawable);
this.getActionBar().setIcon(logo);
LayoutInflater inflator = LayoutInflater.from(this);
View v = inflator.inflate(R.layout.custom_action_bar, null);
((TextView) v.findViewById(R.id.tvActionBarTitle)).setText(Global.ACTIVITY_NAME);
this.getActionBar().setCustomView(v);
}
Hope this will be useful for you :)
But clicking physical button of device will not trigger this button you have to look for it, as I don't have any idea about it
Could you post java code ? Do you have code below in your activity ?
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
getMenuInflater().inflate(R.menu.menu, menu);;
return super.onCreateOptionsMenu(menu);
}
Edit :
I alway use code like this(when i click action_settings it will show action list contains setting and bar, i think just replace android:title="#string/action_settings" with android:icon)
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="always"
android:title="#string/action_settings">
<menu>
<item
android:enabled="true"
android:visible="true"
android:title="setting"
android:id="#+id/setting">
</item>
<item
android:enabled="true"
android:visible="true"
android:title="B"
android:id="#+id/bar">
</item>
</menu>
</item>
<item
android:id="#+id/action_settings1"
android:orderInCategory="100"
android:showAsAction="always"
android:title="#string/action_settings"/>
</menu>
Hope it helps

Android action bar refresh animation - Repeated icon

I follow the answer from this question: link
But I get this on my app:
As you can see on the ActionBar's right side there are two refresh icons (they were actually rotating when I took the screenshot). But I just want one to be there. I'm quite sure the problem is one of them is the item from the Menu XML file and the other is the ImageView from the actionbar_inderterminate_progress.xml file.
Here is my menu.xml file:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.mypackage.android.design.appdesgin.MainActivity" >
<item android:id="#+id/action_search"
android:title="Search"
android:icon="#drawable/ic_action_search"
android:showAsAction="always"
android:actionViewClass="android.widget.SearchView"
android:visible="false" />
<item android:id="#+id/action_create_local_backup"
android:title="Search"
android:icon="#drawable/ic_action_new"
android:showAsAction="always"
android:visible="false" />
<item
android:id="#+id/action_refresh"
android:icon="#drawable/ic_menu_refresh"
android:showAsAction="always"
android:title="Refresh">
</item>
<TextView
android:id="#+id/status_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"/>
And here is my actionbar_inderterminate_progress.xml:
<?xml version="1.0" encoding="utf-8"?>
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/indeterminate_progress"
style="#android:style/Widget.ActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/app_name"
android:src="#drawable/ic_menu_refresh" />
My animation XML file:
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="1000"
android:interpolator="#android:anim/accelerate_decelerate_interpolator" />
And my java code:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.main, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_refresh:
refreshItem = item;
refresh();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void refresh() {
/* Attach a rotating ImageView to the refresh item as an ActionView */
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ImageView imageView = (ImageView) inflater.inflate(R.layout.refresh_button, null);
Animation rotation = AnimationUtils.loadAnimation(getActivity(), R.animator.rotate_refresh);
rotation.setRepeatCount(Animation.INFINITE);
imageView.startAnimation(rotation);
refreshItem.setActionView(imageView);
}
Can anyone point me where and what am I doing wrong to make it only display one animated refresh button?
I was inflating the refresh button two times, one in MyActivity class and the other one on the Fragment that requires the refresh button.

How to change Custom Font of Android Menu Item?

I have the following Android Java and XML code. I want to change the font of Menu Items of my app. I know only that we can change the font of TextView using setTypeface but not able to find anyway for Menu Item.
JAVA Code-:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_refresh1:
Toast.makeText(this, "Item1 Selected", Toast.LENGTH_SHORT)
.show();
break;
case R.id.action_refresh2:
Toast.makeText(this, "Item2 Selected", Toast.LENGTH_SHORT)
.show();
break;
default:
break;
}
}
XML Code-:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_refresh1"
android:orderInCategory="100"
android:showAsAction="always"
android:title="Item1"/>
<item
android:id="#+id/action_refresh2"
android:orderInCategory="100"
android:showAsAction="always"
android:title="Item2"/>
</menu>
I want to change the font of two menu item , but don't know how to integrate settypface for Menu Item.
Create Style in styles.xml
<style name="Style_TextView">
<item name="fontFamily">#font/myriadproregular</item>
</style>
Add Below code into android.support.design.widget.NavigationView
app:itemTextAppearance="#style/Style_TextView"
Note: It works for android.support.design.widget.NavigationView
you have to provide a custom layout for the menu item.
First, in your menu XML set one of the following
CASE 1 if you are using the support library:
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_save"
android:title="#string/action_save"
android:icon="#drawable/ic_action_save"
android:orderInCategory="100"
app:showAsAction="always"
app:actionLayout="#layout/layout_menu_save"/>
</menu>
CASE 2 if you are not using the support library:
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/action_save"
android:title="#string/action_save"
android:icon="#drawable/ic_action_save"
android:orderInCategory="100"
android:showAsAction="always"
android:actionLayout="#layout/layout_menu_save"/>
</menu>
Then in the actionLayout (layout_menu_save.xml in my example) write the following:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/background_transparent_stateful">
<com.example.YourCustomTypefaceTextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:gravity="center_vertical"
android:text="#string/action_save"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:gravity="center_vertical"
android:src="#drawable/ic_action_save"
android:contentDescription="#string/action_save"/>
</LinearLayout>
The background drawable (background_transparent_stateful) is useful for having touch feedback on your custom layout:
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true"
android:drawable="#20FFFFFF">
</item>
<item
android:drawable="#android:color/transparent">
</item>
</selector>
This way you will have a text with custom font on the left as a label with an icon on the right.
Obviously you can customize the layout as you prefer!
EDIT:
If you are using the support library and your activity Theme extends the AppCompatTheme you can get the typical Lollipop "ripple effect" for a better looking touch feedback.
Just replace the custom background with:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="?attr/selectableItemBackgroundBorderless">
...
</LinearLayout>
Just add the font of your choice to your base application theme. Note that that makes the selected font the base font for any unspecified font attributes.
<item name="android:fontFamily">#font/your_font</item>
XML
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_edit"
android:title="#string/edit"
android:visible="true"
app:showAsAction="always" />
</menu>
IN ACTIVITY OR IN FRAGMENT
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Typeface face = Typeface.createFromAsset(getActivity().getAssets(),"fonts/OpenSans-Regular.ttf"); // THIS
TypefaceSpan face = new TypefaceSpan("<REPLACE_WITH_FONT_NAME>"); // OR THIS
SpannableStringBuilder title = new SpannableStringBuilder(getContext().getString(R.string.edit));
title.setSpan(face, 0, title.length(), 0);
menu.add(Menu.NONE, R.id.action_edit, 0, title); // THIS
MenuItem menuItem = menu.findItem(R.id.action_edit); // OR THIS
menuItem.setTitle(title);
super.onCreateOptionsMenu(menu, inflater);
}
You can also use a SpannableStringBuilder when you add a menu item with a TypefaceSpan. For each menu item do something like
TypefaceSpan span = new TypefaceSpan("<REPLACE_WITH_FONT_NAME>");
SpannableStringBuilder title = new SpannableStringBuilder("My Menu Item Title");
title.setSpan(span, 0, title.length(), 0);
menu.add(Menu.NONE, id, index, title);
If your app only needs to work on Android Pie (API level 28) and above you can construct a TypefaceSpan from a Typeface in onPrepareOptionsMenu. Otherwise you can use a CustomTypefaceSpan class like this answer suggests:
public boolean onPrepareOptionsMenu(Menu menu) {
int customFontId = R.font.metropolis_medium;
for (int i = 0; i < menu.size(); i++) {
MenuItem menuItem = menu.getItem(i);
String menuTitle = menuItem.getTitle().toString();
Typeface typeface = ResourcesCompat.getFont(this, customFontId);
SpannableString spannableString = new SpannableString(menuTitle);
// For demonstration purposes only, if you need to support < API 28 just use the CustomTypefaceSpan class only.
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P) {
TypefaceSpan typefaceSpan = typeface != null ?
new TypefaceSpan(typeface) :
new TypefaceSpan("sans-serif");
spannableString.setSpan(typefaceSpan, 0, menuTitle.length(),
Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
} else {
CustomTypefaceSpan customTypefaceSpan = typeface != null ?
new CustomTypefaceSpan(typeface) :
new CustomTypefaceSpan(Typeface.defaultFromStyle(Typeface.NORMAL));
spannableString.setSpan(customTypefaceSpan, 0, menuTitle.length(),
Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
}
menuItem.setTitle(spannableString);
}
return true;
}
main.menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/settings"
android:onClick="openSettings"
android:title="#string/settings"
app:showAsAction="never" />
<item
android:id="#+id/about"
android:onClick="openAbout"
android:title="#string/about"
app:showAsAction="never" />
</menu>
Before & After:
I found this solution is usefull for me .hope it will help full new surfers.
your menu main.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<item android:id="#+id/item1"
android:title="User"
android:orderInCategory="100"
android:visible="true"
app:showAsAction="always"
app:actionViewClass="android.widget.Button"
/>
</menu>
After you inflated the custom menu, you can make a reference to the item of the menu. When you obtain the reference to the menu item, you will be able to customize the item to show icon and Unicode text. Your icon image file has to be stored in the
res/drawable folder.
#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);
//reference to the item of the menu
MenuItem i=menu.findItem(R.id.item1);
Button button_menu =(Button) i.getActionView();
if(itemuser!=null){
// Create Typeface object to use unicode font in assets folder
Typeface font= Typeface.createFromAsset(getApplicationContext().getAssets(),"fonts/arial.ttf");
// Set unicode font to menu item
button_menu .setTypeface(font);
// Set item text and color
button_menu .setText(getResources().getString(R.string._text));
button_menu .setTextColor(Color.WHITE);
// Make item background transparent
button_menu .setBackgroundColor(Color.TRANSPARENT);
// Show icon next to the text
Drawable icon=getApplicationContext().getResources().getDrawable( R.drawable.user);
button_menu .setCompoundDrawablesWithIntrinsicBounds( icon, null, null, null );
}
return true;
}
Simply set itemTextAppearance property of the NavigationView to system or custom style:
<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"
app:headerLayout="#layout/nav_header_main_navigation"
app:itemBackground="#color/white"
app:itemTextAppearance="#style/TextAppearance.AppCompat"
app:itemTextColor="#color/saintpatrickblue"
app:menu="#menu/activity_main_navigation_drawer"/>
The answer by #brahmyadigopula works well.
Here is just a more complete version for the sake of simplification:
The Menu
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<item
android:id="#+id/update"
android:showAsAction="always"
android:title="#string/menu_update"
tools:ignore="UnusedAttribute"
android:actionViewClass="android.widget.Button"/>
</menu>
In your Activity or Fragment:
// Inflater the menu based on the layout above
inflater.inflate(menuRes, menu);
// Set font type face
if (setCustomFontType){
final MenuItem menuItem = menu.findItem(R.id.update);
String title = menuItem.getTitle().toString();
Button button_menu = (Button) menuItem.getActionView();
button_menu.setTypeface("<REPLACE_WITH_FONT_NAME>");
button_menu.setText(title);
button_menu.setTextColor(Color.WHITE);
button_menu.setBackgroundColor(_getResources().getColor(R.color.transparent_color));
button_menu.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onOptionsItemSelected(menuItem);
}
});
}
super.onCreateOptionsMenu(menu, inflater);
If you are using android.support.design library then:
In theme:
app:itemTextAppearance="#style/bottom_navigation_textappreance"
if you are using Material design then:
In XML:
app:itemTextAppearanceActive="#style/bottom_navigation_textappreance"
app:itemTextAppearanceInactive="#style/bottom_navigation_textappreance"
In theme:
<style name="bottom_navigation_textappreance" parent="Widget.Design.BottomNavigationView">
<item name="fontFamily">#font/fira_sans_bold</item>
</style>

Popup menu on click of a button in action Bar

I am trying to implement an action bar in which one of the buttons on click shows a popup menu.
Here's the menu. XML (menu items in the action bar)
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/search"
android:icon="#drawable/ic_action_search"
android:orderInCategory="0"
android:showAsAction="always"
android:title="#string/menu_search"/>
<item
android:id="#+id/refresh"
android:icon="#drawable/ic_action_refresh"
android:orderInCategory="1"
android:showAsAction="always"
android:title="#string/menu_refresh"/>
<Item
android:id="#+id/popup"
android:icon="#drawable/ic_action_search"
android:onClick="showPopup"
android:orderInCategory="1"
android:showAsAction="always"
android:title="#string/menu_search" />
I wish to show a popup menu on the click of the item having id "#+id/popup".
here is the XML for the popup menu
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/item1"
android:icon="#drawable/ic_action_search"
android:orderInCategory="0"
android:showAsAction="always"
android:title="#string/menu_search"/>
<item
android:id="#+id/item2"
android:icon="#drawable/ic_action_search"
android:orderInCategory="1"
android:showAsAction="always"
android:title="#string/menu_search"/>
here is the onClick method for the button
public void showPopup(View v) {
PopupMenu popup = new PopupMenu(this, v);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.overflow, popup.getMenu());
popup.show();
}
And the problem is that no popup shows up on click of that button. Need help folks.
I found this here: http://developer.android.com/guide/topics/ui/menus.html
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/selectImg"
android:icon="#android:drawable/ic_dialog_dialer"
android:showAsAction="always">
<menu>
<item android:id="#+id/top"
android:title="#string/topimg"/>
<item android:id="#+id/bottom"
android:title="#string/botimg" />
</menu>
</item>
</menu>
You can put a menu within a menu to present sub-menus when the item is clicked. Then, in Java, you can use the same methods as usual.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
// View v = findViewById(R.id.f);
switch (item.getItemId()) {
case R.id.top:
//action
return true;
default:
return super.onOptionsItemSelected(item);
}
}
The id of 'top' in the xml is still recognized even though it is a sub menu. This worked for me and it looks just like the popup menu.
Hi every one, that's my own solution : i created the showPopup method, and then i called it in the onOptionsItemSelected like this :
public void showPopup(){
View menuItemView = findViewById(R.id.menu_save);
PopupMenu popup = new PopupMenu(getActivity(), menuItemView);
MenuInflater inflate = popup.getMenuInflater();
inflate.inflate(R.menu.popup, popup.getMenu());
popup.show();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_save:
showPopup();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
popup.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/decon"
android:showAsAction="ifRoom"
android:title="#string/decon"/>
<item
android:id="#+id/mRes"
android:showAsAction="ifRoom"
android:title="#string/mesRes"/>
</menu>
main.xml => it's called onCreateOptionsMenu
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_save"
android:enabled="true"
android:icon="#drawable/action_save"
android:showAsAction="ifRoom|withText"
android:title="#string/action_save"
android:visible="true"/>
</menu>
Finally i
implements PopupMenu.OnMenuItemClickListener to #Override onMenuItemClick method.
As the popup menu is a MENU, you have to handle this by implementing the "onOptionsItemSelected". You'll be able to say what to do for each menu option. It will replace the "onClick" option you defined and will be called automatically.
Try to change 'this' to getActivity().
public void showPopup(View v) {
PopupMenu popup = new PopupMenu(getActivity(), v);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.overflow, popup.getMenu());
popup.show();
}
Hope it helps..!!
I found a solution to this. Instead to using the menu XML to inflate the popup menu, I made a XML layout file.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#8b8989"
android:orientation="vertical"
android:padding="10dip" >
<TextView
android:id="#+id/menuItem1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="#string/menu1" />
<TextView
android:id="#+id/menuItem2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="#string/menu2" />
<TextView
android:id="#+id/menuItem3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="#string/menu3" />
</LinearLayout>
and i changed the onClick method
public void showPopup(View v) {
LayoutInflater inflater = (LayoutInflater) MainActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
PopupWindow pw = new PopupWindow(inflater.inflate(
R.layout.overflow_layout, null, false), 300, 400, true);
pw.showAtLocation(findViewById(R.id.container), Gravity.CENTER, 0,
0);
}
This solved the issue
android:onClick="popup"
may be you should change it to android:onClick="showPopup"?

Categories

Resources