Android appbar is overlaying when all activities extended form drawer activity - android

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

Related

How to change badge number in bottom navigation of main activity from fragment?

I want to change or set badge number from fragment but i have no idea how to do it
here is my code :-
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar;
toolbar = (Toolbar) findViewById(R.id.my_nav_bar);
toolbar.setTitleTextColor(getResources().getColor(R.color.text_white));
setSupportActionBar(toolbar);
navigationView = findViewById(R.id.mynavigation);
drawerLayout = findViewById(R.id.mydrawer);
actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open, R.string.close);
drawerLayout.addDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.syncState();
bottomNavigationView = findViewById(R.id.my_bottom_menu);
badgeDrawable = bottomNavigationView.getOrCreateBadge(R.id.cart);
badgeDrawable.setNumber(0);
bottomNavigationView.setSelectedItemId(R.id.home);
}
Call this method from fragment
MyActivity myAct= (MyActivity) getActivity();
myAct.increaseCount(4);
In MyActivity.java add this method
public void increaseCount(int count){
badgeDrawable.setNumber(count);
}

Android navigationView when is visible on screen listener

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

Is it possible to change extended drawer's style and use it in activity?

Is it possible to change extended drawer's style and use it in activity? If yes how? I'm trying to use this drawer making it transparent in StoreActivity
If you are curious here is my code. HomeNavigation.java, Drawer code
public class HomeNavigation extends AppCompatActivity {
public DrawerLayout mDrawerLayout;
public ActionBarDrawerToggle mToggle;
public Toolbar mToolbar;
public NavigationView mNavigationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drawer_navigation);
mToolbar = (Toolbar) findViewById(R.id.navigation_actionbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setTitle("Welcome Back!");
mNavigationView = (NavigationView) findViewById(R.id.navigation_view);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close);
mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
/**ON NAVIGATION ITEM SELECTED*/
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// enabling drawer toggle by clicking on the app icon.
if (mToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Class I'm trying to use it in StoreActivity.java
public class StoreActivity extends HomeNavigation {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater inflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View contentView = inflater.inflate(R.layout.activity_drawer_navigation_transparent, null, false);
mDrawerLayout.addView(contentView, 0);
getSupportActionBar().setTitle("");
mNavigationView.getMenu().getItem(1).setChecked(true);
}
}

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

Categories

Resources