How to attach fragment to Navigationview - android

I am using NaivgationView in my app, when i select item from drawer,the view of my fragment which i set is not displaying. i also added toast it shows while i select that item can any one help me with this ?
public class MainActivity extends AppCompatActivity
{
private DrawerLayout drawerLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
//Setting Navigation View Item Selected Listener to handle the item click of the navigation menu
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
// This method will trigger on item Click of navigation menu
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
FragmentManager fragmentManager = getFragmentManager();
//Checking if the item is in checked state or not, if not make it in checked state
if(menuItem.isChecked()) menuItem.setChecked(false);
else menuItem.setChecked(true);
//Closing drawer on item click
drawerLayout.closeDrawers();
//Check to see which item was being clicked and perform appropriate action
switch (menuItem.getItemId()){
//Replacing the main content with ContentFragment Which is our Inbox View;
case R.id.home:
Toast.makeText(getApplicationContext(),"Inbox Selected", Toast.LENGTH_SHORT).show();
return true;
// For rest of the options we just show a toast on click
case R.id.be_the_donor:
Toast.makeText(getApplicationContext(),"Stared Selected",Toast.LENGTH_SHORT).show();
RegisterDonor frag = new RegisterDonor();
// update the main content by replacing fragments
fragmentManager.beginTransaction()
.replace(R.id.frame_container, frag)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.addToBackStack(null)
.commit();
return true;
case R.id.search_donor:
Toast.makeText(getApplicationContext(),"Send Selected",Toast.LENGTH_SHORT).show();
return true;
case R.id.tools:
Toast.makeText(getApplicationContext(),"Drafts Selected",Toast.LENGTH_SHORT).show();
return true;
default:
Toast.makeText(getApplicationContext(),"Somethings Wrong",Toast.LENGTH_SHORT).show();
return true;
}
}
});
// Initializing Drawer Layout and ActionBarToggle
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.navigation_drawer_open, R.string.navigation_drawer_close){
#Override
public void onDrawerClosed(View drawerView) {
// Code here will be triggered once the drawer closes as we dont want anything to happen so we leave this blank
super.onDrawerClosed(drawerView);
}
#Override
public void onDrawerOpened(View drawerView) {
// Code here will be triggered once the drawer open as we dont want anything to happen so we leave this blank
super.onDrawerOpened(drawerView);
}
};
//Setting the actionbarToggle to drawer layout
drawerLayout.setDrawerListener(actionBarDrawerToggle);
//calling sync state is necessay or else your hamburger icon wont show up
actionBarDrawerToggle.syncState();
}
#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) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Fragment
public class RegisterDonor extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.register_donor, container, false);
return rootView;
}
}
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">
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
>
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<FrameLayout
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
<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>
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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.example.dayshift.bloodfinder.MainActivity"
tools:showIn="#layout/app_bar_main">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
register_donor
<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.TextInputLayout
android:id="#+id/usernameWrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/register_donor_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:hint="Enter Name"/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="#+id/mobileWrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/register_donor_mobile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:hint="Enter Mobile"/>
</android.support.design.widget.TextInputLayout>
<com.weiwangcn.betterspinner.library.material.MaterialBetterSpinner
android:id="#+id/register_donor_bloodgroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Material Design Spinner"
android:textColorHint="#05ab9a"
app:met_floatingLabel="normal" />
<com.weiwangcn.betterspinner.library.material.MaterialBetterSpinner
android:id="#+id/register_donor_city"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Material Design Spinner"
android:textColorHint="#05ab9a"
app:met_floatingLabel="normal" />
<AutoCompleteTextView
android:id="#+id/register_donor_area"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:text="">
<requestFocus />
</AutoCompleteTextView>
</LinearLayout>

Your app bar occupies the entire screen.
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<FrameLayout
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
Change the app bar to wrap content on the height

In activity_main_drawer write your item with id and title after that in mainactivity in method onNavigationItemSelected for your id declare your fragment and commit

You are telling that the Toasts are showing up but not the fragment so the problem might be the FragmentManager
check you import of fragmentManager which should be
import android.support.v4.app.FragmentManager;
and should not be
import android.app.FragmentManager;
And your fragment transaction should be like this
FragmentManager fragmentManager = getSupportFragmentManager();
RegisterDonor frag = new RegisterDonor();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, frag , "")
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.addToBackStack(null)
.commit();
hope this will help you!

Try this way i guess it works fine :
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
int id = menuItem.getItemId();
switch (id) {
case R.id.be_the_donor:
Fragment registerDonor = new RegisterDonor();
fragmentTransaction.replace(R.id.frame_container, registerDonor);
fragmentTransaction.commit();
drawerLayout.closeDrawers();
break;

Related

I'm having an issue in Navigation view item click listener in androidx

I'm actually working on an app which includes navigation view and the app is running on androidx, I tried all the possible solutions from my point of view. Actual problem is onNavigationItemSelected is not calling even though I implemented NavigationView.OnNavigationItemSelectedListener to my base class.
My HomeActivity class which implements NavigationView.OnNavigationItemSelectedListener is
public class HomeActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private static final String TAG = "checkDate";
ActivityHomeBinding binding;
Context context;
LinearLayoutManager layoutManager;
HomeAdapter adapter;
ActionBarDrawerToggle drawerToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
context = this;
binding = DataBindingUtil.setContentView(this, R.layout.activity_home);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
drawerToggle = new ActionBarDrawerToggle(this, binding.drawer, R.string.open, R.string.close);
binding.drawer.addDrawerListener(drawerToggle);
drawerToggle.syncState();
drawerToggle.setDrawerIndicatorEnabled(false);
drawerToggle.setHomeAsUpIndicator(R.drawable.ic_menu_black_24dp);
List<EventDay> events = new ArrayList<>();
Calendar calendar = Calendar.getInstance();
events.add(new EventDay(calendar, R.drawable.ic_event_black_24dp));
binding.homeLayout.calendarView.setEvents(events);
binding.homeLayout.calendarView.setOnDayClickListener(eventDay -> {
Toast.makeText(context, eventDay.getCalendar().getTime().toString(), Toast.LENGTH_SHORT).show();
Log.d(TAG, "onDayClick: "+Calendar.DAY_OF_MONTH);
});
layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(RecyclerView.VERTICAL);
adapter = new HomeAdapter(context);
binding.homeLayout.rvEvents.setNestedScrollingEnabled(true);
binding.homeLayout.rvEvents.setHasFixedSize(true);
binding.homeLayout.rvEvents.setAdapter(adapter);
binding.homeLayout.rvEvents.setLayoutManager(layoutManager);
binding.navView.setNavigationItemSelectedListener(this);
ItemClickSupport.addTo(binding.homeLayout.rvEvents).setOnItemClickListener((recyclerView, position, v) -> {
Intent intent = new Intent(context, SiteReachActivity.class);
startActivity(intent);
});
}
#Override
public void onBackPressed() {
if (binding.drawer.isDrawerOpen(GravityCompat.START)){
binding.drawer.closeDrawer(GravityCompat.START);
return;
}
super.onBackPressed();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home){
binding.drawer.openDrawer(GravityCompat.START);
}
return true;
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
int id = menuItem.getItemId();
Intent intent;
switch (id){
case R.id.menu_profile:
intent = new Intent(context, ProfileActivity.class);
startActivity(intent);
break;
case R.id.menu_dashboard:
break;
case R.id.menu_history:
intent = new Intent(context, HistoryActivity.class);
startActivity(intent);
break;
case R.id.menu_TADA:
break;
case R.id.menu_logOut:
break;
}
return true;
}
}
And the layout file is
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<androidx.drawerlayout.widget.DrawerLayout
android:id="#+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
android:background="#color/white"
app:headerLayout="#layout/nav_header_main"
app:itemIconTint="#color/colorPrimary"
app:menu="#menu/activity_main_drawer"/>
<include
android:id="#+id/home_layout"
layout="#layout/home_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</androidx.drawerlayout.widget.DrawerLayout>
</layout>
And the layout file for activity_main_drawer is
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_profile"
android:icon="#drawable/user"
android:title="#string/user"/>
<item android:id="#+id/menu_dashboard"
android:icon="#drawable/home"
android:title="Dashboard"/>
<item android:id="#+id/menu_history"
android:icon="#drawable/history"
android:title="History"/>
<item android:id="#+id/menu_TADA"
android:icon="#drawable/wallet"
android:title="TADA"/>
<item
android:id="#+id/menu_logOut"
android:icon="#drawable/ic_power_settings_new_black_24dp"
android:title="Logout"/>
</menu>
In case anything more needed plese do comment. TIA
If you are using androidX then you have to use the NavController for navigation through fragments.
I will post the code I have tried
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
NavController mNavController;
Navigation mNavigation;
BottomNavigationView mBottomNavigationView;
NavigationView mNavigationView;
DrawerLayout mDrawerLayout;
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
mDrawerLayout = findViewById(R.id.drawer_layout);
mBottomNavigationView = findViewById(R.id.bottom_navigation_view);
mNavigationView = findViewById(R.id.navigation_view);
//mNavController = Navigation.findNavController(this,R.id.host_fragment);
mNavController = Navigation.findNavController(this,R.id.host_fragment);
NavigationUI.setupWithNavController(mBottomNavigationView,Navigation.findNavController(this,R.id.host_fragment));
NavigationUI.setupActionBarWithNavController(this, mNavController, mDrawerLayout);
NavigationUI.setupWithNavController(mNavigationView,mNavController);
mNavigationView.setNavigationItemSelectedListener(this);
//NavigationUI.setupActionBarWithNavController(this,Navigation.findNavController(this,R.id.host_fragment));
}
#Override
public boolean onSupportNavigateUp() {
/*return mNavController.popBackStack(R.id.host_fragment,false);*/
return NavigationUI.navigateUp(Navigation.findNavController(this, R.id.host_fragment), mDrawerLayout);
}
#RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
#Override
public void onBackPressed() {
NavController navController = Navigation.findNavController(this, R.id.host_fragment);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if (Objects.requireNonNull(navController.getCurrentDestination()).getId() == R.id.nav_first) {
finishAffinity();
} else {
mNavController.popBackStack(R.id.nav_first, false);
}
}
}
#RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.home:
onBackPressed();
}
return super.onOptionsItemSelected(item);
}
#RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
mDrawerLayout.closeDrawers();
int id = menuItem.getItemId();
switch (id){
//Nav drawer items
case R.id.profile:
mNavController.navigate(R.id.profile);
break;
case R.id.features:
mNavController.navigate(R.id.features);
break;
case R.id.signOut:
finishAffinity();
break;
}
return true;
}
}
You need to have the host fragment where you need to implement the fragments in the activity layout where you are implementing the Navigation
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">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="0dp"
android:layout_height="?attr/actionBarSize"
android:background="#android:color/white"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="10">
<androidx.appcompat.widget.AppCompatTextView
android:id="#+id/toolbar_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="8"
android:gravity="center"
android:padding="10dp"
android:text="#string/jetpack_example"
android:textColor="#000"
android:textSize="15sp" />
<ImageView
android:id="#+id/search_bar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="2"
android:padding="10dp"
android:src="#drawable/about_icon" />
</LinearLayout>
</androidx.appcompat.widget.Toolbar>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_navigation_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:itemBackground="#android:color/white"
app:labelVisibilityMode="selected"
app:layout_constraintBottom_toBottomOf="#+id/host_fragment"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="#menu/menu_drawer" />
<fragment
android:id="#+id/host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="#navigation/navigation_graph"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.navigation.NavigationView
android:id="#+id/navigation_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="#menu/nav_drawer" />
</androidx.drawerlayout.widget.DrawerLayout>
Next you need to have navigation graph for the navigation and using the arguments.You can also implement actions.Create a navigation folder and implement the below implementing the fragment when you click on the particular item.
navigation_graph.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/navigation_graph"
app:startDestination="#id/nav_first">
<fragment
android:id="#+id/nav_first"
android:name="com.escapadetechnologies.jetpacknavigationexample.Fragments.FirstFragment"
android:label="#string/first"
tools:layout="#layout/fragment_first" />
<fragment
android:id="#+id/nav_second"
android:name="com.escapadetechnologies.jetpacknavigationexample.Fragments.SecondFragment"
android:label="#string/second"
tools:layout="#layout/fragment_second" />
<fragment
android:id="#+id/nav_third"
android:name="com.escapadetechnologies.jetpacknavigationexample.Fragments.ThirdFragment"
android:label="#string/third"
tools:layout="#layout/fragment_third" />
<fragment
android:id="#+id/nav_fourth"
android:name="com.escapadetechnologies.jetpacknavigationexample.Fragments.FourthFragment"
android:label="#string/fourth"
tools:layout="#layout/fragment_fourth" />
<fragment
android:id="#+id/profile"
android:name="com.escapadetechnologies.jetpacknavigationexample.Fragments.ProfileFragment"
android:label="#string/profile"
tools:layout="#layout/fragment_profile" />
<fragment
android:id="#+id/features"
android:name="com.escapadetechnologies.jetpacknavigationexample.Fragments.FeaturesFragment"
android:label="#string/features"
tools:layout="#layout/fragment_features" />
It is very handy and with the androidx lot of boilerplate code can be avoided. If you have any doubts you can comment on my answer.

navigation drawer does not started

I have my navigation drawer and i need to open it but i can't>>
Here is my drawer_layout.xml:
<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">
<!-- This LinearLayout represents the contents of the screen -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- The ActionBar displayed at the top -->
<include
layout="#layout/activity_display"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- The main content view where fragments are loaded -->
<FrameLayout
android:id="#+id/flContent"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<!-- The navigation drawer that comes from the left -->
<!-- Note that `android:layout_gravity` needs to be set to 'start' -->
<android.support.design.widget.NavigationView
android:id="#+id/nvView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
android:background="#android:color/white"
app:menu="#menu/drawer_view" />
and this is my toolbar in activity_display.xml:
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/myToolBar"
android:background="#color/top_bar_backgroung">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/dots_vertical"
android:textSize="40sp"
android:layout_marginRight="10dp"
android:layout_gravity="center"
android:gravity="center"
android:id="#+id/openDrawer"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="60dp"
android:src="#drawable/logo"
android:scaleType="centerInside"/>
</android.support.v7.widget.Toolbar>
and here is how i open my drawer :
private DrawerLayout mDrawer;
private Toolbar toolbar;
private NavigationView nvDrawer;
toolbar = (Toolbar) findViewById(R.id.myToolBar);
setSupportActionBar(toolbar);
// Find our drawer view
View viewDrawer = getLayoutInflater().inflate(R.layout.drawer_layout, null);
mDrawer = (DrawerLayout) viewDrawer.findViewById(R.id.drawer_layout);
nvDrawer = (NavigationView) viewDrawer.findViewById(R.id.nvView);
setupDrawerContent(nvDrawer);
//gridView = (GridView) findViewById(R.id.displayGridView);
drawer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i(TAG, "onClick: " );
mDrawer.openDrawer(GravityCompat.END);
}
});
private void setupDrawerContent(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
selectDrawerItem(menuItem);
return true;
}
});
}
public void selectDrawerItem(MenuItem menuItem) {
// Create a new fragment and specify the fragment to show based on nav item clicked
android.support.v4.app.Fragment fragment1 = null;
Class fragmentClass = null;
switch(menuItem.getItemId()) {
case R.id.nav_home_fragment:
Toast.makeText(this, "home", Toast.LENGTH_SHORT).show();
//fragmentClass = FirstFragment.class;
break;
case R.id.nav_personal_fragment:
//fragmentClass = SecondFragment.class;
break;
case R.id.nav_avilableCareer_fragment:
//fragmentClass = ThirdFragment.class;
break;
case R.id.nav_declareCareer_fragment:
//fragmentClass = FourdFragment.class;
break;
default:
//fragmentClass = FirstFragment.class;
}
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
// Insert the fragment by replacing any existing fragment
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.flContent, fragment1).commit();
// Highlight the selected item has been done by NavigationView
menuItem.setChecked(true);
// Set action bar title
setTitle(menuItem.getTitle());
// Close the navigation drawer
mDrawer.closeDrawers();
}
i don't know why i can't start my navigation draw..
i can't see it on screen.
write end instead of right in xml file
android:layout_gravity="right"
to
android:layout_gravity="end"
or alternate solution
use
mDrawer.openDrawer(GravityCompat.RIGHT);
instead of
mDrawer.openDrawer(GravityCompat.END);

Android - How to add the parallax effect to navigation background image

I saw the video with below link:ParallaxHeaderViewPager Github: StowableHeaderViewPager and i am very interesting in the parallax effect that background image can move with animation effect. I want to add this function to my project.
Here are my code:
nav_header_main.xml
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="190dp"
android:background="#drawable/background_material_red"
android:id="#+id/header_bgdimage"
android:orientation="vertical">
<de.hdodenhof.circleimageview.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/profile_image"
android:layout_width="70dp"
android:layout_height="70dp"
android:src="#mipmap/ic_profile"
app:border_color="#FF000000"
android:layout_marginLeft="24dp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginStart="24dp" />
<!--set default name-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Wang Jian"
android:textSize="14sp"
android:textColor="#FFF"
android:textStyle="bold"
android:gravity="left"
android:paddingBottom="4dp"
android:id="#+id/username"
android:layout_above="#+id/email"
android:layout_alignLeft="#+id/profile_image"
android:layout_alignStart="#+id/profile_image" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="user#host.com"
android:id="#+id/email"
android:gravity="left"
android:layout_marginBottom="8dp"
android:textSize="14sp"
android:textColor="#fff"
android:layout_alignParentBottom="true"
android:layout_alignLeft="#+id/username"
android:layout_alignStart="#+id/username" />
</RelativeLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/nav_tabs"
android:layout_below="#+id/header_bgdimage"
android:elevation="6dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_centerHorizontal="true"
android:minHeight="?attr/actionBarSize"
android:fillViewport="false">
<Button
android:id="#+id/me"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="fill_parent"
android:drawableTop="#drawable/ic_action_user"
android:text="我的"
android:fontFamily="#string/font_fontFamily_medium"
android:textColor="#FFFFFF"
android:background="#0F1E2D" />
<Button
android:id="#+id/redeem"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="fill_parent"
android:drawableTop="#drawable/ic_action_mustache"
android:text="特权"
android:fontFamily="#string/font_fontFamily_medium"
android:textColor="#FFFFFF"
android:background="#0F1E2D"/>
<Button
android:id="#+id/topic"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="fill_parent"
android:drawableTop="#drawable/ic_action_dialog"
android:text="话题"
android:textColor="#FFFFFF"
android:fontFamily="#string/font_fontFamily_medium"
android:background="#0F1E2D" />
<Button
android:id="#+id/favorite"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="fill_parent"
android:drawableTop="#drawable/ic_action_heart"
android:text="收藏"
android:fontFamily="#string/font_fontFamily_medium"
android:textColor="#FFFFFF"
android:background="#0F1E2D"/>
</LinearLayout>
</RelativeLayout>
Now i want my navigation background image android:background="#drawable/background_material_red" can have parallax effect.
Here is my java code:NavigationActivity
public class NavigationActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
NavigationView navigationView = null;
Toolbar toolbar = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Set the fragment initially
PersonalFragment fragment = new PersonalFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
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();
navigationView = (NavigationView) findViewById(R.id.nav_view);
//How to change elements in the header programmatically
View headerView = navigationView.getHeaderView(0);
TextView ProfileText = (TextView) headerView.findViewById(R.id.email);
ProfileText.setText("马来西亚第一");
navigationView.setNavigationItemSelectedListener(this);
}
#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) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.tools_bar, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
if(id == R.id.action_search){
Toast.makeText(getApplicationContext(), "Search action is selected!", Toast.LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_profile) {
PersonalFragment fragment = new PersonalFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
setTitle(R.string.title_home);
} else if (id == R.id.nav_award) {
AwardFragment fragment = new AwardFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
setTitle(R.string.title_award);
} else if (id == R.id.nav_others) {
OthersFragment fragment = new OthersFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
setTitle(R.string.title_others);
} else if (id == R.id.nav_manage) {
setTitle(R.string.title_tools);
} else if (id == R.id.nav_setting) {
setTitle(R.string.title_setting);
} else if (id == R.id.nav_about) {
setTitle(R.string.title_about);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}`
I'm a newbie at this stuff so any help will be appreciated. Thanks in Advance!
Here's a cool library for parallax animation. I use this in my many of projects. It's easy to implement. Also if you check code you can understand how you can make parallax effect by yourself.
https://github.com/ksoichiro/Android-ObservableScrollView
There is an "official" solution for what you want to achive in the google design library, with an App Bar and a CollapsingToolbarLayout.
There's a great guide on this blog:

Android add landscape orientation Button in Navigation Drawer

I am a beginner in android, and i am trying to add a set of buttons in navigation drawer as shown in figure 2. i was create a TabLable in navigation drawer as figure 1. Now i want add button to the tablable.
my code look like this:
The first activity NavigationActivity.java
public class NavigationActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
NavigationView navigationView = null;
Toolbar toolbar = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Set the fragment initially
PersonalFragment fragment = new PersonalFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
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();
navigationView = (NavigationView) findViewById(R.id.nav_view);
//How to change elements in the header programmatically
View headerView = navigationView.getHeaderView(0);
TextView ProfileText = (TextView) headerView.findViewById(R.id.email);
ProfileText.setText("马来西亚第一");
navigationView.setNavigationItemSelectedListener(this);
}
#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) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.tools_bar, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
if(id == R.id.action_search){
Toast.makeText(getApplicationContext(), "Search action is selected!", Toast.LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_profile) {
PersonalFragment fragment = new PersonalFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
setTitle(R.string.title_home);
} else if (id == R.id.nav_award) {
AwardFragment fragment = new AwardFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
setTitle(R.string.title_award);
} else if (id == R.id.nav_others) {
OthersFragment fragment = new OthersFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
setTitle(R.string.title_others);
} else if (id == R.id.nav_manage) {
setTitle(R.string.title_tools);
} else if (id == R.id.nav_setting) {
setTitle(R.string.title_setting);
} else if (id == R.id.nav_about) {
setTitle(R.string.title_about);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
And my XML file look like this:
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>
nav_header_main.xml
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="190dp"
android:background="#drawable/background_material_red"
android:id="#+id/header_bgdimage"
android:orientation="vertical"
>
<de.hdodenhof.circleimageview.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/profile_image"
android:layout_width="70dp"
android:layout_height="70dp"
android:src="#mipmap/ic_profile"
app:border_color="#FF000000"
android:layout_marginLeft="24dp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginStart="24dp" />
<!--set default name-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Wang Jian"
android:textSize="14sp"
android:textColor="#FFF"
android:textStyle="bold"
android:gravity="left"
android:paddingBottom="4dp"
android:id="#+id/username"
android:layout_above="#+id/email"
android:layout_alignLeft="#+id/profile_image"
android:layout_alignStart="#+id/profile_image" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="user#host.com"
android:id="#+id/email"
android:gravity="left"
android:layout_marginBottom="8dp"
android:textSize="14sp"
android:textColor="#fff"
android:layout_alignParentBottom="true"
android:layout_alignLeft="#+id/username"
android:layout_alignStart="#+id/username" />
</RelativeLayout>
<android.support.design.widget.TabLayout
android:id="#+id/nav_tabs"
android:layout_below="#+id/header_bgdimage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="6dp"
app:tabTextColor="#d3d3d3"
app:tabSelectedTextColor="#ffffff"
app:tabIndicatorColor="#ffffff"
app:tabMode="fixed"
app:tabGravity="fill"
android:background="#0F1E2D"
android:minHeight="?attr/actionBarSize"
android:fillViewport="false"/>
</RelativeLayout>
nav_custom_tab.xml
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="190dp"
android:background="#drawable/background_material_red"
android:id="#+id/header_bgdimage"
android:orientation="vertical"
>
<de.hdodenhof.circleimageview.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/profile_image"
android:layout_width="70dp"
android:layout_height="70dp"
android:src="#mipmap/ic_profile"
app:border_color="#FF000000"
android:layout_marginLeft="24dp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginStart="24dp" />
<!--set default name-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Wang Jian"
android:textSize="14sp"
android:textColor="#FFF"
android:textStyle="bold"
android:gravity="left"
android:paddingBottom="4dp"
android:id="#+id/username"
android:layout_above="#+id/email"
android:layout_alignLeft="#+id/profile_image"
android:layout_alignStart="#+id/profile_image" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="user#host.com"
android:id="#+id/email"
android:gravity="left"
android:layout_marginBottom="8dp"
android:textSize="14sp"
android:textColor="#fff"
android:layout_alignParentBottom="true"
android:layout_alignLeft="#+id/username"
android:layout_alignStart="#+id/username" />
</RelativeLayout>
<android.support.design.widget.TabLayout
android:id="#+id/nav_tabs"
android:layout_below="#+id/header_bgdimage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="6dp"
app:tabTextColor="#d3d3d3"
app:tabSelectedTextColor="#ffffff"
app:tabIndicatorColor="#ffffff"
app:tabMode="fixed"
app:tabGravity="fill"
android:background="#0F1E2D"
android:minHeight="?attr/actionBarSize"
android:fillViewport="false"/>
</RelativeLayout>
I'm a newbie at this stuff so any help will be appreciated. Thanks in Advance.!

Toolbar is going invisible on switching between fragments

I am developing an android app through support design library for navigation drawer and toolbar.I am switching to fragment on click of navigation drawer item such as rootfragment->fragment1 ->fragment2 ->fragment3. So far there is no problem.but when i want to come back on rootFragment such as rootfragment<-fragment1 <- fragment2 <- fragment3.On this toolbar is going invisible on each fragment. How to solve that problem.
main activity xml file
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
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: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_marginTop="?android:attr/actionBarSize"
android:layout_width="#dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer"/>
<!--app:itemTextColor="#color/profile_name"-->
appbar xml file is
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:id="#+id/containerView">
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:minHeight="?android:attr/actionBarSize"
android:background="?attr/colorPrimary"
android:id="#+id/toolbar"
android:theme="#style/ThemeOverlay.AppCompat.Dark"/>
</LinearLayout>
Main Activity code is:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private TextView userName,userEmailId;
private ImageView profileImage;
#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();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
navigationView.getMenu().getItem(0).setChecked(true);
Fragment fragment = new HomeFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.containerView,fragment,null);
fragmentTransaction.commit();
}
#Override
public void onBackPressed()
{
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
finish();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item)
{
// Handle navigation view item clicks here.
//int id = item.getItemId();
Fragment fragment;
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
switch(item.getItemId()) {
case R.id.home:
fragment = new HomeFragment();
fragmentTransaction.replace(R.id.containerView, fragment, null);
fragmentTransaction.commit();
break;
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
Toolbar is inside of "containerView". Then you replace the "containerView" with the content of the fragments => the toolbar is not visible. To fix the problem move the Toobar outside of the "containerView".
Another approach is to have the "containerView" below the toolbar, like in the code below:
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:minHeight="?android:attr/actionBarSize"
android:background="?attr/colorPrimary"
android:id="#+id/toolbar"
android:theme="#style/ThemeOverlay.AppCompat.Dark"/>
<FrameLayout
android:id="#+id/containerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Please replace MainActivity XML with this:
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
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:fitsSystemWindows="true"
tools:openDrawer="start">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_height="match_parent"/>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_marginTop="?android:attr/actionBarSize"
android:layout_width="#dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_below="#+id/app_bar_main"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer"/>
</RelativeLayout>
<!--app:itemTextColor="#color/profile_name"-->

Categories

Resources