1

here is my entire code,
I've used bottom_navigation and drawer_navigation in my app but my intents in the drawer navigation are not working. I used the same code of drawer _navigation in some other app without buttom_navigation and its working fine, but I don't know where am mistaking in this one...

package com.example.myapplication;

import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

private DrawerLayout drawerLayout;
private ActionBarDrawerToggle drawerToggle;
private NavigationView navigation;


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

    Toolbar toolbar1 = findViewById(R.id.toolbar1);
    setSupportActionBar(toolbar1);

    drawerLayout = findViewById(R.id.drawer_layout);
    drawerToggle = new ActionBarDrawerToggle(MainActivity.this, drawerLayout, toolbar1, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawerLayout.setDrawerListener(drawerToggle);
    drawerToggle.syncState();

    navigation = findViewById(R.id.nav_view);
    navigation.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {
            int id = menuItem.getItemId();
            switch (id) {
                case R.id.nav_cd:
                    Intent intent = new Intent(MainActivity.this, contact_developer1.class);
                    MainActivity.this.startActivity(intent);
                    break;
                case R.id.nav_atapp:
                    Intent intent1 = new Intent(MainActivity.this, about_the_app.class);
                    startActivity(intent1);
                    break;
                case R.id.nav_atchurch:
                    Intent intent2 = new Intent(MainActivity.this, about_the_church.class);
                    startActivity(intent2);
                    break;
                case R.id.nav_ods:
                    Intent intent3 = new Intent(MainActivity.this, our_doctrinal_statement.class);
                    startActivity(intent3);
                    break;
                case R.id.nav_omp:
                    Intent intent4 = new Intent(MainActivity.this, our_musical_philosophy.class);
                    startActivity(intent4);
                    break;
                case R.id.nav_share:
                    Intent sendIntent = new Intent();
                    sendIntent.setAction(Intent.ACTION_SEND);
                    sendIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    sendIntent.setType("text/plain");
                    sendIntent.putExtra(Intent.EXTRA_TEXT, "Hey, download this app!");
                    startActivity(sendIntent);
                    break;
            }
            return true;
        }
    });


    BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
    bottomNav.setOnNavigationItemSelectedListener(navListener);

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                new HomeFragment()).commit();
    }

}


@Override
public void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    drawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    drawerToggle.onConfigurationChanged(newConfig);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (drawerToggle.onOptionsItemSelected(item))
        return true;

    int id = item.getItemId();
    if (id == R.string.app_name) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onBackPressed() {
    if(drawerLayout.isDrawerOpen(GravityCompat.START)){
        drawerLayout.closeDrawer(GravityCompat.START);
    }else {
        super.onBackPressed();
    }
}

private BottomNavigationView.OnNavigationItemSelectedListener navListener =
        new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected( MenuItem item) {
                Fragment selectedFragment = null;

                switch (item.getItemId()) {
                    case R.id.nav_home:
                        selectedFragment = new HomeFragment();
                        break;
                    case R.id.nav_favorites:
                        selectedFragment = new FavoritesFragment();
                        break;
                    case R.id.nav_category:
                        selectedFragment = new CategoryFragment();
                        break;
                }
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        selectedFragment).commit();
                return true;
            }
        };
}

Xml.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity"
    tools:openDrawer="start">

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/drawer_menu" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar1"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:titleTextAppearance="@style/TextAppearance.AppCompat.Large" />
    </LinearLayout>


    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="60dp"
        android:layout_above="@id/bottom_navigation" />
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="?android:attr/windowBackground"
        app:menu="@menu/bottom_navigation" />

        </RelativeLayout>
</android.support.v4.widget.DrawerLayout>

  • What exactly do you mean by "not working"? What happens when you click on a drawer item? – Mike M. Sep 04 '19 at 07:53
  • firstly class name always start with capital letters. – Shivam Oberoi Sep 04 '19 at 07:53
  • @Mike M. NOTHING happens that's what I mean not working – john mantraj Sep 04 '19 at 08:16
  • Nothing? Does the drawer close? – Mike M. Sep 04 '19 at 08:17
  • @Shivam Oberoi where? – john mantraj Sep 04 '19 at 08:17
  • The drawer in a `` must be listed last in order for it to receive touch events properly. Move the `` to after the everything else inside the ``. If you did not modify that file to be like that yourself, then it's possibly due to an issue in the 3.5 upgrade for Android Studio that causes XML to be rearranged incorrectly. If that's your case, have a look at [this post](https://stackoverflow.com/q/57591080) to see how to fix that. Also, you should really have everything else inside the `` in one `ViewGroup`; the main content `ViewGroup`. – Mike M. Sep 04 '19 at 08:39
  • No problem. Btw, all of the other direct children in your `` – i.e., the ``, the ``, and the `` – should really all be in one `ViewGroup` – the main content `ViewGroup` – so you don't get similar touch event issues with those `View`s. As you have it now, those are all filling the ``, and overlapping each other. Anyhoo, glad you got it working. Cheers! – Mike M. Sep 04 '19 at 08:45

1 Answers1

0

Check whether you define activities in AndroidManifest file, And you need to define the meaning of 'not working'.

yarn
  • 1
  • 2