Opening a new fragment on navigation bar - java

I have a navigation bar with fragment container inside. I have added an imageview in it.My problem is to replace fragment container with a new fragment when I choose an item in the navigation bar. When i choose an item on the navigation bar, it opens but fragment container still appear behind.
I would like to open an item on the navigation bar and the fragment container is fully overwritten by the new fragmement.
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:fitsSystemWindows="true"
tools:openDrawer="start"
tools:context=".MainActivity">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="50dp"
android:layout_height="?attr/actionBarSize"
android:background="#00000000"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
android:fitsSystemWindows="true"
/>
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:id="#+id/imageView"
android:layout_marginLeft="80dp"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_marginBottom="17dp"
app:layout_constraintBottom_toTopOf="#+id/textView3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/logo" />
</FrameLayout>
</FrameLayout>
<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"
app:headerLayout="#layout/nav_header"
app:menu="#menu/nav_header_menu"
app:defaultNavHost="true"/>
</androidx.drawerlayout.widget.DrawerLayout>
public class MainActivity extends AppCompatActivity {
private TabLayout tabLayout;
private ViewPager2 viewPager2;
private MyFragment adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabLayout = findViewById(R.id.tabLayout);
viewPager2 = findViewById(R.id.viewPager2);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer=(DrawerLayout) findViewById(R.id.drawer_layout);
NavigationView navigationView=(NavigationView)findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(item -> {
item.setChecked(true);
switch (item.getItemId())
{
case R.id.nav_materials:
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container,new suppliers()).commit();
break;
case R.id.nav_interiorDesign:
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container,new interiorDesign()).commit();
break;
case R.id.nav_Architects:
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container,new architect()).commit();
break;
case R.id.nav_contractors:
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container,new contractorList()).commit();
break;
case R.id.nav_Permit:
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container,new buildPermit()).commit();
break;
case R.id.nav_kanban:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new kanbanManagement()).commit();
break;
case R.id.nav_Estimator:
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container,new costEstimator()).commit();
break;
case R.id.navigation_dashboard:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new dashboard()).commit();
break;
case R.id.nav_funds:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new finance()).commit();
break;
case R.id.nav_sign_out:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new signOut()).commit();
break;
//add other fragments#
}
drawer.closeDrawer(GravityCompat.START);
return true;
});
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this,drawer, toolbar,
R.string.navigation_drawer_open, R.string.navigation_drawer_close);
//changing the color of hamburger icon
toggle.getDrawerArrowDrawable().setColor(getResources().getColor(R.color.black));
drawer.addDrawerListener(toggle);
toggle.syncState();
}

Related

Contents inside activity not working/not interactive after adding navigation drawer

enter image description hereI followed a tutorial on how to add a side navigation drawer but after adding it properly i can`t interact no more with the textedits/maps etc in my fragments, it´s not registering touch for that layout only for the drawer buttons itself.
enter image description here
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
tools:context=".MainActivity">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:itemBackground="#drawable/drawable_selector"
app:itemIconTint="#drawable/color_selector"
app:itemTextColor="#drawable/color_selector"
app:menu="#menu/bottom_nav_menu"/>
<FrameLayout
android:id="#+id/frameLayout"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="#+id/bottomNavigationView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</FrameLayout>
<androidx.drawerlayout.widget.DrawerLayout
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">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/purple_200"
android:id="#+id/toolbar"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
android:elevation="4dp"/>
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:theme="#style/NavigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:id="#+id/nav_view"
app:headerLayout="#layout/nav_header"
app:menu="#menu/drawer_menu"
android:background = "#color/purple_700"
app:itemTextColor="#color/white"
app:itemIconTint="#color/white"
/>
</androidx.drawerlayout.widget.DrawerLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Main activity:
public class MainActivity extends AppCompatActivity {
ActivityMainBinding binding;
private DrawerLayout drawer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
replaceFragment(new HomeFragment());
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = 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();
binding.bottomNavigationView.setOnItemSelectedListener(item -> {
switch (item.getItemId()){
case R.id.home:
replaceFragment(new HomeFragment());
break;
case R.id.dashboard:
replaceFragment(new DashboardFragment());
break;
case R.id.pregnant:
replaceFragment(new Pregnant());
break;
}
return true;
});
}
public void onBackPressed(){
if(drawer.isDrawerOpen(GravityCompat.START)){
drawer.closeDrawer(GravityCompat.START);
}else {
super.onBackPressed();
}
}
private void replaceFragment(Fragment fragment){
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frameLayout, fragment);
fragmentTransaction.commit();
}
}
I also have and xml for the nav header and the drawer itself with some textview, items etcenter image description here
If i bringToFront() the layout it works but the drawer dissapears

fragments overlaid in bottom navigation and drawer layout using viewpager 2 in android app

I'm almost new to programming in Android and there are still things that I don't handle very well. Now I'm doing a project in Android using java that has me half frustrated since I've been trying to find and fix a problem for WEEKS but I still can't identify what it is. The app consists of a view that contains a drawer layout with a series of "options", it also contains a viewpager2 and a bottom navigation with 2 other "options." The error appears when I press the bottom navigation tabs since the two fragments they overlap each other, the same happens when I select an option from the navigation drawer, the fragments are superimposed, then I leave you part of my code so that I hope you can help me, thank you very much for taking the time to help me.
main:
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
DrawerLayout drawerLayout;
ActionBarDrawerToggle actionBarDrawerToggle;
Toolbar toolbar;
NavigationView navigationView;
//variables para cargar el fragment
FragmentManager fragmentManager;
FragmentTransaction fragmentTransaction;
String personName;
ViewPager2 viewPager2;
BottomNavigationView bottomNavigationView;
ViewPagerAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar=findViewById(R.id.toolbar);
setSupportActionBar(toolbar);//se pasa el toolbar
drawerLayout=findViewById(R.id.drawer);
navigationView=findViewById(R.id.navigationView);
navigationView.setNavigationItemSelectedListener(this);
actionBarDrawerToggle=new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.open,R.string.close);
drawerLayout.addDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.setDrawerIndicatorEnabled(true);
actionBarDrawerToggle.syncState();
initView();
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
if (item.getItemId() == R.id.user){
FragmentManager fragmentManager;
FragmentTransaction fragmentTransaction;
fragmentManager=getSupportFragmentManager();
fragmentTransaction=fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, new perfil_user());
fragmentTransaction.commit();
}
else if (item.getItemId() == R.id.exit){
finish();
}
//aquí van las otros fragment
drawerLayout.closeDrawer(GravityCompat.START);
return false;
}
private void initView() {
viewPager2 = findViewById(R.id.vp_horizontal_ntb);
viewPager2.setOffscreenPageLimit(100);
bottomNavigationView = findViewById(R.id.navigation);
adapter = new ViewPagerAdapter(this);
viewPager2.setAdapter(adapter);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment fragment=null;
switch (item.getItemId()){
case R.id.perfil:
fragment=new perfil_user();
break;
case R.id.generar_debate:
fragment=new genera_debate();
break;
}
if (fragment != null) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.container, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
return true;
}
});
}
}
files xml:
content main.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"
>
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
drawer toolbar:
<?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">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#067B30"
android:theme="#style/ThemeOverlay.AppCompat.Dark"
>
</androidx.appcompat.widget.Toolbar>
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:elevation="0dp"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/navigation"
app:menu="#menu/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="?android:attr/windowBackground"
android:foreground="?attr/selectableItemBackground" />
<androidx.viewpager2.widget.ViewPager2
android:id="#+id/vp_horizontal_ntb"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<include
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="#layout/drawer_toolbar"
>
</include>
<include
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="#layout/content_main"
>
</include>
<com.google.android.material.navigation.NavigationView
android:id="#+id/navigationView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:menu="#menu/drawer_menu"
app:headerLayout="#layout/drawer_header"
android:layout_gravity="start"
android:fitsSystemWindows="true"
>
</com.google.android.material.navigation.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>

i have bottom navigation and navigation drawer and i set home screen fragment as default

i have bottom navigation and navigation drawer and i set home screen fragment as default
now problem is when i run my app on Emulator hamburger icon and bottom navigation is showing and home Fragment by default
when i click to hamburger icon its not working
when i remove default home fragment then navigation drawer is working
How to solve this Problem so both navigationDrawer and bottom navigation will work
HomeScreenActivity.java
public class HomeScreenActivity extends AppCompatActivity {
DrawerLayout mDrawerLayout;
ActionBarDrawerToggle mToggle;
BottomNavigationView bottomNavigationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_screen);
bottomNavigationView = findViewById(R.id.homeScreenBottomNavigation);
bottomNavigationView.setOnNavigationItemSelectedListener(bottomNavListener);
// getSupportFragmentManager().beginTransaction().replace(R.id.homeScreenframeLayout, new HomeFragment()).commit();
mDrawerLayout = findViewById(R.id.HomeScreenDrawerLayout);
mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close);
mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
private BottomNavigationView.OnNavigationItemSelectedListener bottomNavListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
Fragment selectedFragment = null;
switch (menuItem.getItemId()) {
case R.id.bottomNavHome:
selectedFragment = new HomeFragment();
break;
case R.id.bottomNavAdd:
selectedFragment = new AddFragment();
break;
case R.id.bottomNavSearch:
selectedFragment = new SearchFragment();
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.homeScreenframeLayout, selectedFragment).commit();
return true;
}
};
}
ActivityHomeScreen.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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HomeScreenActivity"
>
<android.support.v4.widget.DrawerLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/HomeScreenDrawerLayout"
>
<android.support.design.widget.NavigationView
app:headerLayout="#layout/left_navigation_header"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/navigationView"
app:menu="#menu/drawer_layout"
android:layout_gravity="start"
android:background="#ffff"
>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/homeScreenframeLayout"
>
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="#+id/homeScreenBottomNavigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
app:menu="#menu/bottom_navigation"
android:background="?android:attr/windowBackground"
>
</android.support.design.widget.BottomNavigationView>
</RelativeLayout>
The issue is with your ActivityHomeScreen.xml. You have to make DrawerLayout as the parent view. Inside that you have to place BottomNavigationDrawer and remaining code.
And You havent created custom appbar for naviagtion drawer(it now comes with navigation drawer template)
Your ActivityHomeScreen.xml should look like this:
ActivityHomeScree.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>
Now create a layout file app_bar_main.xml. Inside it create a toolbar and place your frameLayout here.
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"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<!--
Place your frameLayout here
-->
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="bottom"
app:menu="#menu/navigation" />
</android.support.design.widget.CoordinatorLayout>
Now both Navdrawer and BottomNavigationDrawer should work.

Using data binding inside of a Drawer Layout, where the drawer holds a fragment

I have a Navigation Drawer Activity, however, I'm using a Fragment inside of the drawer. I can't get both data binding and the fragment working together. With the example code the data binding doesn't work. If I change binding = ContentNewSessionBinding.inflate(getLayoutInflater()) to binding = DataBindingUtil.setContentView(this, R.layout.content_new_session_); I get an error that a view cannot be found the fragment.
activity_new_session.xml
<?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">
<android.support.v4.widget.DrawerLayout
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_new_session_"
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">
<FrameLayout
android:id="#+id/new_session_draw_frame"
android:name="uk.co.alexanderjs.catchtrack2.SpeciesManager_Fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
</layout>
content_new_session.xml
<?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">
<data>
<variable
name="WeatherModel"
type="uk.co.alexanderjs.models.WeatherModel"/>
</data>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/CL_content_new_session"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".NewSession_Activity"
tools:showIn="#layout/app_bar_new_session_">
<android.support.constraint.ConstraintLayout
android:id="#+id/CL_info_tray"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<android.support.v7.widget.AppCompatImageView
android:id="#+id/IV_location_type_info_tray"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginEnd="4dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#id/IV_watch_info_tray"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_cell_tower" />
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
NewSession_Activity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_session)
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final DrawerLayout drawer = 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();
toggle.setDrawerIndicatorEnabled(false);
Class fragmentClass = SpeciesManager_Fragment.class;
Fragment fragment = null;
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e ) {
e.printStackTrace();
}
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.new_session_draw_frame, fragment);
fragmentTransaction.commit();
}
ContentNewSessionBinding binding = ContentNewSessionBinding.inflate(getLayoutInflater());
binding.setWeatherModel(MainActivity.weatherModel);
}
You should try to move that FrameLayout in your activity_new_session.xml outside the NavigationView, between the toolbar layout and the NavigationView is the place for the fragment container and then you can add to your NavigationView:
app:headerLayout="YourHeaderLayout"
in case you want to have a header in your DrawerLayout
app:menu="YourMenuXml"
where you have the items that will show on your DrawerLayout

ViewPager in navigation drawer - Please give some advice

So overall idea is to in first (main page) have swiping gallery and I did it BUT when I choose options from navigation menu I'm having pager layout witch is my navigation drawer layout and its blank cuz just contain android.support.v4.view.ViewPager
CONTENT_MAIN.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
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"
android:id="#+id/pager"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.marcj.justtest.drawer"
>
</android.support.v4.view.ViewPager>
But now I want to go to my navigation drawer choose on of the options there and have OTHER swiping gallery.. but I don't know How to do it because when I choose option for example SOLD I'm having blank layout.
enter image description here
drawer.java file:
package com.example.marcj.justtest;
import java.math.BigDecimal;
public class drawer extends AppCompatActivity implements
NavigationView.OnNavigationItemSelectedListener {
ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drawer);
viewPager = (ViewPager) findViewById(R.id.pager);
PageAdapter padapter = new PageAdapter(getSupportFragmentManager());
viewPager.setAdapter(padapter);
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.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView)
findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
View headerView = navigationView.getHeaderView(0);
String username = getIntent().getStringExtra("username");
TextView text2 = (TextView) headerView.findViewById(R.id.TVusername);
text2.setText(username);
}
#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.navigation, 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;
} else if (id == R.id.action_contact) {
Toast.makeText(this, "Contact Form", Toast.LENGTH_SHORT).show();
Intent i = new Intent(drawer.this, AcContact.class);
startActivity(i);
contact contactForm = new contact();
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.pager,
contactForm, contactForm.getTag()).commit();
} else if (id == R.id.nav_clicked) {
}
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_homepage) {
Intent i= new Intent (drawer.this, drawer.class );
startActivity(i);
} else if (id == R.id.nav_clicked) {
Toast.makeText(this, "Clicked Items", Toast.LENGTH_SHORT).show();
clicked clickedItems = new clicked();
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.pager,
clickedItems, clickedItems.getTag()).commit();
} else if (id == R.id.nav_won) {
Toast.makeText(this, "Won Items", Toast.LENGTH_SHORT).show();
won wonItems = new won();
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.pager,
wonItems, wonItems.getTag()).commit();
} else if (id == R.id.nav_selling) {
Toast.makeText(this, "Item that you Selling",
Toast.LENGTH_SHORT).show();
selling sellingItems = new selling();
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.pager,
sellingItems, sellingItems.getTag()).commit();
} else if (id == R.id.nav_sold) {
Toast.makeText(this, "Sold Items", Toast.LENGTH_SHORT).show();
sold soldItems = new sold();
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.pager,
soldItems, soldItems.getTag()).commit();
} else if (id == R.id.nav_topup) {
Toast.makeText(this, "Top-Up Clicks", Toast.LENGTH_SHORT).show();
topup topupItems = new topup();
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.pager,
topupItems, topupItems.getTag()).commit();
} else if (id == R.id.nav_share) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
PageAdapter.java
package com.example.marcj.justtest;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class PageAdapter extends FragmentPagerAdapter {
public PageAdapter (FragmentManager fm){
super(fm);
}
public Fragment getItem (int arg0){
switch (arg0){
case 0:
return new FragmentOne();
case 1:
return new FragmentTwo();
case 2:
return new FragmentThree();
default:
break;
}
return null;
}
public int getCount(){
return 3;
}
}
Activity_drawer.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.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="wrap_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:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_drawer_menu" />
</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=".drawer">
<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_main" />
</android.support.design.widget.CoordinatorLayout>
activity_drawer.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.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="wrap_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:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_drawer_menu" />
</android.support.v4.widget.DrawerLayout>
activity_drawer file:
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.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="wrap_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:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_drawer_menu" />
content_main
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/pager"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.marcj.justtest.drawer"
>

Categories

Resources