No Back Option showed - android

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.

Related

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.

Android appbar is overlaying when all activities extended form drawer activity

I have a root class
class DrawerLayout extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
protected void onCreateDrawer(Toolbar toolbar) {
// drawer.
android.support.v4.widget.DrawerLayout drawer = (android.support.v4.widget.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 = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {...}
}
And when I create Main Activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// here.
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
super.onCreateDrawer(toolbar);
}
When I create activity with problem:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_about);
super.onCreateDrawer(toolbar);
}
How it looks:
All activities are like activity_main -> app_bar_main -> content_main but with their own layouts, they do not share.
Add setSupportActionBar(toolbar); after Toolbar initialization
like this,
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

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>

setDrawerListener and related activities nullPointException

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

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