intent activity won't start if map is included in the layout - java

My class extends FragmentActivity... Intent activity will start when map is not included in the layout.
But unfortunately, the intent activity won't start if activity_main_map is included in activity_main_nav. Can somebody help me?
public class MainMapActivity extends FragmentActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_nav);
setUpToolbarDrawer();
setUpNavView();
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.add_bus) {
Intent intent = new Intent(MainMapActivity.this, AddBusActivity.class);
startActivity(intent);
} else if (id == R.id.add_driver) {
Intent intent = new Intent(MainMapActivity.this, AddDriverActivity.class);
startActivity(intent);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
public void setUpToolbarDrawer() {
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawerLayout.setDrawerListener(toggle);
toggle.syncState();
}
public void setUpNavView() {
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
activity_main_nav
<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" />
<include
layout="#layout/activity_main_map"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
activity_main_map
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />

I just put the appBar and the included map inside a LinearLayout
Just like this.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include xmlns:android="http://schemas.android.com/apk/res/android"
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<include
layout="#layout/activity_main_map"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</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" />

Related

Toggle button not opening Navigation Drawer [duplicate]

This question already has answers here:
Clicking hamburger icon on Toolbar does not open Navigation Drawer
(1 answer)
on clicking hamburger icon navigation drawer not opening
(2 answers)
Closed 3 years ago.
The drew the red line to show where the orange line should keep going.
After searching similar questions, I could not find a way to solve my problem.
I have a toggle button that shows in my Activity but when I press it, it doesn't open the navigation drawer (if I slide my finger it opens).
*The toggle button shows in the activity (the toolbar is transparent)
OnCreate and onOptionsItemSelect
ActionBarDrawerToggle toggle;
DrawerLayout drawer;
NavigationView navigationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.my_toolbar);
setSupportActionBar(toolbar);
drawer = findViewById(R.id.drawer_layout);
toggle = new ActionBarDrawerToggle
(
this,
drawer,
R.string.navigation_drawer_open,
R.string.navigation_drawer_close
)
{
};
drawer.addDrawerListener(toggle);
toggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setTitle("");
toggle.getDrawerArrowDrawable().setColor(getResources().getColor(R.color.colorPrimaryDark));
FloatingActionButton fab = findViewById(R.id.action_btn);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), NewEventActivity.class);
startActivity(intent);
/*Intent intent = new Intent(getApplicationContext(), testes.class);
startActivity(intent);*/
}
});
navigationView = findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(this);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(toggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
XML code:
<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="#+id/drawer_layout">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:title=""
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/wrapper_layout">
</LinearLayout>
</RelativeLayout>
<android.support.design.widget.FloatingActionButton
android:id="#+id/action_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:backgroundTint="#FE7D4B"
app:srcCompat="#drawable/ic_add" />
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:menu="#menu/drawer_menu"
android:layout_gravity="start"
android:id="#+id/navigation_view"
>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>

Navigation Drawer is not clickable [duplicate]

I am trying to use NavigationView from Android Support Design library in my app. For some reason, OnNavigationItemSelected listener is not being called. Here is my code
Activity Layout
<?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"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/drawer_header"
app:menu="#menu/drawer_menu" />
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.DrawerLayout>
Activity onCreate()
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutID());
toolbar = (Toolbar) findViewById(R.id.activity_toolbar);
setSupportActionBar(toolbar);
toolbar.inflateMenu(R.menu.common_menu);
final ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setHomeAsUpIndicator(R.drawable.ic_menu_white_24dp);
actionBar.setDisplayHomeAsUpEnabled(true);
}
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
navigationView = (NavigationView) findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(final MenuItem menuItem) {
Snackbar.make(contentLayout, menuItem.getTitle() + " pressed", Snackbar.LENGTH_LONG).show();
menuItem.setChecked(true);
// allow some time after closing the drawer before performing real navigation
// so the user can see what is happening
drawerLayout.closeDrawer(GravityCompat.START);
mDrawerActionHandler.postDelayed(new Runnable() {
#Override
public void run() {
navigate(menuItem.getItemId());
}
}, DRAWER_CLOSE_DELAY_MS);
drawerLayout.closeDrawers();
return true;
}
});
usernameTextView = (TextView) findViewById(R.id.drawer_header_username);
usernameTextView.setText(getAppDContext().getAccount().getUsername());
}
When you make XML layout, you should write down NavigationView after BaseLayout (FrameLayout, LinearLayout, etc..)
<DrawerLayout>
<FrameLayout />
<NavigationView />
</DrawerLayout>
For me this did the trick!
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.bringToFront();
Your activity main layout should look like this:
<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/navigationDrawer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="#layout/activity_main_content" />
<android.support.design.widget.NavigationView
android:id="#+id/navigationView"
style="#style/NavigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
app:headerLayout="#layout/header"
app:menu="#menu/menu_drawer"/>
</android.support.v4.widget.DrawerLayout>
In this NavigationView I linked header.xml and menu_drawer.xml (from menu folder)
for example menu_drawer.xml :
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav1"
android:checked="true"
android:icon="#drawable/logo"
android:title="Navigation item 1"/>
<item
android:id="#+id/nav2"
android:icon="#drawable/logo"
android:title="Navigation item 2"/>
</group>
</menu>
than your java code:
public class ActivityMain extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setUpToolbar();
setUpNavDrawer();
}
private void setUpNavDrawer() {
NavigationView view = (NavigationView) findViewById(R.id.navigationView);
mDrawerLayout = (DrawerLayout) findViewById(R.id.navigationDrawer);
view.setNavigationItemSelectedListener(this);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.drawerOpen, R.string.drawerClose);
mDrawerLayout.addDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
}
Check if this work for you. In my project works like a charm.

how can put navigation view when is open on bottom navigaion view?

i want to set navigation view when is open on bottom navigation but i dont khow how do it.i set image below look that i want to dont be navigation behind the bottom navigation but i dont khow i think should customize navigation view and use it instead android navigation view
this is my activity_main.xml
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:layoutDirection="rtl"
tools:openDrawer="start">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
/>
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_marginTop="?attr/actionBarSize"
android:layout_marginBottom="?attr/actionBarSize"
android:layout_height="wrap_content"
app:layout_behavior="android.support.design.widget.AppBarLayout$ScrollingViewBehavior">
<FrameLayout
android:id="#+id/frame_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.v4.widget.NestedScrollView>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="260dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
android:background="#color/white"
app:itemIconTint="#color/darkGray"
app:headerLayout="#layout/layout_navigation_header"
app:itemTextColor="#color/darkGray"/>
</android.support.v4.widget.DrawerLayout>
<com.aurelhubert.ahbottomnavigation.AHBottomNavigation
android:id="#+id/bottomNavigation_main"
android:layout_gravity="bottom"
android:layout_alignParentBottom="true"
android:layoutDirection="rtl"
android:background="#color/colorPrimary"
android:fitsSystemWindows="false"
android:layout_width="match_parent"
app:itemIconTint="#color/navigation_item_color"
app:itemTextColor="#color/navigation_item_color"
android:layout_height="wrap_content" />
and this is my MainActivity.java
public class MainActivity extends AppCompatActivity {
DrawerLayout drawerLayout;
FrameLayout frameLayout;
AHBottomNavigation bottomNavigationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupToolbar();
setupBottomNavigation();
}
private void setupToolbar() {
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(this,
drawerLayout, toolbar, 0, 0);
drawerLayout.addDrawerListener(drawerToggle);
drawerToggle.syncState();
}
private void setupBottomNavigation() {
frameLayout = (FrameLayout) findViewById(R.id.frame_layout);
bottomNavigationView = (AHBottomNavigation) findViewById(R.id.bottomNavigation_main);
AHBottomNavigationItem item1 = new AHBottomNavigationItem(R.string.bottom_navigation_home, R.drawable.ic_home, R.color.navigation_item_color);
AHBottomNavigationItem item2 = new AHBottomNavigationItem(R.string.bottom_navigation_message, R.drawable.ic_home, R.color.navigation_item_color);
AHBottomNavigationItem item3 = new AHBottomNavigationItem(R.string.bottom_navigation_Accounting, R.drawable.ic_home, R.color.navigation_item_color);
AHBottomNavigationItem item4 = new AHBottomNavigationItem(R.string.bottom_navigation_archive, R.drawable.ic_home, R.color.navigation_item_color);
bottomNavigationView.addItem(item1);
bottomNavigationView.addItem(item2);
bottomNavigationView.addItem(item3);
bottomNavigationView.addItem(item4);
bottomNavigationView.setCurrentItem(0, true);
bottomNavigationView.setDefaultBackgroundColor(getResources().getColor(R.color.colorPrimary));
bottomNavigationView.setAccentColor(Color.parseColor("#fdfdfe"));
bottomNavigationView.setInactiveColor(Color.parseColor("#bdbdbd"));
bottomNavigationView.setTitleState(AHBottomNavigation.TitleState.ALWAYS_SHOW);
bottomNavigationView.setOnTabSelectedListener(new AHBottomNavigation.OnTabSelectedListener() {
#Override
public boolean onTabSelected(int position, boolean wasSelected) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
switch (position) {
case 0:
//transaction.replace(R.id.frameLayout_main, new Fragment_home());
drawerLayout.closeDrawers();
break;
case 1:
//transaction.replace(R.id.frameLayout_main, new Fragment_Message());
drawerLayout.closeDrawers();
break;
case 2:
//transaction.replace(R.id.frameLayout_main, new Fragment_Message());
drawerLayout.closeDrawers();
break;
case 3:
// transaction.replace(R.id.frameLayout_main, new Fragment_Message());
drawerLayout.closeDrawers();
break;
}
transaction.commit();
return true;
}
});
}
}
i want to put navigation in front of bottom navigation
findViewById(R.id.drawer_button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// open right drawer
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.openDrawer(GravityCompat.END);
}
});
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
Here is complete reference
https://v4all123.blogspot.in/2016/03/simple-example-of-navigation-view-on.html

Working with Navigation Drawer in multiple activities?

I'm new and I'm learning to use the Android Studio Navigation Drawer Activity Template code (mainMenu.java)
to start this project
I've created another Empty Activity (viewProfile.java) so that it can be called when user press the Navigation Drawer menu. Example: User press 'View Profile' inside the menu.
I need help on how to have a single Navi Drawer menu in all my activities, like viewProfile.java Activity.
Tried copying all codes related to Navi Drawer from mainMenu.java and paste into viewProfile.java. Doesnt work at all, sadly.
I've commented some of the codes that I have tried.
mainMenu.java
public class mainMenu extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
//Defining views
private TextView editTextUserName;
private TextView textView_profile_name;
private TextView textView_profile_email;
private TextView textView_profile_amount;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
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);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_profile) {
Toast.makeText(this, "nav_profile", Toast.LENGTH_SHORT).show();
} else if (id == R.id.nav_listing) {
startActivity(new Intent(this, ViewAllStock.class));
} else if (id == R.id.nav_history) {
Toast.makeText(this, "nav_history", Toast.LENGTH_SHORT).show();
} else if (id == R.id.nav_coming) {
Toast.makeText(this, "nav_coming", Toast.LENGTH_SHORT).show();
} else if (id == R.id.nav_logout) {
logout();
} else if (id == R.id.nav_setting) {
Toast.makeText(this, "nav_setting", Toast.LENGTH_SHORT).show();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
viewProfile.java
public class viewProfile extends AppCompatActivity implements ListView.OnItemClickListener, NavigationView.OnNavigationItemSelectedListener {
private ListView listView;
private String JSON_STRING;
private SwipyRefreshLayout mSwipyRefreshLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_all_stock);
// 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(ViewAllStock.this);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_profile) {
// Handle the camera action
Toast.makeText(ViewAllStock.this, "nav_profile", Toast.LENGTH_SHORT).show();
} else if (id == R.id.nav_listing) {
Toast.makeText(ViewAllStock.this, "nav_listing", Toast.LENGTH_SHORT).show();
} else if (id == R.id.nav_history) {
Toast.makeText(ViewAllStock.this, "nav_history", Toast.LENGTH_SHORT).show();
} else if (id == R.id.nav_coming) {
Toast.makeText(ViewAllStock.this, "nav_coming", Toast.LENGTH_SHORT).show();
} else if (id == R.id.nav_logout) {
Toast.makeText(ViewAllStock.this, "nav_logout", Toast.LENGTH_SHORT).show();;
} else if (id == R.id.nav_setting) {
Toast.makeText(ViewAllStock.this, "nav_setting", Toast.LENGTH_SHORT).show();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
It is advisable to use multiple fragments rather than activities in such a case. But even if you still want to use different activities then simply include navigation drawer in other activities the same way you implemented in one of them. However this leads to writing of same piece of code multiple times, which should be avoided.
Use Fragments and containers so that if you would like to change the layout you can change only part of layout not the entire layout.So your navigation drawer remains safe through out your app.
You should use fragments for this and you can get lots of demo for this.. and if still you wanna use activity you have to include nevigation in every layout like this
<?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" >
<RelativeLayout
android:layout_width="match_parent"
android:background="#drawable/bg"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/container_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:orientation="vertical" >
<include
android:id="#+id/toolbar_offr"
layout="#layout/toolbar_offer" />
</LinearLayout>
<ListView
android:id="#+id/OfferFragLV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_below="#+id/ll1">
</ListView>
</RelativeLayout>
<fragment
android:id="#+id/fragment_navigation_drawer"
android:name="com.phoenix.spicejunction.frag.FragmentDrawer"
android:layout_width="#dimen/nav_drawer_width"
android:layout_height="match_parent"
android:layout_below="#id/mainRL"
android:layout_gravity="start"
android:layout_marginTop="50dp"
app:layout="#layout/fragment_navigation_drawer"
tools:layout="#layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
and here is my fragment_navigation_drawer layout file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:layout_marginTop="40dp"
>
<RelativeLayout
android:id="#+id/nav_header_container"
android:layout_width="match_parent"
android:layout_height="145dp"
android:layout_alignParentTop="true"
android:background="#fff"
android:gravity="center" >
<com.phoenix.spicejunction.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/profileIV"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="#dimen/_10sdp"
android:src="#drawable/ic_launcher"
app:civ_border_color="#color/my_yellow"
app:civ_border_width="3dp" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/profileIV"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:text="TextView"
android:textColor="#000" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="#dimen/_5sdp"
android:text="AGE: "
android:textColor="#000" />
</RelativeLayout>
<!--
<android.support.v7.widget.RecyclerView
android:id="#+id/drawerList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/nav_header_container"
android:layout_marginTop="15dp" />
-->
<ListView
android:id="#+id/home_listDrawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/nav_header_container"
android:layout_gravity="start"
android:layout_marginTop="5dp"
android:entries="#array/slider_array" />
</RelativeLayout>
and i am using this this method
#Override
public void onDrawerItemSelected(View view, int position) {
base.displayView(position);
}
so you have to implemenmt this method in every activity and by calling any activity make them finish..

Viewpager Tabs Fragment & Normal Fragments

I apologize in beforehand for my bad code and bad knowledge. I have just recently started coding in Android and Java
My problem is that I have Navigation Drawer and Tabs. Although if I choose something from the navigation drawer which loads up in a Fragment it doesn't show. Only the tabs are showing.
My plan is that one navigation drawer tab shall have 3 tabs inside and the rest just normal pages without tabs.
MainActivity.java
http://pastebin.com/TV7aWy9c
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">
<FrameLayout
android:id="#+id/fragment_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#000000" />
<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"
android:fitsSystemWindows="true"
tools:context="se.themeister.hello.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
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.TabLayout
android:id="#+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/toolbar"
android:background="?attr/colorPrimary"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
android:theme="#style/AppTheme.AppBarOverlay"/>
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="#009900"
android:layout_below="#id/tab_layout"/>
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main"/>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:src="#android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
I don't know if that's enough or if more information or code is needed
The problem with your code is that your ViewPager with TabLayout should be part of the Fragment, not part of the Activity. And NavigationDrawer should just replace one fragment by another in the viewport (i.e. in the container). Now, in your code, you're trying to somehow insert your new Fragment into the ViewPager.
Here's a very basic app with Navigation Drawer, one Fragment with Tabs and rest fragments without tabs, which you can use as an example:
Activity.xml:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="#+id/fragmentContainer"
android:layout_marginTop="?attr/actionBarSize"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.design.widget.CoordinatorLayout>
<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>
The code in the activity:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainer, new FragmentWithTabs()).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();
}
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.fragment_1) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainer, new FragmentWithTabs()).commit();
} else if (id == R.id.fragment_2) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainer, new FragmentWithoutTabs()).commit();
} else if (id == R.id.fragment_3) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainer, new Fragment()).commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
I.e., as you see, I'm replacing fragments in the FrameLayout with id fragmentContainer.
All logic related to the fragment with tabs, incapsulated in FragmentWithTabs:
public class FragmentWithTabs extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_with_tabs, container, false);
ViewPager viewPager = (ViewPager)rootView.findViewById(R.id.viewPager);
TabLayout tabLayout = (TabLayout)rootView.findViewById(R.id.tabLayout);
viewPager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
#Override
public Fragment getItem(int position) {
return new SubFragment(position == 0? Color.BLUE : position == 1? Color.WHITE : Color.RED);
}
#Override
public CharSequence getPageTitle(int position) {
return position+"";
}
#Override
public int getCount() {
return 3;
}
});
tabLayout.setupWithViewPager(viewPager);
return rootView;
}
}
I hope, it helps

Categories