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();
}
}
Related
Is there a listener for navigationView if it is actived (Is visible on screen)?
I tried setonsystemuivisibility but not works.
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
I need listener to be called when it's on screen and when it's gone.
thanks.
Add a DrawerListener:
mDrawerLayout = findViewById(R.id.drawer_layout);
mDrawerLayout.addDrawerListener(new DrawerLayout.DrawerListener()
{
// other overridden methods not shown
#Override
public void onDrawerOpened(View drawerView)
{
// drawer opened
// todo: announce for accessibility
}
});
Hope This will help you
DrawerLayout mDrawerLayout;
public static ActionBarDrawerToggle mDrawerToggle;
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(FragmentHomeActivity.this, mDrawerLayout, toolbar, R.string.app_name, R.string.app_name){
public void onDrawerClosed(View view) {
//calling onDrawerClosed, when View gone invisible
}
public void onDrawerOpened(View drawerView) {
// calling onDrawerOpened, when View visible
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
If you are using Toolbar, then
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
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>
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.
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>
When I try to show the navigation drawer when doing swipe action, sometimes it doesn't open (you can see it in the linked screenshot), It appears more frequently when I swipe slowly. It appears in the application sample provided by google too.
MenuActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Burger button
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();
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
// Getting navigation view from activity_menu.xml
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
// Getting Navigation Header resources
View headerLayout = navigationView.getHeaderView(0);
emailTextViewNavHeader = (TextView) headerLayout.findViewById(R.id.textViewEmailNavHeaderMenu);
nameTextViewNavHeader = (TextView) headerLayout.findViewById(R.id.textViewNameNavHeaderMenu);
imageViewProfileNavHeader = (CircularImageView) headerLayout.findViewById(R.id.imageViewProfileNavHeaderMenu);
this.setViewResources();
this.fillInReflexionMenuList();
}
activity_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start"
android:background="#555555">
<include
android:id="#+id/main"
layout="#layout/app_bar_menu"
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_menu"
app:menu="#menu/activity_menu_drawer" />
</android.support.v4.widget.DrawerLayout>
Use the following codes for handling and check:
First, add it on the above.
private DrawerLayout drawerLayout;
And, in OnCreate method:
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
if (menuItem.isChecked()) menuItem.setChecked(false);
else menuItem.setChecked(true);
drawerLayout.closeDrawers();
switch (menuItem.getItemId()) {
case R.id.home:
// do some stuffs
return true;
default:
return true;
}
}
});
Add this too, i think it will fixed with these codes:
drawerLayout = (DrawerLayout) findViewById(R.id.drawer);
ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.openDrawer, R.string.closeDrawer) {
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
}
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
};
drawerLayout.setDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.syncState();