How to change Android PopupMenu width - android

I am having trouble changing the width of the Appcompat PopupMenu because the menu item uses this layout abc_popup_menu_item_layout.xml in MenuPopupHelper.java, which set menu item minWidth to 196dip.
<android.support.v7.internal.view.menu.ListMenuItemView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?attr/dropdownListPreferredItemHeight"
android:minWidth="196dip"
android:paddingRight="16dip">
I tried to override minWidth as well as dropDownWidth in the following properties in my style.xml. I also tried to override minWidth in android:dropDownItemStyle, but it did not help. I start to wonder if it is possible to override this hard coded attribute of ListMenuItemView. Has anyone succeeded in modifying a PopupMenu width?
<style name="OrderDetailsActivity" parent="AppTheme">
<item name="android:textAppearanceLargePopupMenu">#style/OrderDetailsPopupMenuTextAppearanceLarge</item>
<item name="android:textAppearanceSmallPopupMenu">#style/OrderDetailsPopupMenuTextAppearanceSmall</item>
<item name="android:dropDownListViewStyle">#style/OrderDetailsListDropDownStyle</item>
<item name="dropDownListViewStyle">#style/OrderDetailsListDropDownStyle</item>
</style>
<style name="OrderDetailsPopupMenuTextAppearanceLarge" parent="#android:style/TextAppearance.DeviceDefault.Widget.PopupMenu.Large">
<item name="android:fontFamily">sans-serif-medium</item>
<item name="android:textSize">#dimen/font_size_medium</item>
</style>
<style name="OrderDetailsPopupMenuTextAppearanceSmall" parent="#android:style/TextAppearance.DeviceDefault.Widget.PopupMenu.Small">
<item name="android:fontFamily">sans-serif-medium</item>
<item name="android:textSize">#dimen/font_size_medium</item>
</style>
<style name="OrderDetailsListDropDownStyle" parent="Widget.AppCompat.ListView.DropDown">
<item name="android:divider">#drawable/list_divider</item>
</style>

You can prepare your custom popup menu item layout file and update this static field using reflection in onCreate() method of Your Application class:
private void updatePopUpMenuItemLayout() {
try {
Field field = MenuPopupHelper.class.getDeclaredField("ITEM_LAYOUT");
field.setAccessible(true);
field.set(null, R.layout.custom_popup_menu_item_layout);
} catch (Exception e) {
Log.e("TAG", e.getMessage());
}
}

Related

Material DatePicker Disable year option

I've got the following code:
private void openDatePicker() {
MaterialDatePicker picker =
MaterialDatePicker.Builder.datePicker()
.setTitleText("Select date")
.setTheme(R.style.DatePickerTheme)
.setSelection(MaterialDatePicker.todayInUtcMilliseconds())
.build();
picker.show(getParentFragmentManager(),"tag");
picker.addOnPositiveButtonClickListener(new MaterialPickerOnPositiveButtonClickListener() {
#Override
public void onPositiveButtonClick(Object selection) {
try {
mtnRecDate.setText(sdf.format(selection));
} catch (Exception e) {
FancyToast.makeText(getContext(),"Setting Date failed!",FancyToast.LENGTH_LONG,FancyToast.ERROR,false).show();
}
}
});
}
Style:
<!-- Picker styles and themes. -->
<style name="DatePickerTheme" parent="#style/ThemeOverlay.MaterialComponents.MaterialCalendar">
<item name="materialCalendarStyle">#style/Widget.MaterialComponents.MaterialCalendar</item>
<item name="materialCalendarFullscreenTheme">#style/ThemeOverlay.MaterialComponents.MaterialCalendar.Fullscreen</item>
<item name="materialCalendarTheme">#style/Widget.MaterialComponents.MaterialCalendar.MonthNavigationButton</item>
<item name="materialCalendarMonthNavigationButton">#style/Widget.MaterialComponents.MaterialCalendar.MonthNavigationButton</item>
</style>
I'm trying to hide the year option:
It should look more like this:
How can I achieve this. I tried changing the style theme (see above) with no results unfourtantly.
You can check this documentation.
As mentioned google documentation code below can fulfill your intent:
<style name="Theme.App" parent="Theme.MaterialComponents.*">
...
<item name="materialCalendarTheme">#style/ThemeOverlay.App.DatePicker</item>
</style>
<style name="ThemeOverlay.App.DatePicker"
parent="#style/ThemeOverlay.MaterialComponents.MaterialCalendar">
<item name="colorPrimary">#color/shrine_pink_100</item>
<item name="colorOnPrimary">#color/shrine_pink_900</item>
<item name="shapeAppearanceSmallComponent">#style/ShapeAppearance.App.SmallComponent</item>
<item name="shapeAppearanceMediumComponent">#style/ShapeAppearance.App.MediumComponent</item>
<!-- Customize text field of the text input mode. -->
<item name="textInputStyle">#style/Widget.App.TextInputLayout</item>
So you should change your style from MaterialCalendar to DatePicker.

BottomSheetDialogFragment theme with skins

How to combine BottomSheetDialogFragment theme with other themes?
My app has skins which are made using themes. BottomSheetDialogFragment should have rounded corners, and I achieve this using:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setStyle(BottomSheetDialogFragment.STYLE_NORMAL, R.style.CustomBottomSheetDialogTheme) /* hack to make background transparent */
}
Then in styles.xml:
<style name="CustomBottomSheetStyle" parent="Widget.Design.BottomSheet.Modal">
<item name="android:background">#android:color/transparent</item>
</style>
<style name="CustomBottomSheetDialogTheme" parent="Theme.MaterialComponents.Light.BottomSheetDialog">
<item name="bottomSheetStyle">#style/CustomBottomSheetStyle</item>
</style>
But If I extend from the Theme.MaterialComponents.Light.BottomSheetDialog I do not get the color scheme which I have defined in the skin theme.
So the question is: How to define Dialog theme inside the skin theme?
You can add in your app theme the bottomSheetDialogTheme attribute to set globally in your app the bottomsheetDialog style.
<style name="AppTheme" parent="Theme.MaterialComponents.*">
......
<item name="bottomSheetDialogTheme">#style/BottomSheetDialog_Rounded</item>
</style>
<style name="BottomSheetDialog_Rounded" parent="#style/ThemeOverlay.MaterialComponents.BottomSheetDialog">
<item name="bottomSheetStyle">#style/BottomSheet_Rounded</item>
</style>
Otherwise in your BottomSheetDialogFragment you can override the getTheme() method.
public class RoundedBottomSheetDialog extends BottomSheetDialogFragment {
//....
#Override public int getTheme() {
return R.style.BottomSheetDialog_Rounded;
}
}
Also to get rounded corners you can use something like:
<!-- BottomSheet Dialog-->
<style name="BottomSheetDialog_Rounded" parent="#style/ThemeOverlay.MaterialComponents.BottomSheetDialog">
<item name="bottomSheetStyle">#style/BottomSheet_Rounded</item>
</style>
<style name="BottomSheet_Rounded" parent="Widget.MaterialComponents.BottomSheet">
<item name="shapeAppearanceOverlay">#style/ShapeAppearanceBottomSheetDialog_Rounded</item>
</style>
<style name="ShapeAppearanceBottomSheetDialog_Rounded" parent="">
<item name="cornerFamily">rounded</item>
<item name="cornerSizeTopRight">16dp</item>
<item name="cornerSizeTopLeft">16dp</item>
<item name="cornerSizeBottomRight">0dp</item>
<item name="cornerSizeBottomLeft">0dp</item>
</style>
Try this instead
kotlin
override fun getTheme(): Int = R.style.CustomBottomSheetDialogTheme
java
#Override
public int getTheme() {
return R.style.CustomBottomSheetDialogTheme
}
override fun onCreateDialog(#Nullable savedInstanceState: Bundle?): Dialog
val dialog = BottomSheetDialog(context!!,R.style.FullScreenBottomSheet)
<style name="FullScreenBottomSheet"
parent="Theme.MaterialComponents.Light.BottomSheetDialog">
<item name="android:windowFullscreen">false</item>
<item name="android:colorBackgroundCacheHint">#null</item>
<item name="android:windowBackground">#color/transparent</item>
<item name="android:statusBarColor">#color/transparent</item>
</style>

Popup menu divider for App compat theme

I used app compat theme style .
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="android:popupMenuStyle">#style/PopupMenu</item>
<item name="android:itemTextAppearance">#style/myCustomMenuTextApearance</item>
<item name="android:listPopupWindowStyle">#style/PopupMenuStyle</item>
</style>
<style name="PopupMenuStyle" parent="Widget.AppCompat.ListPopupWindow">
<item name="android:divider">#drawable/devider</item>
<item name="android:dividerHeight">2dp</item>
</style>
<style name="PopupMenu" parent="#android:style/Widget.PopupMenu">
<item name="android:popupBackground">#color/search_panel_color</item>
<item name="android:textColor">#color/activity_button_text_color</item>
<item name="android:shadowColor">#color/activity_theam_color</item>
</style>
<style name="myCustomMenuTextApearance" parent="#android:style/TextAppearance.Widget.TextView.PopupMenu">
<item name="android:textColor">#color/activity_theam_color</item>
</style>
I want to add a divider in my menu item.
I've tried so many things, but the divider is not applying...
Is there any way to show the divider?
I also have the same problem. The solution is like this:
<style name="PopupMenuListView" parent="#style/Widget.AppCompat.ListView.DropDown">
<item name="android:divider">#000000</item>
<item name="android:dividerHeight">1dp</item>
</style>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:dropDownListViewStyle">#style/PopupMenuListView</item>
</style>
You can also refer to the following link:
How to add dividers between specific menu items?
I have one solution.you can design popup as per your choice by programming.use below code to display popup menu.
private ListPopupWindow listPopupWindow;
listPopupWindow = new ListPopupWindow(getApplicationContext());
listPopupWindow.setWidth(400);
listPopupWindow.setDropDownGravity(Gravity.CENTER);
listPopupWindow.setAdapter(new listpopupadapter(a, type));
listPopupWindow.setAnchorView(v);
listPopupWindow.show();
Here listpopupadapter is class to design your list as it below.
public class listpopupadapter extends BaseAdapter {
ArrayList<String> a;
String type;
public listpopupadapter(ArrayList<String> a, String type) {
this.a = a;
this.type = type;
}
#Override
public int getCount() {
return a.size();
}
#Override
public Object getItem(int position) {
return getItem(position);
}
#Override
public long getItemId(int position) {
return position;
}
#SuppressLint("ViewHolder")
#Override
public View getView(final int position, View convertView,
ViewGroup parent) {
// TODO Auto-generated method stub
View root = LayoutInflater.from(parent.getContext()).inflate(
R.layout.raw_filter, null);
}
}
From your theme style, I guessed you used Toolbar. Is your menu popup is showed from Toolbar? If so, you can customize as the following step.
Define the theme
<style name="AppToolbarPopupTheme" parent="Widget.AppCompat.PopupMenu.Overflow">
<item name="android:dropDownListViewStyle">#style/AppDropDownListViewStyle</item>
</style>
<style name="AppDropDownListViewStyle" parent="Widget.AppCompat.ListView.DropDown">
<item name="android:divider">#drawable/line_divider</item>
<item name="android:dividerHeight">1dp</item>
</style>
Then apply the theme to Toolbar
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:popupTheme="#style/AppToolbarPopupTheme">
</android.support.v7.widget.Toolbar>
if you haven't found the answer to this question then this is how it worked for me using the style:
<style name="PopupMenu">
<item name="android:itemBackground">#color/background_medium_gray</item>
<item name="android:background">#android:color/transparent</item>
<item name="android:textColor">#android:color/black</item>
<item name="android:colorBackground">#color/BackgroundGray</item>
<item name="android:dividerHeight">1dp</item>
</style>
Context context = new ContextThemeWrapper(getActivity(), R.style.PopupMenu);
final PopupMenu popupMenu = new PopupMenu(context, view);
final MenuInflater menuInflater = popupMenu.getMenuInflater();
I suggest you add dummy groups,try this way
<group>
<!--add it like as a separator-->
<item
android:title=""
android:showAsAction="always"
android:enabled="false" />
</group>
Using a Material theme removes dividers.May be this is the simple solution to this problem.
You can try this or any holo theme (i.e #android:style/Widget.ListPopupWindow) to get divider effect in popup
<!-- Change Overflow Menu ListView Divider Property -->
<style name="PopupMenuListView" parent="android:Widget.ListPopupWindow">
<item name="android:divider">#FF0000</item>
<item name="android:dividerHeight">2dp</item>
</style>
I've made a driver app that needed a popup the ride was coming, at that time I used this one. So please try this one. Maybe it will help.
<activity
android:name="driver_activity_name
android:theme="#android:style/Theme.Translucent.NoTitleBar" />

Why is the text selection tool is half shown and is not clickable

I have an AppCompatActivity which contains android.app.Fragment. In fragment I have a button on the toolbar which fires the method showDialog():
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_add_ingredient) {
showDialog();
return true;
}
return super.onOptionsItemSelected(item);
}
private void showDialog() {
// 1. Instantiate an AlertDialog.Builder with its constructor
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// 2. Chain together various setter methods to set the dialog
// characteristics
builder.setTitle(R.string.dialog_ingredient_add_title);
builder.setView(mActivityMain.getLayoutInflater().inflate(
R.layout.dialog_shopping_ingredient_add, null));
builder.setPositiveButton(R.string.dialog_ingredient_ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
builder.setNegativeButton(R.string.dialog_ingredient_cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
// 3. Get the AlertDialog from create()
dialog = builder.create();
dialog.show();
}
As it can be seen from the code, a android.support.v7.app.AlertDialog appears.
Picture 1. AlertDialog shown.
Then I select some text.
Picture 2. Text selected.
And somehow text selection tool shows only half of it and is unclickable.
My styles.xml file is below.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="AppTheme.Base" />
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<!-- colorPrimary is used, for instance, for the default ActionBar (but not Toolbar) background. We specify the same color for the toolbar background in toolbar.xml.. -->
<item name="colorPrimary">#color/color_primary</item>
<!-- colorPrimaryDark is used for the status bar (with the battery, clock, etc). -->
<item name="colorPrimaryDark">#color/color_primary_dark</item>
<!-- colorAccent is used as the default value for colorControlActivated which is used to tint widgets. -->
<item name="colorAccent">#color/color_accent</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="drawerArrowStyle">#style/DrawerArrowStyle</item>
<item name="android:itemTextAppearance">#style/MenuTextApearance</item>
<item name="colorControlNormal">#000000</item>
<item name="colorControlActivated">#color/color_highlight_text</item>
<item name="colorControlHighlight">#color/color_highlight_text</item>
<item name="actionMenuTextColor">#android:color/white</item>
<item name="android:actionMenuTextColor">#android:color/white</item>
<item name="windowActionModeOverlay">true</item>
<item name="android:textColorHighlight">#color/color_highlight_text</item>
<item name="actionModeBackground">#drawable/myapp_action_mode_background</item>
</style>
<style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
<item name="spinBars">true</item>
<item name="color">#android:color/white</item>
</style>
<style name="MenuTextApearance" parent="#android:style/TextAppearance.Widget.IconMenu.Item">
<item name="android:textColor">#000000</item>
</style>
<style name="ActionBarThemeOverlay" parent="">
<item name="android:textColorPrimary">#FFFFFF</item>
<item name="colorControlNormal">#FFFFFF</item>
<item name="colorControlHighlight">#00000000</item>
</style>
<style name="HeaderBar">
<item name="android:background">?colorPrimary</item>
</style>
<style name="HeaderBarTransparent">
<item name="android:background">#color/color_primary_transparent</item>
</style>
<style name="ActionBarPopupThemeOverlay" parent="ThemeOverlay.AppCompat.Light">
<item name="android:background">#00000000</item>
<item name="android:textColor">#FFFFFF</item>
</style>
Question:
How can I make the text selection tool fully visible and clickable?

How to change background color of ListView item?

There is the following method:
#Override
public void mark() {
ImageView image=(ImageView)view.findViewById(R.id.listItemRepeatingTypeImage);
image.setBackgroundColor(Color.RED);
image.invalidate();
Log.e("event", "mark");
view.setBackgroundColor(Color.GREEN);
view.invalidate();
}
As you can see, I try to change background color for View and ImageView (view is an item of ListView). This event becomes, but there is no new background. How can I fix it?
Try to apply this style to your ListView:
<style name="List">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">fill_parent</item>
<item name="android:background">#FFFFFF</item>
<item name="android:dividerHeight">1dp</item>
<item name="android:divider">#CCCCCC</item>
<item name="android:cacheColorHint">#FFFFFF</item>
<item name="android:listSelector">#00000000</item>
</style>
And in your xml layout this:
<ListView
android:id="#+id/listView"
style="#style/List"
android:paddingTop="1dp"/>

Categories

Resources