3 dot setting menu for android apps with custom ti

2019-08-31 02:02发布

in an android app, if you create a new project,

then automatically the 3 dot settings menu is created on phones where it is needed

and it is handled the same way as it would have in older versions by:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

if you create a custom title, you have to add the 3 dot menu yourself

how do I know when it is needed? meaning phones that don't have the settings button

also how do i create a context menu that is customized and attached to the 3 dot button

Edit:

after a disscusion with Shobhit Puri, I understood that I was not considering the actionbar, since I am using a minimum API 8, I don't have it,

so there is the option that CommonsWare just supplied to check if the settings menu exists (I still need to check if it exists in API 8)

Shobhit Puri's suggestion was:

requestWindowFeature(Window.FEATURE_NO_TITLE); 
setContentView(...) ; 
ActionBar ab = getActionBar(); 
ab.setTitle("My Title"); 
ab.setSubtitle("sub-title") ;

but that of cores requires API 11 or the support library V7 either way I am excepting Shobhit Puri's answer, because of all his help, and I will post my final solution when I know it works

also thanks to CommonsWare for a nice answer

Edit2:

I decided to go with CommonsWare solution for now, I wrote it like this:

if (android.os.Build.VERSION.SDK_INT >= 14) {
            ViewConfiguration vc = ViewConfiguration.get(this);
            if (!vc.hasPermanentMenuKey()) {
                setting_dots.setVisibility(View.VISIBLE);
                setting_dots.setOnClickListener(this);
                registerForContextMenu(setting_dots);
            }
        }

ideally I think you should use the actionbar, because it provides you with most of the work, but it has a lot of compatability issues with API 8 which for now I would rather avoid

3条回答
一夜七次
2楼-- · 2019-08-31 02:17

how do I know when it is needed? meaning phones that don't have the settings button

Call hasPermanentMenuKey() on a ViewConfiguration.

also how do i create a context menu that is customized and attached to the 3 dot button

By programming. Since you are not using an action bar, it is impossible to give you specific advice that would be relevant.

查看更多
爷的心禁止访问
3楼-- · 2019-08-31 02:18

Google says Actionbar overflow only appears on phones that have no menu hardware keys. Phones with menu keys display the action overflow when the user presses the key. If you still want to implement this you may follow this solution.

Click here for your reference.

Just copy this method in your activity and call the method from onCreate method

 private void getOverflowMenu() {
    try {
       ViewConfiguration config = ViewConfiguration.get(this);
       Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
       if(menuKeyField != null) {
           menuKeyField.setAccessible(true);
           menuKeyField.setBoolean(config, false);
       }
   } catch (Exception e) {
       e.printStackTrace();
   }
 }

查看更多
手持菜刀,她持情操
4楼-- · 2019-08-31 02:29

As @dumazy pointed out that the Action bar's Menu Overflow icon is only shown on those devices which do not have a hardware menu-button.

How do I know when it is needed? meaning phones that don't have the settings button

This is handled by Android itself. You don't need to worry.

how do i create a context menu that is customized and attached to the 3 dot button

You can just have a an xml file inside Menu folder in res. Then you can specify the xml file inside the MenuInflater. Eg:

lets name it list_menu.xml

?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@+id/menu_item_1"
        android:title="@string/menu_string_1" 
        android:showAsAction="ifRoom|withText"/>
    <item android:id="@+id/menu_item_1"
        android:title="@string/menu_string_2" 
        android:showAsAction="ifRoom|withText"/>
</menu>

In the onCreateOptionsMenu you can set it as:

public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater mi = getMenuInflater();
    mi.inflate(R.menu.list_menu, menu);
    return true;
}

This menu would be attached to the overflow-icon and have the items that you want to show when it is clicked. There are some hacks like this which can show the overflow-icon on all devices but using them is highly discouraged. Let android handle this itself.

You seem to use Title bar. Instead, try to use Action Bar for the same.

Hope this answers your question.

查看更多
登录 后发表回答