Force the ActionBar to display the OverFlow button (three points)

Source: Internet
Author: User

Force the ActionBar to display the OverFlow button (three points)

About the three buttons at the rightmost of the ActionBar:


The official name should be overflow menu. devices above android 4.2.2 are displayed by default, but devices of android 4.1.2 are not displayed. It seems that android does not provide APIs for display, therefore, the mandatory display solution is found online (available for test ).

The key code is as follows:

private void getOverflowMenu() {ViewConfiguration viewConfig = ViewConfiguration.get(this);try {Field overflowMenuField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");if(null != overflowMenuField){overflowMenuField.setAccessible(true);overflowMenuField.setBoolean(viewConfig, false);}} catch (NoSuchFieldException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalArgumentException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalAccessException e) {// TODO Auto-generated catch blocke.printStackTrace();}}
You can directly call the onCreate () method:

protected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.activity_home_page);...//force to display overflow menu in action bargetOverflowMenu();}

The original author added that it would not be a problem to devices that are immediately applied to android4.4 or above: even if the overflowMenuField is null, the Code is also empty, so there is no impact.

I actually add the following version compatibility conditions when calling:

//force to display overflow menu in action bar when sdk version less than 21 if((Build.VERSION.SDK_INT >= 10) & (Build.VERSION.SDK_INT < 21)) {getOverflowMenu();}
No problems found.

Finally, I would like to thank you for your selfless sharing.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.