I work on an Android App with Navigation Drawer, but when I added the code for the Drawer to the existing code (a webview and a bottom bar) the toolbar does not display. The Drawer works.
Here is my code:
MainActivity.java
package julians.de.test;
import android.content.res.Configuration;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import com.roughike.bottombar.BottomBar;
import com.roughike.bottombar.OnMenuTabSelectedListener;
public class MainActivity extends AppCompatActivity {
private CoordinatorLayout coordinatorLayout;
private SwipeRefreshLayout swipeContainer;
Toolbar toolbar;
DrawerLayout drawerLayoutgesamt;
ActionBarDrawerToggle drawerToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar1);
setSupportActionBar(toolbar);
coordinatorLayout = (CoordinatorLayout) findViewById(R.id.three_buttons_activity);
swipeContainer = (SwipeRefreshLayout) findViewById(R.id.swipeContainer);
drawerLayoutgesamt = (DrawerLayout) findViewById(R.id.drawerlayoutgesamt);
drawerToggle = new ActionBarDrawerToggle(MainActivity.this,drawerLayoutgesamt,R.string.auf, R.string.zu);
drawerLayoutgesamt.setDrawerListener(drawerToggle);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
drawerToggle.syncState();
swipeContainer.setColorSchemeResources(android.R.color.holo_blue_bright,
android.R.color.holo_green_light,
android.R.color.holo_orange_light,
android.R.color.holo_red_light);
final WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new WebViewClient());
myWebView.getSettings().setLoadWithOverviewMode(true);
myWebView.getSettings().setUseWideViewPort(true);
myWebView.getSettings().setBuiltInZoomControls(true);
myWebView.getSettings().setDisplayZoomControls(true);
myWebView.loadUrl("http://www.apple.com");
swipeContainer.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
myWebView.reload();
swipeContainer.setRefreshing(false);
}
});
BottomBar bottomBar = BottomBar.attach(this, savedInstanceState);
bottomBar.setItemsFromMenu(R.menu.three_buttons_menu, new OnMenuTabSelectedListener() {
#Override
public void onMenuItemSelected(int itemId) {
switch (itemId) {
case R.id.recent_item:
myWebView.getSettings().setLoadWithOverviewMode(true);
myWebView.getSettings().setUseWideViewPort(true);
myWebView.getSettings().setBuiltInZoomControls(true);
myWebView.getSettings().setDisplayZoomControls(true);
myWebView.loadUrl("http://www.apple.com");
break;
case R.id.favorite_item:
myWebView.getSettings().setLoadWithOverviewMode(false);
myWebView.getSettings().setUseWideViewPort(false);
myWebView.getSettings().setBuiltInZoomControls(false);
myWebView.getSettings().setDisplayZoomControls(false);
myWebView.loadUrl("http://www.apple.com");
break;
case R.id.location_item:
myWebView.getSettings().setLoadWithOverviewMode(true);
myWebView.getSettings().setUseWideViewPort(true);
myWebView.getSettings().setBuiltInZoomControls(true);
myWebView.getSettings().setDisplayZoomControls(true);
myWebView.loadUrl("http://www.apple.com");
break;
}
}
});
bottomBar.setActiveTabColor("#C2185B");
bottomBar.useDarkTheme(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();
if (drawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(new Configuration());
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/three_buttons_activity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimary"
tools:context=".MainActivity">
<android.support.v4.widget.DrawerLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:id="#+id/drawerlayoutgesamt"
>
<!-- Activity Layout-->
<RelativeLayout
android:layout_width="match_parent"
android:id="#+id/activitylayout"
android:layout_height="match_parent" >
<include
android:id="#+id/toolbar1"
layout="#layout/tool_bar"
/>
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/swipeContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
<!-- Drawer Layout -->
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/drawerlayoutsingle"
android:layout_gravity="start"
android:background="#fff"
>
<TextView
android:text="Drawer Layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
</android.support.design.widget.CoordinatorLayout>
tool_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/toolbar1"
android:background="#ffd3d3d3"
android:layout_alignParentTop="true"
android:minHeight="?attr/actionBarSize"
android:fitsSystemWindows="true">
</android.support.v7.widget.Toolbar>
styles.xml
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="drawerArrowStyle">#style/drawerarrowstyle1</item>
</style>
<style name="drawerarrowstyle1" parent="Widget.AppCompat.DrawerArrowToggle">
<item name="spinBars">true</item>
</style>
</resources>
Thanks for your help!
Have you tried using the Navigation Drawer template provided by android studio and if so use a fragment as main activity in place of the activity
DrawerLayout acts as a top-level container for window content ... To use a DrawerLayout, position your primary content view as the first child with width and height of match_parent and no layout_gravity
You should try this structure
DrawerLayout
CoordinatorLayout
AppBarLayout
<include> toolbar
<!-- activity layout (without toolbar) -->
<!-- drawer layout -->
Related
i am having a serious weird bug in android navigation view item click, the items are only clicking once when you open the app and stops clicking after, but the weirdest part is that the item is responding to click at the edge every time.
Below is my main xml code
<?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:layoutDirection="ltr"
tools:openDrawer="start">
<include layout="#layout/layout_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:theme="#style/NavigationView"
app:itemTextColor="#color/black"
app:itemIconTint="#color/black"
android:background="#color/white"
app:headerLayout="#layout/nav_header_main"
android:elevation="5dp"
app:menu="#menu/activity_main_drawer" />
</androidx.drawerlayout.widget.DrawerLayout>
Navigation menu items
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:checked="true"
android:id="#+id/home"
android:icon="#drawable/ic_home"
android:title="#string/home" />
<item
android:id="#+id/latest"
android:icon="#drawable/ic_latest"
android:title="#string/latest" />
<item
android:id="#+id/category"
android:icon="#drawable/ic_category"
android:title="#string/category" />
</group>
</menu>
The layout Frame
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="#style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/toolbar"
android:theme="#style/AppTheme.AppBarOverlay"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>
</com.google.android.material.appbar.AppBarLayout>
<FrameLayout
android:layout_marginTop="55dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/frame_one"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
My Activity code
package com.education.books.MySchoolLibrary;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.drawerlayout.widget.DrawerLayout;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.google.android.material.navigation.NavigationView;
public class NewAct extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
public Toolbar toolbar;
private ProgressBar progressBar;
private DrawerLayout drawer;
private NavigationView navigationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Tool");
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
this.navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
View headerview = navigationView.getHeaderView(0);
findViewById(R.id.frame_one).setOnClickListener(v -> {
Toast.makeText(this, "frame ti ya werey!!!", Toast.LENGTH_SHORT).show();
});
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
int id = item.getItemId();
drawer.closeDrawers();
if (id == R.id.home) {
Toast.makeText(this, "clicked", Toast.LENGTH_SHORT).show();
} else if (id == R.id.latest) {
Toast.makeText(this, "clicked", Toast.LENGTH_SHORT).show();
} else if (id == R.id.category) {
Toast.makeText(this, "clicked", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "clicked", Toast.LENGTH_SHORT).show();
}
return true;
}
}
But if i copy this whole code into an entirely new application, it is working fine, pls i don't know what i am doing wrong. Thanks.
I am also facing same issue.found a work around solution like below
nav_view.menu.findItem(R.id.menu_share).setOnMenuItemClickListener {
shareApp()
drawer_layout.closeDrawer(GravityCompat.START)
true
}
I want to make different content in the tabs for each navbar item. When I select Navbar1 and go to Tab one or Tab two or whatever, I want to show a text. When I switch to Navbar2 in navbar and select Tab one, Tab two etc.. I want to show a different content.
I'll show the code if necessary, but it's just android-studio default navbar drawer and I added tabs fragments.
Can you please give me any directions on how to proceed in regards to tabs fragments, mainactivity, navbar?
EDIT(Code and SS added):
Images: https://imgur.com/a/IVC5FrS
MainActivity.java
package cf.hazzu.bacalaureat;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
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.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.Toast;
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);
TabLayout tabLayout = (TabLayout)findViewById(R.id.tabs);
ViewPager Pager = (ViewPager)findViewById(R.id.viewpager);
tabpagerAdapter Tabpageradapter = new tabpagerAdapter(getSupportFragmentManager());
Pager.setAdapter(Tabpageradapter);
tabLayout.setupWithViewPager(Pager);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
#Override
public void 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_donate) {
Intent viewIntent =
new Intent("android.intent.action.VIEW",
Uri.parse("https://hazzu.cf/app/"));
startActivity(viewIntent);
return true;
}
if (id == R.id.action_info) {
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_one) {
setTitle("Navbar 1");
} else if (id == R.id.nav_two) {
setTitle("Navbar 2");
} else if (id == R.id.nav_three) {
setTitle("Navbar 3");
} else if (id == R.id.nav_smoke) {
Context context = getApplicationContext();
CharSequence text = "Some text, idk...";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
tabpagerAdapter.java
package cf.hazzu.bacalaureat;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
public class tabpagerAdapter extends FragmentStatePagerAdapter {
String[] tabarray = new String[]{"Tab1", "Tab2", "Tab3"};
Integer tabnumber = 3;
public tabpagerAdapter(FragmentManager fm) { super(fm); }
#Nullable
#Override
public CharSequence getPageTitle(int position) {
return tabarray[position];
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
tab_one tab1 = new tab_one();
return tab1;
case 1:
tab_two tab2 = new tab_two();
return tab2;
case 2:
tab_three tab3 = new tab_three();
return tab3;
}
return null;
}
#Override
public int getCount() {
return tabnumber;
}
}
tab_one.java
package cf.hazzu.bacalaureat;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {#link Fragment} subclass.
*/
public class tab_one extends Fragment {
public tab_one() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_tab_one, container, false);
}
}
tab_two.java
package cf.hazzu.bacalaureat;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {#link Fragment} subclass.
*/
public class tab_two extends Fragment {
public tab_two() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_tab_two, container, false);
}
}
tab_three.java
package cf.hazzu.bacalaureat;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {#link Fragment} subclass.
*/
public class tab_three extends Fragment {
public tab_three() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_tab_three, container, false);
}
}
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=".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"
app:popupTheme="#style/AppTheme.PopupOverlay" />
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:tabIndicatorColor="#color/white">
<android.support.design.widget.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tab One" />
<android.support.design.widget.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tab Two" />
<android.support.design.widget.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tab Three" />
</android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
</android.support.design.widget.CoordinatorLayout>
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=".MainActivity"
tools:showIn="#layout/app_bar_main">
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
</android.support.constraint.ConstraintLayout>
fragment_tab_one.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".tab_one">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/screen_tab1"
android:gravity="center"
android:textSize="24sp"/>
</FrameLayout>
fragment_tab_two.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".tab_two">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/screen_tab2"
android:gravity="center"
android:textSize="24sp"/>
</FrameLayout>
menu/activity_main_drawer.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_one"
android:icon="#drawable/ic_font_download_black_24dp"
android:title="Navbar 1" />
<item
android:id="#+id/nav_two"
android:icon="#drawable/ic_face_black_24dp"
android:title="Navbar 2" />
<item
android:id="#+id/nav_three"
android:icon="#drawable/ic_insert_drive_file_black_24dp"
android:title="Navbar 3" />
</group>
<item android:title="Chestii">
<menu>
<item
android:id="#+id/nav_smoke"
android:icon="#drawable/ic_smoking_rooms_black_24dp"
android:title="Smth" />
</menu>
</item>
</menu>
so i made a menu class and now i try to extend my others activitys from that one
i get the button but the slide its not working in the others activitys but its apears on the left top... lest go to the code
this is my xml activity_navigatoin_menu
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
android:id="#+id/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="ar.com.puebloyreforma.pyr.NavigationMenu">
<android.support.design.widget.NavigationView
app:headerLayout="#layout/menuheader"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/caldroid_black"
app:itemTextColor="#color/white"
app:itemIconTint="#color/white"
app:menu="#menu/drawermenu"
android:layout_gravity="start"
>
</android.support.design.widget.NavigationView>
this is my menudrawer
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/noticias" android:title="Noticias" android:icon="#drawable/iconews"></item>
<item android:id="#+id/calendario" android:title="Calendario" android:icon="#drawable/icocalen"></item>
<item android:id="#+id/sysacad" android:title="Sysacad" android:icon="#drawable/icosysa"></item>
<item android:id="#+id/contacto" android:title="Contacto" android:icon="#drawable/icocon"></item>
<item android:id="#+id/reglamento" android:title="Reglamento" android:icon="#drawable/icoreg"></item>
</menu>
and here my activity
package ar.com.puebloyreforma.pyr;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
public class NavigationMenu extends AppCompatActivity {
private DrawerLayout mDL ;
private ActionBarDrawerToggle mT ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation_menu);
mDL = (DrawerLayout) findViewById(R.id.drawer) ;
mT = new ActionBarDrawerToggle(this , mDL ,R.string.open , R.string.close) ;
mDL.addDrawerListener(mT);
mT.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mT.onOptionsItemSelected(item)){
return true ;
}
return super.onOptionsItemSelected(item);
}
}
and this is one child activity
package ar.com.puebloyreforma.pyr;
import android.app.ActionBar;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
public class PdfsActivity extends NavigationMenu {
public String getTarget() {
return target;
}
public void setTarget(String target) {
this.target = target;
}
public String target ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pdfs);
Button tt , tm , tn, reg ;
tt = (Button)this.findViewById(R.id.tt);
tm = (Button)this.findViewById(R.id.tm);
tn = (Button)this.findViewById(R.id.tn);
reg = (Button)this.findViewById(R.id.reg);
final String pdfs[] = {"tm" ,
"tt" ,
"tn",
"Ord1549"
} ;
tm.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setTarget(pdfs[0]);
Intent hola = new Intent (PdfsActivity.this,ReglamentoActivity.class);
hola.putExtra("TARGET", getTarget());
startActivity(hola);
}
});
tt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setTarget(pdfs[1]);
Intent hola = new Intent (PdfsActivity.this,ReglamentoActivity.class);
hola.putExtra("TARGET", getTarget());
startActivity(hola);
}
});
tn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setTarget(pdfs[2]);
Intent hola = new Intent (PdfsActivity.this,ReglamentoActivity.class);
hola.putExtra("TARGET", getTarget());
startActivity(hola);
}
});
reg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setTarget(pdfs[3]);
Intent hola = new Intent (PdfsActivity.this,ReglamentoActivity.class);
hola.putExtra("TARGET", getTarget());
startActivity(hola);
}
});
}
that has its onw layout
<?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"
android:orientation="vertical"
tools:context="ar.com.puebloyreforma.pyr.PdfsActivity">
<Button
android:id="#+id/tm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MAƱANA"
android:layout_marginRight="60dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintVertical_bias="0.758"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent" />
<Button
android:id="#+id/tt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TARDE"
android:layout_marginBottom="8dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.335"
android:layout_marginRight="123dp"
app:layout_constraintRight_toRightOf="parent" />
<Button
android:id="#+id/tn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="NOCHE"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
android:layout_marginLeft="68dp"
app:layout_constraintVertical_bias="0.572"
app:layout_constraintLeft_toLeftOf="parent" />
<Button
android:id="#+id/reg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="REGLA"
app:layout_constraintLeft_toRightOf="#+id/tn"
android:layout_marginLeft="0dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
android:layout_marginRight="8dp"
app:layout_constraintVertical_bias="0.572"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintHorizontal_bias="0.713" />
</android.support.constraint.ConstraintLayout>
Let me post my code with which I manage to obtain NavigationDrawer in child activity.
MyAppCompatActivity(ParentActivity class)
public abstract class MyAppCompatActivity extends BaseAppCompatActivity {
#Override
public void setContentView(#LayoutRes int layoutResID) {
CoordinatorLayout activityParentBase = (CoordinatorLayout) getLayoutInflater().inflate(R.layout.activity_navigation_drawer, null);
RelativeLayout content = (RelativeLayout) activityParentBase.findViewById(R.id.content);
setContentView(activityParentBase);
getLayoutInflater().inflate(layoutResID, content, true);
super.setContentView(activityParentBase);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("");
setSupportActionBar(toolbar);
final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
MyAppCompatActivity.this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
int id = item.getItemId();
if (id == R.id.nav_item1) {
} else if (id == R.id.nav_item2) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
});
}
}
activity_navigation_drawer.xml(ParentActivity XML)
<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"
android:fitsSystemWindows="true"
tools:context=".MyAppCompatActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
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>
<android.support.v4.widget.DrawerLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/drawer_layout"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:openDrawer="start">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/content" />
<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:menu="#menu/menu_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
</android.support.design.widget.CoordinatorLayout>
ChildNavigationActivity (ChildActivity class)
public class ChildNavigationActivity extends MyAppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_child_navigation);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); //we have included "toolbar" in the parentactivity xml. So not need to do it in childactivity xml.
/**NOTE: Do Not use "setSupportActionBar(toolbar)" since you have already done it in your parent activity*/
toolbar.setTitle("ChildNavigationActivity");
}
}
activity_child_navigation(ChildActivity XML)
<FrameLayout 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=".ChildNavigationActivity">
<!--Put all your ChildActivity Widgets here-->
</FrameLayout>
I am trying to implement navigation drawer inside the fragment, but I am facing problem when I am implementing the NavigationListener in FeedFragment.java, can you please help me to solve the problem and help me knowing the procedure implementing listener and toolbar inside the fragment activity. I have added all the code structure below which I have used to implement it.and even attached screenshot. Many a thanks in advance.
here is the screenshot which I am implementing
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"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.sirajm.boom.MainActivity">
<include layout="#layout/content_main" />
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="?android:attr/windowBackground"
app:menu="#menu/navigation" />
</LinearLayout>
content_main.xml
<FrameLayout android:id="#+id/content_panel"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
xmlns:android="http://schemas.android.com/apk/res/android">
</FrameLayout>
fragment_feed.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.design.widget.NavigationView>
</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=".MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme">
<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" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_fragment" />
</android.support.design.widget.CoordinatorLayout>
content_fragment.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="match_parent"
android:layout_height="match_parent"
android:text="Hello Blank Fragment" />
</LinearLayout>
MainActivity.java
package com.example.sirajm.boom;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.TextView;
import com.example.sirajm.boom.Fragments.FeedFragment;
import com.example.sirajm.boom.Fragments.twoFragment;
import com.example.sirajm.boom.Fragments.threeFragment;
import com.example.sirajm.boom.Fragments.fourFragment;
import com.example.sirajm.boom.Fragments.fiveFragment;
public class MainActivity extends AppCompatActivity {
private TextView mTextMessage;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
getSupportFragmentManager().beginTransaction().replace(R.id.content_panel,new FeedFragment()).commit();
return true;
case R.id.navigation_store:
getSupportFragmentManager().beginTransaction().replace(R.id.content_panel,new twoFragment()).commit();
return true;
case R.id.navigation_orders:
getSupportFragmentManager().beginTransaction().replace(R.id.content_panel,new threeFragment()).commit();
return true;
case R.id.navigation_service:
getSupportFragmentManager().beginTransaction().replace(R.id.content_panel,new fourFragment()).commit();
return true;
case R.id.navigation_profile:
getSupportFragmentManager().beginTransaction().replace(R.id.content_panel,new fiveFragment()).commit();
return true;
}
return false;
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// mTextMessage = (TextView) findViewById(R.id.message);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
BottomNavigationHelper.disableShiftMode(navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
getSupportFragmentManager().beginTransaction().add(R.id.content_panel,new FeedFragment()).commit();
}
}
FeedFragment.java
package com.example.sirajm.boom.Fragments;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.example.sirajm.boom.R;
public class FeedFragment extends Fragment implements NavigationView.OnNavigationItemSelectedListener {
View rootView;
public FeedFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
rootView =inflater.inflate(R.layout.fragment_feed, container, false);
Toolbar toolbar = (Toolbar) rootView.findViewById(R.id.toolbar);
DrawerLayout drawer = (DrawerLayout) rootView.findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
getActivity(), drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) rootView.findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener((NavigationView.OnNavigationItemSelectedListener) getActivity());
return rootView;
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
switch (id) {
case R.id.nav_event:
Toast.makeText(getContext(), "ev1", Toast.LENGTH_SHORT).show();
break;
case R.id.nav_news:
Toast.makeText(getContext(), "ev2", Toast.LENGTH_SHORT).show();
break;
case R.id.nav_offers:
Toast.makeText(getContext(), "ev3", Toast.LENGTH_SHORT).show();
break;
case R.id.nav_problems:
Toast.makeText(getContext(), "ev4", Toast.LENGTH_SHORT).show();
break;
case R.id.nav_talents:
Toast.makeText(getContext(), "ev5", Toast.LENGTH_SHORT).show();
break;
case R.id.nav_shortmov:
Toast.makeText(getContext(), "ev6", Toast.LENGTH_SHORT).show();
break;
case R.id.nav_share:
Toast.makeText(getContext(), "ev7", Toast.LENGTH_SHORT).show();
break;
case R.id.nav_send:
Toast.makeText(getContext(), "Send", Toast.LENGTH_SHORT).show();
break;
case R.id.nav_logout:
Toast.makeText(getContext(), "logout", Toast.LENGTH_SHORT).show();
break;
}
DrawerLayout drawer = (DrawerLayout) rootView.findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
Why don't you make your base activity implements NavigationView.OnNavigationItemSelectedListener
Then add these those calls
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.setItemIconTintList(null);
navigationView.setNavigationItemSelectedListener(this);
So you don't need to implement it from fragment.
You just replace or add the fragments.
I'm trying to create a navigation drawer with swipe tab. But my viewpager is not working fine. I can only select the tab. Viewpager can not load any fragment. so I can not swipe it. If i change the height of the viewpager it hides the tab and load all the fragment.
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<include
android:id="#+id/app_bar"
layout="#layout/app_bar" />
<android.support.v4.widget.DrawerLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/drawer_layout">
<FrameLayout
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<android.support.design.widget.TabLayout
android:id="#+id/sliding_tab"
style="#style/MyCustomTabLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
app:tabMode="scrollable" />
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#android:color/white" />
</FrameLayout>
<fragment
android:id="#+id/nav_drawer"
android:name="com.example.usaukglu.tablayoyt.NavigationDrawer"
android:layout_width="#dimen/drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
app:layout="#layout/navigation_drawer"
tools:layout="#layout/navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
Here is my MainActivity.java Code:
package com.example.usaukglu.tablayoyt;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
NavigationDrawer navigationDrawer;
DrawerLayout drawerLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_appbar);
ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
TabLayout tabLayout= (TabLayout) findViewById(R.id.sliding_tab);
toolbar= (Toolbar) findViewById(R.id.app_bar);
drawerLayout= (DrawerLayout) findViewById(R.id.drawer_layout);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager(),MainActivity.this));
tabLayout.setupWithViewPager(viewPager);
navigationDrawer= (NavigationDrawer) getSupportFragmentManager().findFragmentById(R.id.nav_drawer);
navigationDrawer.setUp(R.id.nav_drawer,drawerLayout,toolbar);
}
}
Here is my ViewpagerAdapter.java code
package com.example.usaukglu.tablayoyt;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
class ViewPagerAdapter extends FragmentPagerAdapter {
final int PAGE_COUNT = 3;
private String tabTitles[] = new String[] { "Tab1", "Tab2", "Tab3" };
private Context context;
public ViewPagerAdapter(FragmentManager fm, Context context) {
super(fm);
this.context = context;
}
#Override
public int getCount() {
return PAGE_COUNT;
}
#Override
public Fragment getItem(int position) {
Fragment fragment=null;
if (position==0){
fragment=new FragmentPage();
}
if (position==1){
fragment=new FragmentExpense();
}
if (position==2){
fragment=new FragmentIncome();
}
return fragment;
}
#Override
public CharSequence getPageTitle(int position) {
// Generate title based on item position
return tabTitles[position];
}
}
You shouldn't use FrameLayout as container for the tab + pager. FrameLayout is used mostly to put views above others. To fix it you should change this FrameLayout to LinearLayout with vertical orientation. Also the view page height should be match_parent.