2

Hi guys i want to create 3 dot action bar menu at fragment level, the condition is i want to show that menu at just 1 fragment not on all and if i make them at my main activity, Then i can't hide them so that's why i need to make them at fragment level. so, far i have tried this code on my fragment

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // toggle nav drawer on selecting action bar app icon/title
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    switch (item.getItemId()) {
        case R.id.sync:
            Toast.makeText(this, "Sync data...", Toast.LENGTH_SHORT).show();
            return true;
    }
    return super.onOptionsItemSelected(item);
}

But its saying the method "onCreateOptionsMenu" doesn't override from its super class.

It look like I'm miss something very basic, don't know what it is.

Thanks

Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79
Abdul.Moqueet
  • 902
  • 12
  • 19

4 Answers4

2

Try like this

menu_filter.xml

<menu 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" >


    <item
        android:id="@+id/action_filter"
        android:title="@string/filter"
        android:orderInCategory="10"
        android:icon="@drawable/filter"
        app:showAsAction="ifRoom" />


</menu>

OnCreate Method of fragment

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

onCreateOptionsMenu

@Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.menu_filter.xml, menu);  // Use filter.xml from step 1
    }

onOptionsItemSelected

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if(id == R.id.action_filter){
            //Do whatever you want to do 
            return true;
        }

        return super.onOptionsItemSelected(item);
    } 

I hope it might help you !

Kinjal
  • 1,195
  • 4
  • 13
  • 24
  • Where should i put on onCreateOptionsMenu method on my main activity or on my fragment where i want 3 dots ? – Abdul.Moqueet Nov 09 '16 at 09:24
  • By this code i can show 3 dots menu there's no doubt but since we r creating it at maniactivity so we can hide it of different fragment, by this code it appears on all fragments and that's not what i want – Abdul.Moqueet Nov 09 '16 at 09:28
  • put onCreateOptionsMenu method in your fragment in which you want to display dot menu – Kinjal Nov 09 '16 at 09:32
  • I have already tried putting onCreateOptionMenu method on my fragment, But its saying the method "onCreateOptionsMenu" doesn't override from its super class. – Abdul.Moqueet Nov 09 '16 at 09:35
  • I tried some adjustment on Oncreate method and now it works thanks – Abdul.Moqueet Nov 09 '16 at 09:50
1

Put an ImageButton on fragment's layout with "3 dots" drawable. Then use PopupMenu to show the menu when that ImageButton is clicked. I hope the following answer given by Shylendra helps you: https://stackoverflow.com/a/21329225/7010102

Community
  • 1
  • 1
0

Remove override annotation because it doesn't allow you to do so.Or try to use different xml menu resource to overcome this problem.Say main_menu is for your MainAcitivty and main_frag1 for your Fragment.

Real73
  • 490
  • 4
  • 13
0
    // Todo Three Dots Code.....
    @Override
    public void onPrepareOptionsMenu(Menu menu) {

    }


    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        menu.clear();
        inflater.inflate(R.menu.minu_filter, menu);
        menu.findItem(R.id.action_enter_manually).setVisible(true);
        menu.findItem(R.id.action_validation_report).setVisible(false);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if(id == R.id.action_enter_manually){
            Log.e("keshav","Enter Manually");
            Intent i=new Intent(getActivity(), EnterManually.class);
            startActivity(i);
            //Do whatever you want to do
            return true;
        }
        if(id == R.id.action_validation_report){
            Log.e("keshav","Enter Manually 7128");
            Intent i=new Intent(getActivity(), DateWiseReportActivity.class);
            startActivity(i);

            //Do whatever you want to do
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

----------------------------------------------------------------
                       menu_filter.xml
----------------------------------------------------------------

<menu 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">

    <item
        android:id="@+id/action_enter_manually"
        android:icon="@mipmap/enter_manualy_48"
        android:orderInCategory="10"
        android:title="Enter Manually"
        app:showAsAction="collapseActionView" />

    <item
        android:id="@+id/action_validation_report"
        android:icon="@drawable/done"
        android:orderInCategory="10"
        android:title="Validation Report"
        app:showAsAction="collapseActionView" />


</menu>
Keshav Gera
  • 10,807
  • 1
  • 75
  • 53