Android Popup Menu displays the menu below the anchor text if space is available otherwise above the anchor text. It disappears if you click outside the popup menu. Theandroid.widget.PopupMenu is the direct subclass of java.lang.Object class.
How to customize Popup Menu's background, margin, item text color, item text size?
1.styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="windowNoTitle">true</item> <item name="windowActionBar">false</item> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> <item name="popupMenuStyle">@style/MyPopupMenu</item> <item name="android:itemTextAppearance">@style/itemTextStyle.AppTheme</item> </style> <!-- To change Popup Menu background and margin--> <style name="MyPopupMenu" parent="@style/Widget.AppCompat.PopupMenu.Overflow"> <item name="android:popupBackground">@android:color/white</item> <item name="android:dropDownHorizontalOffset">10dp</item> <item name="android:dropDownVerticalOffset">5dp</item> </style> <!-- To change Popup Menu Item's text color and text size--> <style name="itemTextStyle.AppTheme" parent="@android:style/TextAppearance.Widget.IconMenu.Item"> <item name="android:textColor">#05437e</item> <item name="android:textSize">12sp</item> </style>
2.AndroidManifest.xml
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> -------- </activity> </application>
3.menu.xml
Create a resource folder named menu in res folder.Create menu.xml in menu folder.
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/item_view" android:title="@string/item_menu_04"/> <item android:id="@+id/item_delete" android:title="@string/item_menu_05"/> </menu>
4.activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" tools:context="com.ream.inex.Main2Activity"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Show Popup"/> </LinearLayout>
5.MainActivity.java
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); Button button = (Button) findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { PopupMenu popup = new PopupMenu(MainActivity.this, view); MenuInflater inflater = popup.getMenuInflater(); inflater.inflate(R.menu.inex_menu, popup.getMenu()); popup.setGravity(Gravity.START); popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { switch (item.getItemId()){ case R.id.item_view : Toast.makeText(MainActivity.this, "View", Toast.LENGTH_SHORT).show(); break; case R.id.item_delete : Toast.makeText(MainActivity.this, "Delete", Toast.LENGTH_SHORT).show(); break; } return false; } }); popup.show(); } }); } }
EmoticonEmoticon