Add action menu in a snippet, Android Studio

1

Good! I'm trying to add an action bar to a fragment, but I can not get it in the xml or java. This would be my action menu

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

<item
    android:id="@+id/action_facial_scan"
    android:icon="@drawable/ic_facial_action"
    android:title="@string/facial_scan"
    app:showAsAction="ifRoom" />

This is the xml of my fragment

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/lightBackground"
tools:context=".fragments.EmptyFragment">

<ImageView
    android:layout_width="wrap_content"
    android:layout_marginLeft="50dp"
    android:layout_marginRight="50dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:src="@drawable/ic_enterprise_logo"/>

and this would be the code in java

    package com.idtknology.identytech.identymanage.fragments;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;

    public class EmptyFragment extends Fragment {
public EmptyFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_empty, container, false);
}

}

I would appreciate any help !!

    
asked by Mar Aq 08.10.2017 в 15:42
source

1 answer

0

First you must overwrite the corresponding methods that one should always use when using menus: these methods are:

OnCreateOptionsMenu()

and

OnOptionsItemSelected()

You must implement the following to the class of your fragment:

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if(id == R.id.action_facial_scan){

            //El código que se ejecutara al hacer click en esa opción

        }

        return super.onOptionsItemSelected(item);
    }

To make this all work you must implement a third thing in your fragment. In the onCreate method of your fragment (if you do not have an onCreate method, add it) you must include setHasOptionsMenu (true);

So that your onCreate method would look like this:

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

Of course you must implement this fragment in your class, you have two forms dynamically:

Suppose that the class of your fragment is called MiMenu, then you should instantiate it in the class where you want to add the fragment:

MiMenu miMenuFragmento/*por ejemplo */= new  MiMenu();


FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().add(R.id.layout_contenedor_del_fragmento, miMenuFragmento/*el nombre que le dimos a la instancia*/).commit();

or via xml adding this to the xml where you want the fragment to appear:

<fragment
    android:layout:width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/id_de_tu_fragmento" //id de tu fragmento
    android:name="..." //nombre completo de tu fragmento
    tools:layout="@layout/fragment_your_fragment_name"> //nombre de tu fragmento

Finally remember that the fragments included through xml can not be replaced by others, to replace them you must include them dynamically

    
answered by 08.10.2017 / 16:39
source