How to apply drawerlayout on specific fragments? - java

I have the following problem:
I want to use navigation components and therefore I have only one activity (MainActivity) and several fragments.
I have two types of fragments: authenticating stuff and the actual app.
The actual app contains a drawer layout and the auth stuff should only be in contraint layouts.
But the problem is that the auth fragments also are showing the statusbar of the drawer layout.
I tried to hide the actionbar on these fragments calling ((AppCompatActivity)getActivity()).getSupportActionBar().hide();
But this did not work and I got a black space at the top of the screen.
Is there any way to apply the drawerlayout to specific fragments or a general better approach to get this behavior?
MainActivity.java
public class MainActivity extends AppCompatActivity
{
private NavController navController;
private DrawerLayout drawerLayout;
private AppBarConfiguration appBarConfiguration;
private NavController.OnDestinationChangedListener listener;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
navController = Navigation.findNavController(this, R.id.fragment);
drawerLayout = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.navigationView);
NavigationUI.setupWithNavController(navigationView, navController);
appBarConfiguration = new AppBarConfiguration.Builder(R.id.firstFragment, R.id.secondFragment)
.setOpenableLayout(drawerLayout)
.build();
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
navController.addOnDestinationChangedListener((_c, destination, _b) ->
{
if (destination.getId() == R.id.mainFragment ||
destination.getId() == R.id.loginFragment ||
destination.getId() == R.id.registerUserFragment)
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
else
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".MainActivity">
<fragment
android:id="#+id/fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="1dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="1dp"
android:layout_marginBottom="1dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="#navigation/my_nav" />
<com.google.android.material.navigation.NavigationView
android:id="#+id/navigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="#menu/drawer_menu" />
</androidx.drawerlayout.widget.DrawerLayout>
my_nav.xml
<?xml version="1.0" encoding="utf-8"?>
<navigation 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/my_nav"
app:startDestination="#id/mainFragment">
<fragment
android:id="#+id/firstFragment"
android:name="com.mioai.gamehub.FirstFragment"
android:label="fragment_first"
tools:layout="#layout/fragment_first">
<action
android:id="#+id/navigateToSecondFragment"
app:destination="#id/secondFragment"
app:enterAnim="#android:anim/fade_in"
app:exitAnim="#android:anim/fade_out" />
</fragment>
<fragment
android:id="#+id/secondFragment"
android:name="com.mioai.gamehub.SecondFragment"
android:label="fragment_second"
tools:layout="#layout/fragment_second" />
<fragment
android:id="#+id/mainFragment"
android:name="com.mioai.gamehub.MainFragment"
tools:layout="#layout/fragment_main">
<action
android:id="#+id/action_mainFragment_to_firstFragment"
app:destination="#id/firstFragment" />
<action
android:id="#+id/action_mainFragment_to_loginFragment"
app:destination="#id/loginFragment" />
</fragment>
<fragment
android:id="#+id/loginFragment"
android:name="com.mioai.gamehub.LoginFragment"
android:label="fragment_login"
tools:layout="#layout/fragment_login" >
<action
android:id="#+id/action_loginFragment_to_registerUserFragment2"
app:destination="#id/registerUserFragment" />
</fragment>
<fragment
android:id="#+id/registerUserFragment"
android:name="com.mioai.gamehub.RegisterUserFragment"
android:label="fragment_register_user"
tools:layout="#layout/fragment_register_user" />
</navigation>

Related

bottomnavigationview with navigation Drawer

I want to implement a bottomnavigationview with a navigation drawer as an interior element
I managed to create a bottomnavigationview and a navigation drawer but I don't see how to put the navigation drawer in the bottomnavigationview
My MainActivity java:
public class MainActivity extends AppCompatActivity {
private static final String TAG = "debinf MainActivity";
//public static final String FRAGMENT_KEY = "fragment";
private BottomNavigationView bottomNavigationView;
private NavigationView navigationView;
private DrawerLayout drawerLayout;
private NavController navController;
private AppBarConfiguration appBarConfiguration;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(TAG, "onCreate: ");
bottomNavigationView = (BottomNavigationView) findViewById(R.id.main_bottom_nav);
navigationView = (NavigationView) findViewById(R.id.main_sidebar);
drawerLayout = (DrawerLayout) findViewById(R.id.main_drawer);
setupNavigation();
}
private void setupNavigation() {
Log.i(TAG, "setupNavigation: ");
navController = Navigation.findNavController(this, R.id.main_fragment);
appBarConfiguration =
new AppBarConfiguration.Builder(R.id.navigation_friends,R.id.navigation_order,R.id.navigation_home,R.id.navigation_groups,R.id.navigation_settings,R.id.navigation_profile,R.id.navigation_all_activity) //Pass the ids of fragments from nav_graph which you dont want to show back button in toolbar
.setDrawerLayout(drawerLayout)
.build();
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration); //Setup toolbar with back button and drawer icon according to appBarConfiguration
NavigationUI.setupWithNavController(navigationView, navController);
NavigationUI.setupWithNavController(bottomNavigationView, navController);
/*
** Listener for bottomNavigation must be called after been setupWithNavController
** This command will override NavigationUI.setupWithNavController(bottomNavigationView, navController)
** and the automatic transaction between fragments is lost
* */
//bottomNavigationView.setOnNavigationItemSelectedListener(this);
//navigationView.setNavigationItemSelectedListener(this);
}
#Override
public void onBackPressed() {
Log.i(TAG, "onBackPressed: ");
if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
Log.i(TAG, "onBackPressed: DRAWER IS OPEN - CLOSING IT");
drawerLayout.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onSupportNavigateUp() {
Log.i(TAG, "onSupportNavigateUp: ");
// replace navigation up button with nav drawer button when on start destination
return NavigationUI.navigateUp(navController, appBarConfiguration) || super.onSupportNavigateUp();
}
My activity_main.xml :
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
android:id="#+id/main_drawer"
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=".MainActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="#+id/main_fragment"
android:layout_width="0dp"
android:layout_height="0dp"
android:name="androidx.navigation.fragment.NavHostFragment"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="#id/main_bottom_nav"
app:defaultNavHost="true"
app:navGraph="#navigation/mainnav_graph"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/main_bottom_nav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="#menu/bottom_nav_menu">
</com.google.android.material.bottomnavigation.BottomNavigationView>
</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.navigation.NavigationView
android:id="#+id/main_sidebar"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="#menu/drawer_menu"/>
</androidx.drawerlayout.widget.DrawerLayout>
My bottom_nav_menu.xml (fragment in bottomnavigationview):
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/navigation_home"
android:icon="#drawable/ic_home_black_24dp"
android:title="#string/title_home" />
<item
android:id="#+id/navigation_friends"
android:icon="#drawable/ic_home_black_24dp"
android:title="#string/title_friends" />
<item
android:id="#+id/navigation_order"
android:icon="#drawable/ic_dashboard_black_24dp"
android:title="#string/title_order" />
</menu>
My drawer_menu.xml (fragments in drawer):
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/navigation_friends"
android:icon="#drawable/ic_home_black_24dp"
android:title="#string/title_friends" />
<item
android:id="#+id/navigation_order"
android:icon="#drawable/ic_dashboard_black_24dp"
android:title="#string/title_order" />
<item
android:id="#+id/navigation_groups"
android:icon="#drawable/ic_dashboard_black_24dp"
android:title="#string/title_groups" />
<item
android:id="#+id/navigation_profile"
android:icon="#drawable/ic_dashboard_black_24dp"
android:title="#string/title_profile" />
<item
android:id="#+id/navigation_settings"
android:icon="#drawable/ic_dashboard_black_24dp"
android:title="#string/title_settings" />
</menu>
My mainnav_graph.xml (all fragments):
<?xml version="1.0" encoding="utf-8"?>
<navigation 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/mobile_navigation"
app:startDestination="#+id/navigation_home">
<fragment
android:id="#+id/navigation_home"
android:name="fr.spindyo.bottombarwithdrawer.ui.home.HomeFragment"
android:label="#string/title_home"
tools:layout="#layout/fragment_home" />
<fragment
android:id="#+id/navigation_friends"
android:name="fr.spindyo.bottombarwithdrawer.ui.friends.FriendsFragment"
android:label="#string/title_friends"
tools:layout="#layout/fragment_friends" />
<fragment
android:id="#+id/navigation_order"
android:name="fr.spindyo.bottombarwithdrawer.ui.order.OrderFragment"
android:label="#string/title_order"
tools:layout="#layout/fragment_order" />
<fragment
android:id="#+id/navigation_groups"
android:name="fr.spindyo.bottombarwithdrawer.ui.groups.GroupsFragment"
android:label="#string/title_groups"
tools:layout="#layout/fragment_groups" />
<fragment
android:id="#+id/navigation_profile"
android:name="fr.spindyo.bottombarwithdrawer.ui.profile.ProfileFragment"
android:label="#string/title_profile"
tools:layout="#layout/fragment_profile" />
<fragment
android:id="#+id/navigation_settings"
android:name="fr.spindyo.bottombarwithdrawer.ui.settings.SettingsFragment"
android:label="#string/title_settings"
tools:layout="#layout/fragment_settings" />
<fragment
android:id="#+id/navigation_all_activity"
android:name="fr.spindyo.bottombarwithdrawer.ui.allFragment.AllFragment"
android:label="#string/title_all_activity"
tools:layout="#layout/fragment_all_fragment" />
</navigation>

Custom TabLayout Indicator - Android

I have been trying to make a custom TabLayout indicator like Google Drive's one but didn't figure it out.
I tried the following article:
https://medium.com/#adrianespi94/apply-new-google-style-to-your-android-material-tabs-4d498c993c51
It works fine, but not as good as the one in the GIF below.
Thanks in advance.
There's two thing like this.
works by clicking on tab.
works by horizontal scrolling and clicking on tab also.
I don't know what you want actually. cause, the link you gave that is no 1. which is TabHost. I am adding source code for both.
ViewPagerAdapter :
public class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> fragmentList=new ArrayList<>();
private final List<String> fragmentListTitles=new ArrayList<>();
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
#Override
public int getCount() {
return fragmentListTitles.size();
}
//return page title
#Override
public CharSequence getPageTitle(int position) {
return fragmentListTitles.get(position);
}
public void AddFragment(Fragment fragment,String title)
{
fragmentList.add(fragment);
fragmentListTitles.add(title);
}
}
MainActivity :
private TabLayout tabLayout;
private AppBarLayout appBarLayout;
private ViewPager viewPager;
tabLayout=(TabLayout)findViewById(R.id.tablayout);
viewPager=(ViewPager)findViewById(R.id.viewpager);
//create viewpageradaper class object
ViewPagerAdapter adapter=new ViewPagerAdapter(getSupportFragmentManager());
//adding fragments using adapter object
adapter.AddFragment(new FreeFireFragment(), "Free Fire");
adapter.AddFragment(new PubgFragment(), "Pubg");
adapter.AddFragment(new CallOfDutyFragment(), "Call of Duty");
//set adapter into viewpager
viewPager.setAdapter(adapter);
//set viewpager into tablayout
tabLayout.setupWithViewPager(viewPager);
If you want to add icon :
//for set icon into tab items
final int[] ICONS = new int[]{
R.drawable.ff,
R.drawable.pubg,
R.drawable.cod
};
//enter the following source code below the MainActivity source code
//set icon to tab items
tabLayout.getTabAt(0).setIcon(ICONS[0]);
tabLayout.getTabAt(1).setIcon(ICONS[1]);
tabLayout.getTabAt(2).setIcon(ICONS[2]);
activity_home.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical">
<com.google.android.material.tabs.TabLayout
android:id="#+id/tablayout"
android:layout_width="match_parent"
android:layout_height="55dp"
app:tabBackground="#color/homeColor"
app:tabGravity="fill"
app:tabIndicatorColor="#color/tabIndicator"
app:tabMode="fixed"
app:tabSelectedTextColor="#color/tabIndicator"
app:tabTextColor="#color/tabTextColor">
</com.google.android.material.tabs.TabLayout>
<androidx.viewpager.widget.ViewPager
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/viewpager">
</androidx.viewpager.widget.ViewPager>
</LinearLayout>
Above source code works as no 2.
Now, no 1:
Layout :
<TabHost
android:id="#+id/tab_host"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#id/rl_">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/Filters"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
layout="#layout/filters_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="#+id/Adjustments"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include
layout="#layout/adjustment_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</FrameLayout>
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#40cccccc" />
</LinearLayout>
</TabHost>
MainActivity
//setup tabHost
TabHost tabHost = findViewById(R.id.tab_host);
tabHost.setup();
//set item 1 in tabhost
TabHost.TabSpec Filters= tabHost.newTabSpec("Filters");
Filters.setContent(R.id.Filters);
Filters.setIndicator("Filters");
tabHost.addTab(Filters);
//set item 2 in tabhost
TabHost.TabSpec Adjustments= tabHost.newTabSpec("Adjustments");
Adjustments.setContent(R.id.Adjustments);
Adjustments.setIndicator("Adjustments");
tabHost.addTab(Adjustments);
I made the solution by myself, it seems like the solution can be done easily without libraries or any spaghetti code.
First, you need to an XML file for tab indicator:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:bottomLeftRadius="0dp"
android:bottomRightRadius="0dp"
android:topLeftRadius="8dp"
android:topRightRadius="8dp" />
<size android:height="3dp" />
<solid android:color="#color/selectedColor" />
</shape>
Then make a Selector to change the indicator color based on your selection:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/selectedColor" android:state_selected="true" />
<item android:color="#color/notSelectedColor" />
</selector>
Finally in your TabLayout, add these lines:
<com.google.android.material.tabs.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabIconTint="#drawable/tab_selector_color" //for selection
app:tabIndicator="#drawable/tab_indicator" //for rounded corners
app:tabIndicatorAnimationMode="elastic" //for that elastic swiping effect
app:tabIndicatorColor="#color/selectedColor"
app:tabIndicatorFullWidth="false"
app:tabInlineLabel="true"
app:tabSelectedTextColor="#color/selectedColor"
app:tabTextColor="#color/notSelectedColor"/>

How to remove the shadow under Action bar

I have been trying to get the shadow under the app Action bar to disappear but it wont which is very annoying. iI have set the theme to NoActionBar and opted to using toolbar instead.
And have i have also set the elevation to 0dp in my activity.java
package com.example.drawerlayout;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;
import android.os.Bundle;
import android.view.MenuItem;
import androidx.appcompat.widget.Toolbar;
import com.google.android.material.appbar.AppBarLayout;
public class MainActivity extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle;
private Toolbar mToolbar;
private AppBarLayout actionLayout;
private Toolbar t;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close);
mToolbar = (Toolbar) findViewById(R.id.nav_action);
setSupportActionBar(mToolbar);
mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setElevation(0);
}
}
The Toolbar looks thus:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="#+id/nav_action"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/mm_blue" />
</com.google.android.material.appbar.AppBarLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Simply add app:elevation="0dp"
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:elevation="0dp">
<!-- Toolbar -->
</com.google.android.material.appbar.AppBarLayout>
Set this property toolbar:elevation="0dp" to AppBarLayout to remove shadow from Toolbar.
eg-
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appbar"
toolbar:elevation="0dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="#+id/nav_action"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/mm_blue" />
</com.google.android.material.appbar.AppBarLayout>
Check below code
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="0dp"
app:elevation="0dp">
<androidx.appcompat.widget.Toolbar
android:id="#+id/nav_action"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/mm_blue" />
</com.google.android.material.appbar.AppBarLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Just remove the AppBar layout and directly put your toolbar in the coordinate layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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">
<androidx.appcompat.widget.Toolbar
android:id="#+id/nav_action"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/mm_blue" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Try this may it should help you out.

Android Object coming back null after onCreate

Android newbie here.
I'm trying to set an onClick event on a LinearLayout. But I keep getting the error
Attempt to invoke virtual method 'void android.widget.LinearLayout.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
Which, if I understand correctly means that the reference is null. Which doesn't make sense to me since I'm calling it after onCreate
Here's my code, anybody have any clues what I'm doing wrong?
MainActivity - The place with HERE! is where the crash is occuring
package ...
import ...
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar 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.setDrawerListener(toggle);
toggle.syncState();
final NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
setNavHeaderOnClickAction();
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// ...
}
public void setNavHeaderOnClickAction(){
// HERE! Here's the problemsome area
LinearLayout navHeaderUser = (LinearLayout) findViewById(R.id.nav_header_user);
navHeaderUser.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Intent myIntent = new Intent(MainActivity.this, UserProfile.class);
// MainActivity.this.startActivity(myIntent);
// DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
// drawer.closeDrawer(GravityCompat.START);
}
});
}
}
activity_main.xml
<?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">
<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" />
</android.support.v4.widget.DrawerLayout>
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.trevorwood.biggles.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="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include android:id="#+id/main_content" layout="#layout/content_blank"/>
<android.support.design.widget.AppBarLayout
android:id="#+id/search_button"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_gravity="top|end"
android:layout_margin="10dp"
android:background="#android:drawable/ic_menu_search">
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
nav_header_main.xml (layout) included in activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="120dp"
android:background="#drawable/side_nav_bar"
android:gravity="center"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:theme="#style/ThemeOverlay.AppCompat.Dark">
<LinearLayout
android:id="#+id/nav_header_user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:clickable="true">
<ImageView
android:id="#+id/imageView"
android:layout_width="60dp"
android:layout_height="60dp"
android:paddingTop="#dimen/nav_header_vertical_spacing"
app:srcCompat="#android:drawable/sym_def_app_icon"
android:clickable="false"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:clickable="false">
<TextView
android:id="#+id/textView"
android:layout_width="303dp"
android:layout_height="wrap_content"
android:paddingTop="#dimen/nav_header_vertical_spacing"
android:text="JohnSmith007"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"
android:clickable="false"/>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1,500 points"
android:clickable="false"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
activity_main_drawer.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group
android:id="#+id/grp1"
android:checkableBehavior="single">
<item
android:id="#+id/nav_home"
android:icon="#drawable/ic_menu_home"
android:title="Home" />
<item
android:id="#+id/nav_library"
android:icon="#drawable/ic_menu_book"
android:title="Library" />
<item
android:id="#+id/nav_create"
android:icon="#drawable/ic_menu_add"
android:title="Create" />
<item
android:id="#+id/nav_user"
android:icon="#drawable/ic_menu_user"
android:title="My Account" />
<item
android:id="#+id/nav_settings"
android:icon="#drawable/ic_menu_cog_wheel"
android:title="Settings" />
</group>
<group
android:id="#+id/grp2"
android:checkableBehavior="single">
<item
android:id="#+id/nav_about"
android:icon="#drawable/ic_menu_about"
android:title="About" />
<item
android:id="#+id/nav_sign_out"
android:icon="#drawable/ic_menu_sign_out"
android:title="Sign Out" />
</group>
</menu>
content_blank.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="70dp"
android:text="content_blank"/>
</LinearLayout>
NavigationView header is usually included this way:
<android.support.design.widget.NavigationView
android:id="#+id/nvView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#android:color/white"
app:headerLayout="#layout/main_drawer_header"
app:itemTextColor="#color/black"
app:menu="#menu/menu_main_drawer"
app:itemIconTint="#null"/>
Since support libraries version 23.1.0 NavigationView is using a RecyclerView and the header is added as one of RecyclerView's items.
Activity's findViewById won't find the header and its internals.
To get access to it you need to get the header from NavigationView and call findViewById relative to header view:
NavigationView navigationView = (NavigationView) findViewById(R.id.nvView);
View navHeaderview = navigationView.getHeaderView(0);
LinearLayout navHeaderUser = (LinearLayout) navHeaderview.findViewById(R.id.nav_header_user);
navHeaderUser.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Intent myIntent = new Intent(MainActivity.this, UserProfile.class);
// MainActivity.this.startActivity(myIntent);
// DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
// drawer.closeDrawer(GravityCompat.START);
}
});
Hope I got it right and it'll work for you!

Drawer Layout hamburger and toolbar not visible despite working nav drawer?

I am currently creating a navigation menu without using fragments. I've seemingly created the working system that opens classes and keeps the navigation bar present at each stage by using a BaseActivity and inheriting it into the classes that need the drawer. However upon loading the app seemingly the 3 bar 'Hamburger' icon isn't present and neither is the toolbar at the top (See photo). The navigation bar does seemingly work as I can swipe from the left and click to load other classes. I feel like my onCreate() in my BaseActivity is missing something but with googling I can't seem to find a solution.
Image of current issue:
Image of Nav Bar Working with issue in background:
This is my base activity:
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
public class BaseActivity extends AppCompatActivity {
DrawerLayout drawerLayout;
ActionBarDrawerToggle actionBarDrawerToggle;
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_closed);
drawerLayout.addDrawerListener(actionBarDrawerToggle);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem item) {
Intent anIntent;
switch (item.getItemId()) {
case R.id.nav_homepage:
anIntent = new Intent(getApplicationContext(), homepage.class);
startActivity(anIntent);
drawerLayout.closeDrawers();
break;
case R.id.nav_add_roster:
anIntent = new Intent(getApplicationContext(), add_roster.class);
startActivity(anIntent);
drawerLayout.closeDrawers();
break;
case R.id.nav_check_schedule:
anIntent = new Intent(getApplicationContext(), schedule.class);
startActivity(anIntent);
drawerLayout.closeDrawers();
break;
}
return false;
}
});
}
}
Here's my homepage which is what is loaded initially:
import android.os.Bundle;
import android.widget.FrameLayout;
public class homepage extends BaseActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FrameLayout contentFrameLayout = (FrameLayout) findViewById(R.id.content_frame); //Remember this is the FrameLayout area within your content_main.xml
getLayoutInflater().inflate(R.layout.homepage, contentFrameLayout);
}
}
Here's my activity main:
<?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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/content_frame"/>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigation_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" />
</android.support.v4.widget.DrawerLayout>
You can try this, add the code below in your BaseActivity :
#Override
public void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
actionBarDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
actionBarDrawerToggle.onConfigurationChanged(newConfig);
}
EDIT:
Enable the home button in your onCreate function of BaseActivity as well :
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
There is missing a toolbar layout in your xml :
<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">
<android.support.design.widget.CoordinatorLayout
android:id="#+id/rootLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#eeeeee">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.AppBarLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/content_frame"/>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigation_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" />
</android.support.v4.widget.DrawerLayout>
Hope this helps;
Sorry for my english.
Try this way,
<?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">
<include
layout="#layout/app_bar_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/navigation_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" />
</android.support.v4.widget.DrawerLayout>
and in app_bar_toolbar.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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<RelativeLayout
android:id="#+id/relative_tool_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
</android.support.v7.widget.Toolbar>
</RelativeLayout>
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
</android.support.design.widget.CoordinatorLayout>
and in content_main.xml, use this.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.esec_0052.fingertips.FingerTipMainActivity"
tools:showIn="#layout/app_bar_finger_tip_main">
<FrameLayout
android:id="#+id/frame_parent"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>

Categories