0

I am new to android, I have been learning since 6 months. Currently I am working on a project where I want to add different toolbar menu for different bottom navigation bar items and I am using fragments for items.

My mainactivity layout code is :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbarHome"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">


    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbarHome"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize" />

</android.support.design.widget.AppBarLayout>


<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="?android:attr/windowBackground"
        app:menu="@menu/menu_bottom_navigation" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/btnfab_Add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center_horizontal"
        android:layout_marginBottom="@dimen/size_20dp"
        android:clickable="true"
        android:focusable="true"
        app:elevation="8dp"
        app:fabSize="normal"
        app:srcCompat="@android:drawable/ic_input_add" />

</FrameLayout>

and My mainactivity java code is this :

    public class Home extends AppCompatActivity {

    private  SharedPreferences sharedPreferences;

    //xml references
    BottomNavigationView navigation;


    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction transaction = fragmentManager.beginTransaction();

            switch (item.getItemId()) {
                case R.id.action_home:
                    transaction.replace(R.id.content, new HomeFragment()).commit();
                    return true;
                case R.id.action_notification:
                    transaction.replace(R.id.content, new ActivityLogFragment()).commit();
                    return true;
            }

            return false;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        sharedPreferences = getSharedPreferences(HomePrev.AccountType, MODE_PRIVATE);

        // create class object
        GPSTracker gps = new GPSTracker(this);

        // check if GPS enabled
        if (gps.canGetLocation()) {

            String latitude = String.valueOf(gps.getLatitude());
            String longitude = String.valueOf(gps.getLongitude());

            SharedPreferences.Editor editor = sharedPreferences.edit();

            editor.putString("Lattitude", latitude);
            editor.putString("Longitude", longitude);
            editor.apply();

        } else {
            // can't get location
            // GPS or Network is not enabled
            // Ask user to enable GPS/network in settings
            gps.showSettingsAlert();
        }

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbarHome);
        setSupportActionBar(toolbar);
       navigation = findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

        //Manually displaying the first fragment - one time only
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.content, new HomeFragment());
        transaction.commit();

        FloatingActionButton btnFabAdd = findViewById(R.id.btnfab_Add);
    }

    @Override
    public void onBackPressed() {
        new AlertDialog.Builder(this)
                .setTitle("Really Exit?")
                .setMessage("Are you sure you want to exit?")
                .setNegativeButton(android.R.string.no, null)
                .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface arg0, int arg1) {
                        Home.super.onBackPressed();
                    }
                }).create().show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.


        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.home, menu);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the HomePrev/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        } else if (id == R.id.action_notification) {

            startActivity(new Intent(Home.this,Notifications.class));
            return true;

        }
        else if (id == R.id.action_search) {
            return true;
        } else if (id == R.id.action_profile_info) {

            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

So my question is that how can I add different menus according to the the bottom navigation bar item selected by the user.

Kindly revert soon and If you are unable to understand my question then also revert me but don't put this question on hold.

Umesh P
  • 228
  • 2
  • 7
JGD
  • 21
  • 1
  • 7

0 Answers0