How can we change the width of PopupMenu, and center the text? - android

I'm having a hard time in android studio.
I want the width changed to match the text size in PopupMenu, and the text to be centered.
In activity, PopupMenu used :
Context wrapper = new ContextThemeWrapper(PatientListActivity.this, R.style.YOURSTYLE);
PopupMenu popupMenu = new PopupMenu(wrapper, typeView);
popupMenu.getMenuInflater().inflate(R.menu.menu_type, popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
And my PopupMenu's menu item is :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/item_all"
android:title="All"/>
<item android:id="#+id/item_p"
android:title="P"/>
<item android:id="#+id/item_et"
android:title="ET"/>
<item android:id="#+id/item_no"
android:title="ㅡ"/>
</menu>
I specified PopupMenu's style like this, but nothing changed.
<style name="YOURSTYLE" parent="Widget.AppCompat.PopupMenu">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_gravity">center_horizontal</item>
</style>
I hope that the width size in PopupMenu can be changed, and the text can be arranged in the center.

Related

¿Change text color main menu android?

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.

How to display submenu from PopupMenu without Dismissing Android

I have Java show popup menu function and xml below It work, but I want to display both Min menu and sub menu when user click on main Item.
For example When user Click on Price display child of price, and don't hide the main like price or type of product..., and if click type of product display child of type of product but don't hide main.
Here the result I expect to be.
Show popup menu java
//Filter Menu
public void showPopupMenu(View view) {
// inflate menu
android.widget.PopupMenu popup = new PopupMenu(getApplicationContext(), view);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.dailydeal_filter, popup.getMenu());
//popup.setOnMenuItemClickListener(new MyMenuItemClickListener());
popup.show();
}
Here is my xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/category"
android:title="Price">
<menu>
<item android:title="Price From 10-100"></item>
<item android:title="Price From 100-200"></item>
<item android:title="Price From 200-300"></item>
<item android:title="Price Higher then 300"></item>
</menu>
</item>
<item android:id="#+id/type_product"
android:title="Type Product">
<menu>
<item android:title="Buy Sale"></item>
<item android:title="Business Directory"></item>
</menu>
</item>
</menu>

Change Popup Menu Items font size dynamatically Android

I am creating an android app which has a popup menu and it is opened when a button is clicked and I want to increase the text size of menu items in that popup menu dynamically(based on the users input).
Can anyone help on how do that programatically?
In my class:
menuButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Creating the instance of PopupMenu
PopupMenu popup = new PopupMenu(getActivity(), menuButton);
//Inflating the Popup using xml file
popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
//registering popup with OnMenuItemClickListener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.one:
// Some Stuffs
break;
case R.id.two:
// Some Stuffs
break;
default:
break;
}
return true;
}
});
And my xml file is ::
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fav_menu">
<group android:id="#+id/first_section" android:checkableBehavior="none" >
<item
android:id="#+id/one"
android:title="#string/sort"
android:visible="true"
/>
</group>
<group android:id="#+id/first_two" android:checkableBehavior="none">
<item
android:id="#+id/two"
android:title="#string/sort"
android:visible="true"
/>
</group>
</menu>
This is very easy with SpannableString:
//### format text of popup-items ###
for(int i=0;i<popup.getMenu().size();i++){
MenuItem tmpItem = popup.getMenu().getItem(i);
String tmpText = tmpItem.getTitle().toString();
SpannableString s = new SpannableString(tmpText);
s.setSpan(new RelativeSizeSpan(1.4f), 0, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
s.setSpan(new ForegroundColorSpan(Color.BLUE), 0, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tmpItem.setTitle(s);
}
//##################################
In my style file:
<style name="menuStyle">
<item name="android:layoutDirection">rtl</item>
<item name="android:textColor">#color/onyx</item>
<item name="android:textSize">15sp</item>
<item name="android:fontFamily">#font/ge_ss_two_light</item>
</style>
In my java file:
Context myContext = new ContextThemeWrapper(context, R.style.menuStyle);
MenuBuilder menuBuilder = new MenuBuilder(context);
MenuInflater inflater = new MenuInflater(context);
inflater.inflate(R.menu.options_menu, menuBuilder);
You can change the text size by adding this code to styles.xml and use it in manifest file.
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:popupMenuStyle">#style/PopupMenu</item>
<item name="android:textAppearanceLargePopupMenu">#style/myPopupMenuTextAppearanceLarge</item>
<item name="android:textAppearanceSmallPopupMenu">#style/myPopupMenuTextAppearanceSmall</item>
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
<style name="PopupMenu" parent="#android:style/Widget.PopupMenu">
<item name="android:popupBackground">#android:color/white</item>
<item name="android:textSize">12sp</item>
</style>
<style name="myPopupMenuTextAppearanceSmall" parent="#android:style/TextAppearance.DeviceDefault.Widget.PopupMenu.Small">
<item name="android:textSize">15sp</item>
</style>
<style name="myPopupMenuTextAppearanceLarge" parent="#android:style/TextAppearance.DeviceDefault.Widget.PopupMenu.Large">
<item name="android:textSize">25sp</item>
</style>

how to change the color of PopupMenu background

I am using the following code for creating pop up menu..
i need to change the background color of the menu.. how can i do that. please help.
//Creating the instance of PopupMenu
PopupMenu popup = new PopupMenu(ctx, holder.ll_overflow);
//Inflating the Popup using xml file
popup.getMenuInflater().inflate(R.menu.bday_contacts_menu, popup.getMenu());
//registering popup with OnMenuItemClickListener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
return true;
}
});
popup.show();//showing popup menu
You can change it using styles
<style name="AppBaseTheme" parent="#android:style/Theme.Light.NoTitleBar">
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:popupMenuStyle">#style/PopupMenu</item>
</style>
<style name="PopupMenu" parent="#android:style/Widget.PopupMenu">
<item name="android:popupBackground">#android:color/white</item>
<item name="android:textColor">#FF01F0</item>
<item name="android:textSize">12sp</item>
</style>

Change text color of a single item in a PopupMenu

I have a popup menu that appears when the user clicks an image. The code used is:
PopupMenu popupMenu = new PopupMenu(mContext, mImageView);
popupMenu.setOnMenuItemClickListener(MyClass.this);
popupMenu.inflate(R.menu.menu_my_class_options);
popupMenu.show();
The menu has three actions, and I would like that the text in one of them to be colored red (instead of black).
Is that possible?
I've found some answers (e.g., here) that show how to do this with the activity's Options Menu, but they require access to the onCreateOptionsMenu method...
Thanks in advance.
-- EDIT --
The 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">
<item
android:id="#+id/item_edit"
app:showAsAction="ifRoom|withText"
android:title="#string/action_edit_address"
android:visible="true"/>
<item
android:id="#+id/item_retake_photo"
app:showAsAction="ifRoom|withText"
android:title="#string/action_retake_photo"
android:visible="true"/>
<item
android:id="#+id/item_delete"
app:showAsAction="ifRoom|withText"
android:title="#string/action_delete_shipment"
android:visible="true"/>
</menu>
You can achieve this by following steps.
In your style.xml--- add the lines
<style name="style" parent="android:Theme.Holo.Light">
<item name="textAppearanceSmallPopupMenu">#style/myPopupMenuTextAppearanceColor</item>
<item name="android:popupMenuStyle">#style/myPopupMenuStyle</item>
</style>
And below code
<style name="myPopupMenuStyle" parent="#style/Widget.AppCompat.Light.PopupMenu">
</style>
<style name="myPopupMenuTextAppearanceColor" parent="#style/TextAppearance.AppCompat.Light.Widget.PopupMenu.Small">
<item name="android:textColor">#000000(Your Color Code)</item>
</style>
Update for single menu item color
For single menu item color you can simply use the below code
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.your_menu, menu);
int positionOfMenuItem = 0;
// 0 or whatever your item position at which you want to change the color...
MenuItem item = menu.getItem(positionOfMenuItem);
SpannableString s = new SpannableString("My red MenuItem");
s.setSpan(new ForegroundColorSpan(Color.RED//your color), 0, s.length(), 0);
item.setTitle(s);
}
I know that this answer is belated, but maybe it will be useful for somebody. Try to use this
<string name="action_delete_shipment"><font fgcolor='#FF1400'>Delete Shipment</font></string>
Based on the answer of #Shadik Khan, actually you just need to create SpannableString, change the color or it before putting the item into the menu list. Like below
if (messageActionMap.keyAt(i) == LIST_ITEM_DELETE) {
SpannableString itemRecall = new SpannableString(messageActionMap.valueAt(i));
itemRecall.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.red)), 0, itemRecall.length(), 0);
mMessagePopupMenu.getMenu().add(Menu.NONE, messageActionMap.keyAt(i), 1, itemRecall);
}

Categories

Resources