onCreateOptionsMenu() override not being called in BaseActivity - android

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>

Related

How to add icon of NavigationView drawer Android

I am a new application developer. I did an example of (NavigationView drawer) and it works great.But my problem is I need to know how I can add it.
Like this:
I have add it as ImageButton in activity bar_main but I don't need it like that.
As such, I have problems like the following:
I have seen many examples that the icon is not added in this way that is not an ImageButton. It's like come from the system without any ImageButton .So I need do that.
Now if I work like that , it will get overlapped with the search icon.When pressing the search button, the drawer icon should disappear.
my code:
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
static ImageButton menuRight;
private DrawerLayout drawer;
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_m);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
View headerView = navigationView.getHeaderView(0);
navigationView.setNavigationItemSelectedListener(this);
navigationView.bringToFront();
toolbar =findViewById(R.id.toolbar);
toolbar.setTitle("");
setSupportActionBar(toolbar);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
menuRight =(ImageButton) findViewById(R.id.leftRight);
menuRight.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
drawer.openDrawer(GravityCompat.START);
}
}
});
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
drawer.closeDrawers();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
int id = item.getItemId();
if (id == R.id.nav_home) {
Intent i = new Intent(this, MainActivity.class);
startActivity(i);
} else if (id == R.id.nav_Politics) {
Intent i = new Intent(this, Ploysity.class);
startActivity(i);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
return false;
}
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
}
super.onBackPressed();
}
}
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.appbar.AppBarLayout 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="wrap_content"
android:background="#color/colorPrimary">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"></androidx.appcompat.widget.Toolbar>
<ImageButton
android:id="#+id/leftRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/colorPrimary"
android:src="#drawable/ic_menu_black_24dp" />
</RelativeLayout>
</com.google.android.material.appbar.AppBarLayout>
You can implement left navigation something like this:
<androidx.drawerlayout.widget.DrawerLayout ...>
<!--Main content-->
<androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
<!--Left navigation-->
<com.google.android.material.navigation.NavigationView
android:layout_gravity="start"
app:headerLayout="#layout/navigation_header"
app:menu="#menu/navigation_view_menu"
... />
</androidx.drawerlayout.widget.DrawerLayout>
For more information: Material-Design-Navigation-View
if you wish to change the icon, then you need to add this line:
drawer.setHomeAsUpIndicator(R.drawable.ic_your_drawer_icon); //set your own
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(false);
toggle.setDrawerIndicatorEnabled(false);
toggle.setHomeAsUpIndicator(R.drawable.ic_custom_drawer_icon);

My app crashes when i use the add() and replace() methods of FragmentTransaction

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.

Animate Back Arrow for all sub fragments

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>

Connecting toolbar with the navigation drawer

I have several activities where I want to use the same navigation drawer. Here's part of my Activity class code related to the drawer:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
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);
I'm planning to make a DrawerActivity class and extend it from all those activities which want the Navigation drawer. Therefore, I will have to move all the drawer code to that DrawerActivity.
The problem:
toolbar is important for the initialization of ActionBarDrawerToggle. Every activity has a different toolbar depending upon the needs of that activity. Therefore, the toolbar layouts must be kept in their respective layout xml files. How would I use the toolbar then in the DrawerActivity class?
Layout xml for the drawer:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
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:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<FrameLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#+id/frame_layout"
xmlns:android="http://schemas.android.com/apk/res/android">
</FrameLayout>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
app:itemBackground="#drawable/drawer_list_selector"
style="#style/NavigationDrawer"
app:headerLayout="#layout/sidenav_header"
app:menu="#menu/side_navigation_menu"
app:theme="#style/Drawer"/>
</android.support.v4.widget.DrawerLayout>
Here's the SideNavigation activity (which I'm trying to extend by those classes who need navigation drawer): pastebin.com/hheMXku5
main statement of the log says:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayShowTitleEnabled(boolean)' on a null object reference
Here's the link to the onCreate of one of the activity class that extends the SideNavigation class: pastebin.com/iKYXVbda
okey so as you want to use drawer with Activity. I am posting my code here. I hope this will help you out.
First of all you need to develop NavigationDrawer. I know you can do it by your self.
Now create child Activity for Example Home.java and extend it with your drawer Activity let's say MainActivity.java.
it will be something like this.
public class Home extends MainActivity{
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
Now usually we perform setContentView in onCreate, bur here do something like this.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getLayoutInflater().inflate(R.layout.home, frameLayout);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
Where frameLayout would be your container, which you have take in MainActivity.
Note : don't forget to take another ToolBar in home Activity. because it will override MainActivity's toolbar.
EDIT
I am posting my Whole code please refer it. Although I have took my custom Navigation drawer but it can work with NavigationView too.
activity_main
<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" >
<RelativeLayout
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="false" >
<!-- ToolBar of MainActivity -->
<include
android:id="#+id/toolbar_headers"
layout="#layout/toolbar_header" />
</RelativeLayout>
<!-- here will be your NavigationView -->
<com.random.extraclasses.ScrimInsetsFrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:layout_marginRight="-8dp"
android:elevation="8dp"
android:fitsSystemWindows="true"
app:insetForeground="#4000" >
<include
android:id="#+id/drawar_layout"
layout="#layout/drawar_layout" />
</com.gujarat.property.extraclasses.ScrimInsetsFrameLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity implements OnClickListener {
protected RelativeLayout frameLayout;
protected DrawerLayout mDrawerLayout;
protected ListView mDrawerList;
protected ScrimInsetsFrameLayout mLayout;
protected ActionBarDrawerToggle mDrawerToggle;
protected static boolean isLaunch = true;
Toolbar toolbar;
private CharSequence mTitle;
private String[] navMenuTitles;
private TypedArray navMenuIcons;
private ArrayList<NavDrawerItem> navDrawerItems;
private NavDrawerListAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
navMenuIcons = getResources()
.obtainTypedArray(R.array.nav_drawer_icons);
frameLayout = (RelativeLayout) findViewById(R.id.frame_container);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.list);
mLayout = (ScrimInsetsFrameLayout) findViewById(R.id.container);
toolbar = (Toolbar) findViewById(R.id.toolbar_headers);
setSupportActionBar(toolbar);
navDrawerItems = new ArrayList<NavDrawerItem>();
navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons
.getResourceId(0, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons
.getResourceId(1, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons
.getResourceId(2, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons
.getResourceId(3, -1)));
navMenuIcons.recycle();
adapter = new NavDrawerListAdapter(getApplicationContext(),
navDrawerItems);
mDrawerList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
selectedPos = position - 1;
displayView(position);
}
});
mDrawerList.setAdapter(adapter);
mDrawerList.setFocusable(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, R.string.app_name) {
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (isLaunch) {
isLaunch = false;
displayView(0);
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void displayView(int position) {
mDrawerLayout.closeDrawer(mLayout);
MainActivity.position = position;
switch (position) {
case 0:
if (this.getClass().getSimpleName().equals("Home")) {
} else {
Intent i = new Intent(this, Home.class);
startActivity(i);
overridePendingTransition(R.anim.fadein, R.anim.fadeout);
}
break;
case 1:
if (this.getClass().getSimpleName().equals("ForSale")) {
} else {
Intent i = new Intent(this, ForSale.class);
startActivity(i);
overridePendingTransition(R.anim.fadein, R.anim.fadeout);
}
break;
case 2:
if (this.getClass().getSimpleName().equals("ForRent")) {
} else {
Intent i = new Intent(this, ForRent.class);
startActivity(i);
overridePendingTransition(R.anim.fadein, R.anim.fadeout);
}
break;
case 3:
if (this.getClass().getSimpleName().equals("NewProjects")) {
} else {
Intent i = new Intent(this, NewProjects.class);
startActivity(i);
overridePendingTransition(R.anim.fadein, R.anim.fadeout);
}
break;
default:
break;
}
}
#Override
public void setTitle(CharSequence title) {
mTitle = title;
getSupportActionBar().setTitle(mTitle);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
#Override
public void onClick(View v) {
}
}
And finally Home.java
public class Home extends MainActivity implements OnClickListener {
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getLayoutInflater().inflate(R.layout.home, frameLayout);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setTitle("");
}
#Override
public void onBackPressed() {
super.onBackPressed();
if (mDrawerLayout.isDrawerOpen(mLayout)) {
isLaunch = false;
mDrawerLayout.closeDrawer(mLayout);
} else {
finishAffinity();
}
}
And just for reference home.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/toolbar"
android:background="#drawable/back51" >
<TextView
android:id="#+id/etSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:background="#android:color/transparent"
android:text="Search Property"
android:textColor="#666666" />
</RelativeLayout>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|enterAlways"
android:background="#color/toobar"
android:minHeight="?attr/actionBarSize"
android:theme="#style/ThemeOverlay.AppCompat.Dark"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" >
</android.support.v7.widget.Toolbar>
</RelativeLayout>
I hope this will help you out.

Android NavigationView swipe sometimes not openging

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();

Categories

Resources