java - Android overflow menu overlapping actionbar -
i'm trying make menu 2 items, "exit" , "settings", "exit" item located on actionbar, while "settings" item located in overflow menu.
however when click on menu icon, overflow menu overlaps "exit" item. there anyway can prevent behaviour?
menue.xml
<menu xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:android="http://schemas.android.com/apk/res/android"> <group android:checkablebehavior="single"> <item android:title="@string/setting" android:id="@+id/setting" android:orderincategory="2" app:showasaction="never" /> <item android:title="@string/exit" android:id="@+id/exit" android:orderincategory="1" app:showasaction="withtext|ifroom" android:icon="@drawable/exit" /> </group> </menu>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout 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" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.meunueexample.mainactivity"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="hello world!" app:layout_constraintbottom_tobottomof="parent" app:layout_constraintleft_toleftof="parent" app:layout_constraintright_torightof="parent" app:layout_constrainttop_totopof="parent" /> </relativelayout>
main_activity.java
package com.example.meunueexample; import android.app.*; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.menu; import android.view.menuitem; public class mainactivity extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); } @override public boolean oncreateoptionsmenu(menu menu) { // wil inflate menue in action bar getmenuinflater().inflate(r.menu.main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // here selected item form group option int id = item.getitemid(); // id of selected item these ids not user defined if (id == r.id.setting) { android.app.dialogfragment myfragment= new dialogfragment(); // made object of dialog fragment myfragment.show(getfragmentmanager() , "thedialog"); // thedialog object created in dialogfragment class return true; } else if (id == r.id.exit) { finish(); return true; } return super.onoptionsitemselected(item); } }
dialog_fragment.java
package com.example.meunueexample; import android.app.alertdialog; import android.app.dialog; import android.content.dialoginterface; import android.os.bundle; import android.widget.toast; /** * created aq on 9/10/2017. */ public class dialogfragment extends android.app.dialogfragment { @override public dialog oncreatedialog(bundle savedinstancestate) { // override method ake dialog box alertdialog.builder thedialog = new alertdialog.builder(getactivity()); // jiss activity ka oper na show hona hy thedialog.settitle("sample dialog"); thedialog.setmessage("hlw aq"); thedialog.setpositivebutton("ok", new dialoginterface.onclicklistener() { // on click listener on button clicked @override public void onclick(dialoginterface dialog, int which) { toast.maketext(getactivity(), "clicked ok", toast.length_short).show(); } }); // place semicolon there thedialog.setnegativebutton("cancel", new dialoginterface.onclicklistener() { // on click listener on button clicked @override public void onclick(dialoginterface dialog, int which) { toast.maketext(getactivity(), "clicked cancel", toast.length_short).show(); } }); // place semicolon there return thedialog.create(); } // may change abndroid version android manifest , build.gradle }
you can change behaviour creating style vertical offset, so:
<style name="overflowmenu" parent="widget.appcompat.popupmenu.overflow"> <!-- required pre-lollipop. --> <item name="overlapanchor">false</item> <item name="android:dropdownverticaloffset">-4.0dip</item> <!-- required lollipop. --> <item name="android:overlapanchor">false</item> <item name="android:dropdownverticaloffset">4.0dip</item> </style>
you can apply style in theme:
<item name="actionoverflowmenustyle">@style/overflowmenu</item>
Comments
Post a Comment