I have multiple fragments, when switching from fragment to fragment, tool bar will be updated with back arrow. user will now have option to go previous fragment.
animation is working when toggling between hamburger menu and back arrow while switching between fragments back arrow is not animating.
private FragmentManager.OnBackStackChangedListener backStackListener = new FragmentManager.OnBackStackChangedListener() {
#Override
public void onBackStackChanged() {
setNavIcon();
}
;
};
#Override
protected void onCreate(Bundle savedInstanceState) {
Log.d(taglaunch, "on Create Running");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_appdrawer);
navigate();
if (savedInstanceState == null) {
switchtofragment();
}
getFragmentManager().addOnBackStackChangedListener(backStackListener);
}
public void switchtofragment() {
FragmentMenu fragmentMenu = new FragmentMenu();
FragmentManager manger = getFragmentManager();
manger.beginTransaction().replace(R.id.app_bar, fragmentMenu, "fragmenu").commit();
}
public void navigate() {
Log.d(taglaunch, "Navigating");
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
public void setDefault() {
Log.d(taglaunch, "Setting to default");
drawer.openDrawer(GravityCompat.START);
}
protected void setNavIcon() {
int backStackEntryCount = getFragmentManager().getBackStackEntryCount();
toggle.setDrawerIndicatorEnabled(backStackEntryCount == 0);
}
tool bar xml
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarCustom">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.customToolbar">
</android.support.v7.widget.Toolbar>
style :
<style name="AppTheme.customToolbar" parent="ThemeOverlay.AppCompat.Light">
<item name="drawerArrowStyle">#style/DrawerArrowStyle</item>
</style>
Related
This question already has answers here:
NavigationView OnNavigationItemSelectedListener not being called
(3 answers)
Closed 4 years ago.
Nothing happens when i click the items from my navigation drawer. I've tried putting the block of code to replace fragments in the onCreate() method just to test if my FragmentTransaction is working and they work well. I don't know what i am doing wrong. Here's my code for the navigation drawer that i learned from tutorials
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout mDrawerLayout;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionbar = getSupportActionBar();
actionbar.setDisplayHomeAsUpEnabled(true);
actionbar.setHomeAsUpIndicator(R.drawable.ic_menu);
mDrawerLayout = findViewById(R.id.drawer_layout);
mDrawerLayout.addDrawerListener(
new DrawerLayout.DrawerListener() {
#Override
public void onDrawerSlide(#NonNull View drawerView, float slideOffset) {
}
#Override
public void onDrawerOpened(#NonNull View drawerView) {
}
#Override
public void onDrawerClosed(#NonNull View drawerView) {
}
#Override
public void onDrawerStateChanged(int newState) {
}
}
);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, mDrawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
mDrawerLayout.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView)findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
item.setChecked(true);
mDrawerLayout.closeDrawers();
int id = item.getItemId();
FragmentTransaction ft = getFragmentManager().beginTransaction();
switch (id) {
case R.id.nav_budget:
BudgetMain fragBudgetMain = new BudgetMain();
ft.replace(R.id.flContent, fragBudgetMain);
ft.addToBackStack(null);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
break;
case R.id.nav_expenses:
Expenses fragExp = new Expenses();
ft.replace(R.id.flContent, fragExp);
ft.addToBackStack(null);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
break;
case R.id.nav_selfassessment:
SelfAssessment fragSelf = new SelfAssessment();
ft.replace(R.id.flContent, fragSelf);
ft.addToBackStack(null);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
break;
case R.id.nav_mealcard:
MealCard fragMeal = new MealCard();
ft.replace(R.id.flContent, fragMeal);
ft.addToBackStack(null);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
break;
case R.id.nav_ranking:
Ranking fragRanking = new Ranking();
ft.replace(R.id.flContent, fragRanking);
ft.addToBackStack(null);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
break;
}
mDrawerLayout.closeDrawer(GravityCompat.START);
return true;
}
This is my activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Use DrawerLayout as root container for activity -->
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:background="#drawable/gradient2">
<!-- Layout to contain contents of main body of screen (drawer will slide over this) -->
<!-- Container for contents of drawer - use NavigationView to make configuration easier -->
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:menu="#menu/drawer_view"
app:headerLayout="#layout/nav_header"/>
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.DrawerLayout>
This is my app_bar_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
tools:context="com.example.activity.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:background="#drawable/gradient"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="#+id/flContent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?android:attr/actionBarSize"/>
</android.support.design.widget.CoordinatorLayout>
My drawer_view.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_home"
android:icon="#drawable/ic_home"
android:title="Home"/>
<item
android:id="#+id/nav_budget"
android:icon="#drawable/ic_budget"
android:title="Budget"/>
<item
android:id="#+id/nav_expenses"
android:icon="#drawable/ic_expenses"
android:title="Expenses"/>
<item
android:id="#+id/nav_mealcard"
android:icon="#drawable/ic_mealcard"
android:title="Meal Card"/>
<item
android:id="#+id/nav_selfassessment"
android:icon="#drawable/ic_selfassessment"
android:title="Self-Assessment"/>
<item
android:id="#+id/nav_ranking"
android:icon="#drawable/ic_ranking"
android:title="Ranking"/>
</group>
</menu>
Try this one please:
mDrawerList.bringToFront();
mDrawerLayout.requestLayout();
So you onCreate() method will look like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionbar = getSupportActionBar();
actionbar.setDisplayHomeAsUpEnabled(true);
actionbar.setHomeAsUpIndicator(R.drawable.ic_menu);
mDrawerLayout = findViewById(R.id.drawer_layout);
mDrawerLayout.addDrawerListener(
new DrawerLayout.DrawerListener() {
#Override
public void onDrawerSlide(#NonNull View drawerView, float slideOffset) {
}
#Override
public void onDrawerOpened(#NonNull View drawerView) {
}
#Override
public void onDrawerClosed(#NonNull View drawerView) {
}
#Override
public void onDrawerStateChanged(int newState) {
}
}
);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, mDrawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
mDrawerLayout.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView)findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
mDrawerList.bringToFront();
mDrawerLayout.requestLayout();
}
My base activity displays the toolbar but has no title, burger or overflow menus on the bar. I can not figure out how to get the menus to show. Any help would be much appreciated! onCreateOptionsMenu() is not being called in the base activity when I debug.
BaseActivity
public abstract class BaseActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_base);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open,
R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView)
findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
#Override
public void setContentView(int layoutResID) {
final DrawerLayout fullView = (DrawerLayout)
getLayoutInflater().inflate(R.layout.activity_base, null);
FrameLayout activityContainer = (FrameLayout)
fullView.findViewById(R.id.activity_content);
getLayoutInflater().inflate(layoutResID, activityContainer, true);
super.setContentView(fullView);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
}
myActivity
public class myActivity extends BaseActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scan_item);
}
}
Any idea what I am missing to make this work? I have read around and am really struggling to fix this!
My app_bar_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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"
tools:context="com.mpd.sfs.BaseActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
This is my navigation.class which is also my main class.It has inside my navigation drawer and allfragments(fragment)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation33);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
toolbar.setNavigationIcon(R.drawable.ic_flash);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
cooLayout=(ConstraintLayout)findViewById(R.id.cooLayout);
FloatingActionButton myFab = (FloatingActionButton) findViewById(R.id.fabId);
myFab.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(navigation33.this,Post.class));
}
});
auth = FirebaseAuth.getInstance();
// db = FirebaseFirestore.getInstance();
final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
authListener = new FirebaseAuth.AuthStateListener() {
#Override
public void onAuthStateChanged(#NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user == null) {
startActivity(new Intent(navigation33.this, login_user.class));
finish();
}
}
};
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
setTitle("Appx");
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
fragmentManager = getSupportFragmentManager();
myFragmentTransaction = fragmentManager.beginTransaction();
myFragmentTransaction.replace(R.id.cooLayout, new AllFragments()).commit();
}
I have the answers here where just by adding getSupportActionBar().setDisplayHomeAsUpEnabled(true); fixes their problem,however not mine.
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/orange"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include
layout="#layout/content_navigation33" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fabId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
app:backgroundTint="#color/colorPrimaryDark"
app:srcCompat="#drawable/ic_lightbulb" />
Shouldn't this be by default,what am I missing here?
Thanks in advance!
How it is
How I want it to be
Try setting the icon for your toolbar.
toolbar.setNavigationIcon(R.drawable.ic_back_arrow);
toggle.setToolbarNavigationClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onSupportNavigateUp();
}
});
EDIT:
I tested it here and if you add can controle the showing up or hiding of the back arrow by calling this on your toggle object:
toggle.setDrawerIndicatorEnabled(false);
Then to update the toolbar again
public void onBackPressed() {
...
toggle.setDrawerIndicatorEnabled(true);
So if you do this:
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
toggle.setDrawerIndicatorEnabled(false);
Don't forget to add:
#Override
public boolean onSupportNavigateUp() {
onBackPressed();
return true;
}
This works only when:
myFragmentTransaction.addToBackStack(null);
You won't get the hamburger button but the back arrow. So you need to make your toggle a member variable so you can access it globally and control when you want to show or hide the back arrow.
I want to change fragments whenever an item on the Navigation Drawer is clicked.
My app crashes when I use the add() and the replace() method.
The following is my code.
EventActivity.java:
public class EventActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
FragmentTransaction fragmentTransaction;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
fragmentTransaction = getSupportFragmentManager().beginTransaction();
/** App crashes when I add this Line, but there is no error on the console **/
fragmentTransaction.add(R.id.main_container, new AboutFragment());
fragmentTransaction.commit();
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
switch(id) {
case R.id.nav_about:
//.setVisibility(View.GONE);
fragmentTransaction = getSupportFragmentManager().beginTransaction();
/** Error here **/
fragmentTransaction.replace(R.id.main_container, new AboutFragment());
fragmentTransaction.commit();
break;
case R.id.nav_agenda:
break;
case R.id.nav_news_feed:
break;
case R.id.nav_speakers:
break;
case R.id.nav_contact_us:
break;
case R.id.nav_chat:
}
//End of EventActivity.java
app_bar_event.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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"
tools:context="com.tedxdsce.tedxdsce.EventActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/main_container"
>
</FrameLayout>
<include layout="#layout/content_event" />
</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
app:srcCompat="#android:drawable/ic_dialog_email" />
The hierarchy is:
activity_event.xml -> app_bar_event.xml - main_container(FrameLayout) -> content_event.xml
Thank you very much for your time and assistance in this matter.
Edit: The app just crashes without any errors in the log console.
First of all welcome to SO, If you are posting any question then at-least post Error
logs with it. So that other developer can detect error directly.
you should replay to other developer's comment as well!
Which I can't see here
Anyways, set your drawer layout and navigation view like this.
public class MainActivity extends AppCompatActivity implements View.OnClickListener ,NavigationView.OnNavigationItemSelectedListener {
String TAG = "MainActivity";
DrawerLayout drawer;
NavigationView navigationView;
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getData();
init();
setData();
setListner();
}
private void setListner() {
}
private void init() {
navigationView = (NavigationView) findViewById(R.id.nav_view);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
toolbar = (Toolbar) findViewById(R.id.toolbar);
}
private void setData() {
toolbar.setTitle(getResources().getString("title"));
setSupportActionBar(toolbar);
// getSupportActionBar().setTitle("Home");
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
//Setting default as first fragment which is at position 0
navigationView.getMenu().getItem(0).setChecked(true);
onNavigationItemSelected(navigationView.getMenu().getItem(0));
//toggle.setDrawerIndicatorEnabled(false);
navigationView.setNavigationItemSelectedListener(this);
}
#Override
public void onClick(View v) {
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
int id = item.getItemId();
Fragment fragment = null;
switch (id) {
case R.id.about_us:
toolbar.setTitle(getResources().getString("title of your fragment"));
getSupportFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
fragment = new AboutFragment();
break;
// other fragments as per the navigation items...
}
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
I hope your AboutUs fragment is proper.
Try this Example:
Android Sliding Menu using Navigation Drawer
https://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/
Thanks all for your answers.
I have fixed the problem. I had forgotten to implement the onFragmentInteracion interface.
public class EventActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener,
AboutFragment.OnFragmentInteractionListener{
FragmentTransaction fragmentTransaction;
PS: I couldn't figure it earlier because my app kept crashing without any error log.
My code throws a NullPointException on the following three lines
drawer.setDrawerListener(toggle)
navigationView.setNavigationItemSelectedListener(this)
drawer.isDrawerOpen(GravityCompat.START
Here is my full activity
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
NavigationView navigationView = null;
Toolbar toolbar = null;
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
HomeFragment fragment = new HomeFragment();
FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
I followed various answers to try to fix them none of them worked please help.
MainActivity.Xml
<?xml version="1.0" encoding="utf-8"?>
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
Tried adding the onDrawerOpened and onDrawerClosed.
and also the FrameLayout in the app_bar_main also Reports Empty Tag
try putting variables here and add method onDrawerOpened, onDrawerClosed
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
NavigationView navigationView = null;
Toolbar toolbar = null;
DrawerLayout drawer;
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
HomeFragment fragment = new HomeFragment();
FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
navigationView = (NavigationView) findViewById(R.id.nav_view);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close){
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
}
drawer.setDrawerListener(toggle);
toggle.syncState();
navigationView.setNavigationItemSelectedListener(this);
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
#Override
public void onBackPressed() {
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}