Abdul.Moqueet
Abdul.Moqueet

Reputation: 1077

Implement 3 dot menu to a fragment

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

Upvotes: 2

Views: 2399

Answers (4)

Keshav Gera
Keshav Gera

Reputation: 11264

    // 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>

Upvotes: 0

Real73
Real73

Reputation: 500

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.

Upvotes: 0

user7010102
user7010102

Reputation:

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

Upvotes: 1

Kinjal
Kinjal

Reputation: 1193

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 !

Upvotes: 2

Related Questions