Search code examples
androidandroid-actionbar

Overflow dots not appearing on tablet


For some reason the overflow dots do not appear on my tablet for my master detail application when I select a list item but yet they do on my handset. I'm not sure why is occurring despite using the necessary code. Does anyone there is any code missing or that shouldn't be there?

MainActivity.java

public class MainActivity extends ActionBarActivity {

    private boolean mTwoPane;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        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();
        }

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle(getResources().getString(R.string.greeting));

        FragmentMainList newFragment = new FragmentMainList();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.master_container, newFragment);
        transaction.commit();

        if (findViewById(R.id.detail_container) != null) {
            mTwoPane = true;
        }
    }
}

Activity Class

public class MyProductActivity extends ActionBarActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_fragment);

        if (savedInstanceState == null) {
            FragmentProduct newFragment = new FragmentProduct();
            FragmentTransaction transaction = this.getSupportFragmentManager().beginTransaction();
            transaction.replace(R.id.detail_container, newFragment);
            transaction.commit();
        }

        ActionBar actionBar = getSupportActionBar(); 
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            final Intent intent = NavUtils.getParentActivityIntent(this);
            intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
            NavUtils.navigateUpTo(this, intent);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

Fragment Class

public class FragmentProduct extends android.support.v4.app.Fragment {

    public FragmentProduct() {}

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_product, container, false);

        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);

        return v;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        View v = getView();

        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            final Intent intent = NavUtils.getParentActivityIntent(getActivity());
            intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
            NavUtils.navigateUpTo(getActivity(), intent);
            return true;
        }

        int id = item.getItemId();

        if (id == R.id.action_options) {
        }

        return super.onOptionsItemSelected(item);
    }
}

Solution

  • The dots will be shown only for devices which don't have hardware Menu button. For devices with a hardware Menu button no overflow dots are shown since tapping on the menu button will do the same. For more here

    Edit

    If for some reason you really want to show it, you can add below code in your Application.onCreate() or main (launcher) activity onCreate()

    // force show actionbar 'overflow' button on devices with hardware menu button
    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();
    }