Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android How to edit the Action bar menu from Fragment

I created an App that uses Fragment. From my MainActivity I set the ActionBar.

But in one of my Fragment I need to modify the action icons and on click.

So With the code below, when I load the my Fragment, it still display the action bar menu from MainActivity

here is my MainActivity :

public void restoreActionBar() {
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
    actionBar.setDisplayShowTitleEnabled(true);
    actionBar.setTitle(mTitle);

    // enable ActionBar app icon to behave as action to toggle nav drawer
    actionBar.setDisplayHomeAsUpEnabled(true);
    actionBar.setHomeButtonEnabled(true);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    if (!mNavigationDrawerFragment.isDrawerOpen()) {
        // Only show items in the action bar relevant to this screen
        // if the drawer is not showing. Otherwise, let the drawer
        // decide what to show in the action bar.
        getMenuInflater().inflate(R.menu.main, menu);


        //Handle the Search Menu
        SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search));
        searchView.setQueryHint(this.getString(R.string.action_search));

        ((EditText)searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text))
        .setHintTextColor(getResources().getColor(R.color.white));      
        searchView.setOnQueryTextListener(OnQuerySearchView);

        mSearchCheck = false;


        restoreActionBar();
        return true;
    }

    return super.onCreateOptionsMenu(menu);
}//end onCreateOptionsMenu

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle presses on the action bar items
    switch (item.getItemId()) {

        /** CAMERA **/
        case R.id.action_camera:
            //openCamera();
            Utils.makeToast(getApplicationContext(), "Implement Camera", false);
            return true;

        /** SEARCH **/   
        case R.id.action_search:
            //openSearch();
            mSearchCheck = true;
            Utils.makeToast(getApplicationContext(), "Implement Search", false);
            return true;

        /** SETTINGS **/ 
        case R.id.action_settings:
            //openSettings();
            Utils.makeToast(getApplicationContext(), "Implement Settings", false);
            return true;

        /** ABOUT **/ 
        case R.id.action_help:
            //openHelp();
            Utils.makeToast(getApplicationContext(), "Implement Help", false);
            return true;


        default:
            return super.onOptionsItemSelected(item);
    }//end switch
}//end onOptionsItemSelected

private OnQueryTextListener OnQuerySearchView = new OnQueryTextListener() {
    @Override
    public boolean onQueryTextSubmit(String arg0) {
        // TODO Auto-generated method stub
        return false;
    }
    @Override
    public boolean onQueryTextChange(String arg0) {
        // TODO Auto-generated method stub
        if (mSearchCheck){
            // implement your search here
        }
        return false;
    }
};//end OnQueryTextListener

Here is the layout :

<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"
    tools:context="com.example.sellbeesclean.MainActivity" >


    <!-- CAMERA -->
    <item
        android:id="@+id/action_camera"
        android:orderInCategory="100"
        android:icon="@drawable/ic_action_camera"
        android:title="@string/action_camera"
        app:showAsAction="ifRoom|collapseActionView"/>

    <!-- SEARCH -->
    <item
        android:id="@+id/action_search"
        android:orderInCategory="100"
        android:icon="@drawable/ic_action_search"
        android:title="@string/action_search"
        app:actionViewClass="android.support.v7.widget.SearchView"
        app:showAsAction="ifRoom|collapseActionView"/>

    <!-- SETTINGS -->
    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:title="@string/action_settings"
        app:showAsAction="never"/>

    <!-- HELP -->
    <item
        android:id="@+id/action_help"
        android:orderInCategory="100"
        android:icon="@drawable/ic_action_help"
        android:title="@string/action_help"
        app:showAsAction="ifRoom|collapseActionView"/> </menu>

Her is my Fragment :

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        setHasOptionsMenu(true);
        View rootView = inflater.inflate(R.layout.user_profile_fragment, container, false);
        Log.i(TAG, "onCreateView");
.....

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        setHasOptionsMenu(true);
    }

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

    }   



    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle presses on the action bar items
        switch (item.getItemId()) {

            /** EDIT **/
            case R.id.action_edit:
                //openEditProfile(); //Open Edit Profile Fragment
                Utils.makeToast(MyApplication.getAppContext(), "Implement Camera", false);
                return true;



            default:
                return super.onOptionsItemSelected(item);
        }//end switch
    }//end onOptionsItemSelected

here the fragment menu layout

<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"
    tools:context="com.example.sellbeesclean.MainActivity" >


    <!-- EDIT -->
    <item
        android:id="@+id/action_edit"
        android:orderInCategory="100"
        android:icon="@drawable/ic_action_edit"
        android:title="@string/action_edit_profile"
        app:showAsAction="ifRoom|collapseActionView"/></menu>
like image 975
Thiago Avatar asked Dec 24 '14 04:12

Thiago


People also ask

How do I customize my action bar?

This example demonstrate about how to create a custom action bar in Android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How do I access the activity toolbar in fragment?

if you are using custom toolbar or ActionBar and you want to get reference of your toolbar/action bar from Fragments then you need to first get instance of your Main Activity from Fragment's onCreateView Method like below. ImageView vRightBtn = activity. toolbar.

How do you add action items to the action bar in Android?

All action buttons and other items available in the action overflow are defined in an XML menu resource. To add actions to the action bar, create a new XML file in your project's res/menu/ directory. The app:showAsAction attribute specifies whether the action should be shown as a button on the app bar.


Video Answer


1 Answers

In your fragment's onCreateView method write

setHasOptionsMenu(true);

And inflate your menu xml file in onCreateOptionsMenu method

In onCreateOptionsMenu of a fragment, write

menu.clear();

before inflating menus

like image 152
TechHelper Avatar answered Sep 22 '22 08:09

TechHelper