Android: Overlapping issue with multiple fragments under MainActivity containing Tabs - android

I am facing this problem where I have 3 Tabs inside MainActivity and each Tab contains a Fragment. When I click on an item under the drawer to start a new fragment, it gets overlapped on the MainActivity's Tabs view. Below is my code:
Fragment currentFragment;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
viewInflate = inflater.inflate(R.layout.fragment_city, container, false);
return viewInflate;
}
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
Intent i;
int id = item.getItemId();
if (id == R.id.nav_home) {
TabLayout tl = (TabLayout) findViewById(R.id.main_tabs);
tl.setVisibility(View.VISIBLE);
showFragment(new HomeFragment());
setTitle(R.string.app_name);
}
if (id == R.id.nav_about_us) {
TabLayout tl = (TabLayout) findViewById(R.id.main_tabs);
tl.setVisibility(View.GONE);
showFragment(new AboutUsFragment());
setTitle(R.string.about_us);
}
if (id == R.id.nav_favorites) {
TabLayout tl = (TabLayout) findViewById(R.id.main_tabs);
tl.setVisibility(View.GONE);
showFragment(new FavoritesFragment());
setTitle(R.string.favorites);
}
if (id == R.id.nav_settings) {
i = new Intent(this, SettingsActivity.class);
startActivity(i);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
public void showFragment(Fragment fragment) {
if (currentFragment != null && fragment.getClass().equals(currentFragment.getClass()))
return;
currentFragment = fragment;
FragmentManager fragmentManager = this.getSupportFragmentManager();
if (fragmentManager == null)
return;
FragmentTransaction ft = fragmentManager.beginTransaction();
if (ft == null)
return;
ft.replace(R.id.content_frame, fragment).commitAllowingStateLoss();
}
Below is the xml code of fragment_city.xml
<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"
android:background="#color/content_background"
tools:context="com.creationjunkies.fragments.CityFragment">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/swipe_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v7.widget.RecyclerView
android:id="#+id/cityBargainsRecyclerView"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
</FrameLayout>
And activity_main.xml code is:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
android:background="#color/content_background"
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:fitsSystemWindows="true"
tools:openDrawer="start"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.creationjunkies.cities.MainActivity">
<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">
<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"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay">
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="#+id/main_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="android.support.design.widget.AppBarLayout$ScrollingViewBehavior"
android:orientation="vertical">
<FrameLayout
android:id="#+id/frameTopAds"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</FrameLayout>
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<android.support.v4.view.ViewPager
android:id="#+id/tab_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="android.support.design.widget.AppBarLayout$ScrollingViewBehavior" />
</FrameLayout>
<FrameLayout
android:id="#+id/frameAds"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</FrameLayout>
</LinearLayout>
</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>
Your help is much appreciated!

Set fragment background as white.
It will solve the issue.
<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"
android:background="#ffffff"
tools:context="com.creationjunkies.fragments.CityFragment">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/swipe_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v7.widget.RecyclerView
android:id="#+id/cityBargainsRecyclerView"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>

Related

how to have a custom drawer layout with recycler view?

i want to create a custom drawer layout and have a recycler view inside it, how can i do this so all recycler view rows show inside the drawer layout and make it scrollable, i.e i want to show the whole FoodListFragment in the drawer.
my drawer layout
<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:context=".MainActivity"
tools:openDrawer="start">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
my recycler view which is inside a fragment
<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"
android:padding="8dp"
tools:context=".FoodListFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerview"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
You can achieve this by adding your fragment inside DrawerLayout. Check below:
First, Create a container for your fragment inside DrawerLayout
<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:context=".MainActivity"
tools:openDrawer="start">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
<FrameLayout
android:layout_gravity="start"
android:id="#+id/main_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<FrameLayout
android:id="#+id/drawer_fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true" />
</androidx.drawerlayout.widget.DrawerLayout>
Then, Attach your fragment in this container using FragmentTransaction
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, new FoodListFragment());
fragmentTransaction.commit();
add following code in your drawer layout
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:menu="#menu/navigation_menu"
android:layout_gravity="start"
app:headerLayout="#layout/nav_header"
android:id="#+id/nv">
</android.support.design.widget.NavigationView>
in your MainActivity
private NavigationView nv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nv = (NavigationView)findViewById(R.id.nv);
nv.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
int id = item.getItemId();
switch(id)
{
case R.id.account:
Toast.makeText(MainActivity.this, "My Account",Toast.LENGTH_SHORT).show();break;
case R.id.settings:
Toast.makeText(MainActivity.this, "Settings",Toast.LENGTH_SHORT).show();break;
case R.id.mycart:
Toast.makeText(MainActivity.this, "My Cart",Toast.LENGTH_SHORT).show();break;
default:
return true;
}
return true;
}
});

Fragment hides drawer menu

The onViewCreate method of Chapter1Fragment class (extending from android.app.Fragment) is returning an object of a class which is subclass of android.opengl.GLSurfaceView
This fragment appears fine on the activity. But the problem is that the drawer navigation view does hides behind this fragment. Please suggest how the drawer menu can be fixed?
here is the code of onViewCreate
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container,
Bundle savedInstanceState) {
int chapterNumber = 1;
getActivity().setTitle(R.string.txt_chap1);
mPageFlipView = new PageFlipView(getActivity(), chapterNumber);
mGestureDetector = new GestureDetector(getActivity(), this);
mPageFlipView.setOnTouchListener(new View.OnTouchListener(){
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
mPageFlipView.onFingerUp(motionEvent.getX(), motionEvent.getY());
return true;
}
return mGestureDetector.onTouchEvent(motionEvent);
}
});
return mPageFlipView;
}
There is another Fragment HomeFragment which invokes the following code to make Chapter1Fragment appear
Chapter1Fragment fragment = new Chapter1Fragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frameLayout, fragment);
fragmentTransaction.commit();
Here is the xml file that is passed to setContentView() of MainActivity's onCreate()
<?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:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
Here is the app_bar_main.xml from layout directory
<?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="viento.com.bookapp.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
</android.support.design.widget.CoordinatorLayout>
Here is the content_main.xml from layout directory
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="android.support.design.widget.AppBarLayout$ScrollingViewBehavior"
/>

How to code ViewPagers inside Items of BottomNavigationMenu as we have in Google+, Reddit, Quora app?

I am trying to achieve a layout like this by using Fragments as Items of BottomNavigationMenu and inside those Fragment, I am using ViewPager.
But I am getting layout errors like this.
This is the code
getSupportFragmentManager().beginTransaction().add(R.id.fragment_frame, searchPropertyFragment).commit();
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
int id = item.getItemId();
switch (id){
case R.id.bottom_menu_properties:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_frame, new SearchPropertyFragment).commit();
break;
case R.id.bottom_menu_chat:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_frame, new ChatFragment).commit();
break;
case R.id.bottom_menu_profile:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_frame, new ProfileFragment).commit();
break;
case R.id.bottom_menu_notifications:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_frame, new NotificationFragment).commit();
break;
}
return true;
}
});
This is the viewPager code inside SearchPropertyFragment
titlesList.clear();
fragmentsList.clear();
titlesList.add("Featured");
titlesList.add("Yours");
titlesList.add("Following");
fragmentsList.add(new FeaturedFragment());
fragmentsList.add(new YoursFragment());
fragmentsList.add(new FollowingFragment());
pagerAdapter = new PagerAdapter(getActivity().getSupportFragmentManager(),
titlesList, fragmentsList);
viewpager.setAdapter(pagerAdapter);
tabs.setupWithViewPager(viewpager);
What is the proper approach to get this layout.
Use FrameLayout and BottomNavigationView inside DrawerLayout where FrameLayout is the container for the fragment which contains the ViewPager.
This is the layout of the DrawerLayout
<?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/main_content"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/fragment_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--This is where the fragment will be placed-->
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#color/navigationItemBackground"
app:itemIconTint="#color/navigationItemIcon"
app:itemTextColor="#color/navigationItemtext"
app:menu="#menu/potential_tenant_menu"
android:layout_alignParentStart="true" />
</android.support.design.widget.CoordinatorLayout>
</android.support.v4.widget.DrawerLayout>
This is the fragment which will be placed in the FrameLayout
<?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:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<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/ThemeOverlay.AppCompat.Light"
app:layout_scrollFlags="scroll|enterAlways"
app:layout_collapseMode="pin"/>
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>

Navigation Drawer does not fit underneath the status bar (android studio) [duplicate]

This question already has answers here:
How do I use DrawerLayout to display over the ActionBar/Toolbar and under the status bar?
(10 answers)
Closed 6 years ago.
I am working on an app in android studio. I'm trying to create a navigation drawer that fits right underneath the status bar. For some reason the navigation bar shows up underneath the app bar instead kind of like in this picture:
Navdrawer1
I would like it to look like this:
Navdrawer2
Here is my xml code:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical"
>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff4000"
android:id="#+id/toolbar"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:title="Home"
/>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent"
android:layout_gravity="start"
android:layout_width="match_parent"
android:id="#+id/drawerLayout"
>
<FrameLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/containerView">
</FrameLayout>
<android.support.design.widget.NavigationView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:layout_gravity="start"
android:background="#FFFFFF"
app:headerLayout="#layout/navigation_drawer_header"
android:id="#+id/shitstuff"
app:itemTextColor="#color/black"
app:menu="#menu/drawermenu"
android:layout_marginTop="-24dp"
/>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
Any help would be great! Thanks!
Here you need toolbar :
<?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"//fit to top status bar
tools:openDrawer="start">
<include
layout="#layout/app_bar_home_actvity"
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.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
app_bar_home_actvity.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" //fit to top status bar
tools:context=".activity.HomeActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_home_actvity" />
</android.support.design.widget.CoordinatorLayout>
You must include your toolbar within the "NavigationView"
Your code would look something like this:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
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
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">
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff4000"
android:id="#+id/toolbar"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:title="Home"
/>
</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="left"
android:fitsSystemWindows="true">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
activity_main_nav.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:context=".MainActivity"
android:elevation="7dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="#layout/toolbar" />
<!-- Let's add fragment -->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/frame"/>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigation"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="#menu/activity_home_drawer"
app:headerLayout="#layout/header"/>
</android.support.v4.widget.DrawerLayout>
header.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:background="#drawable/back_header"
android:layout_height="178dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="Header"
android:textColor="#android:color/white"/>
</LinearLayout>
Menu Folder activity_home_drawer.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:id="#+id/grp1" android:checkableBehavior="single">
<item
android:id="#+id/nav_first"
android:icon="#drawable/first"
android:title="First" />
</group>
// if you want to underline than use group
<item
android:id="#+id/nav_second"
android:icon="#drawable/second"
android:title="second" />
</menu>
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_nav);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(R.id.frame, new YourFragment()).commit();
}
setNavigationDrawer();
setToolBar();
}
private void setToolBar() {
final Toolbar tb = (Toolbar) findViewById(R.id.toolbar1);
setSupportActionBar(tb);
ActionBar ab = getSupportActionBar();
ab.setHomeAsUpIndicator(R.drawable.ic_menu_white_24dp);
ab.setDisplayHomeAsUpEnabled(true);
ab.setDisplayShowHomeEnabled(true);
final android.app.FragmentManager fm = getFragmentManager();
fm.addOnBackStackChangedListener(new android.app.FragmentManager.OnBackStackChangedListener() {
#Override
public void onBackStackChanged() {
if (getSupportFragmentManager().getBackStackEntryCount() ==0) {
dLayout.closeDrawers();
finish();
}
else
{
dLayout.closeDrawers();
}
}
});
}
private void setNavigationDrawer() {
dLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
NavigationView navView = (NavigationView) findViewById(R.id.navigation);
Menu m = navView.getMenu();
for (int i=0;i<m.size();i++) {
MenuItem mi = m.getItem(i);
//for aapplying a font to subMenu ...
SubMenu subMenu = mi.getSubMenu();
if (subMenu!=null && subMenu.size() >0 ) {
for (int j=0; j <subMenu.size();j++) {
MenuItem subMenuItem = subMenu.getItem(j);
}
}
}
navView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
Fragment frag = null;
int itemId = menuItem.getItemId();
if (itemId == R.id.first) {
// frag = new First();
frag = new SearchJobActivity();
} else if (itemId == R.id.second) {
frag = new second();
}
if (frag != null) {
openFragmentNew(frag);
dLayout.closeDrawers();
return true;
}
return false;
}
});
}
public void openFragmentNew(Fragment fragment) {
String backStateName = fragment.getClass().getName();
FragmentManager manager = getSupportFragmentManager();
//fragment not in back stack, create it.
FragmentTransaction ft = manager.beginTransaction();
if(!fragments.contains(backStateName)) {
// ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
// ft.setCustomAnimations(R.anim.fade_in, R.anim.fade_out);
ft.replace(R.id.frame, fragment);
ft.addToBackStack(backStateName);
ft.commit();
System.out.println("backStateName" + fragments);
}
else
{
// ft.remove(fragment);
ft.replace(R.id.frame, fragment);
ft.commit();
// manager.popBackStack();
}
}
#Override
public void onBackPressed() {
if (dLayout.isDrawerOpen(GravityCompat.START)) {
dLayout.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
change accoding to you in mainactivity

Android Preferences Overlaps Toolbar

I have an android app with preference
As you can see the preferences overlaps the toolbar.
This is my main fragment 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" />
<FrameLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<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>
which include a new 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=".MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include
layout="#layout/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/toolbar" />
<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_partial_secure" />
</android.support.design.widget.CoordinatorLayout>
and finally the content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.gilca.ebspma.MainActivity">
<TextView
android:id="#+id/location"
android:paddingTop="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
My xml preferences are this one
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<PreferenceCategory
android:summary="Username and password information"
android:title="Informações de Login">
<EditTextPreference
android:key="username"
android:summary="Introduzir username"
android:title="Username" />
<EditTextPreference
android:key="password"
android:summary="Introduzir password"
android:title="Password" />
</PreferenceCategory>
<PreferenceCategory
android:summary="sessao"
android:title="Definições">
<Preference
android:key="#string/myCoolButton"
android:summary="Mude aqui"
android:title="Mudar a password na BD" />
<CheckBoxPreference
android:key="checkBox"
android:summary="On/Off"
android:title="Manter Sessão" />
</PreferenceCategory>
</PreferenceScreen>
And this xml is called from SettingsFragment.java
public class SettingsFragment extends PreferenceFragmentCompat {
SharedPreferences sharedPreferences;
#Override
public void onCreatePreferences(Bundle bundle, String s) {
addPreferencesFromResource(R.xml.prefs);
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
}
}
So...why do i have this problem and how can i fix that?
UPDATE
The code asked
public boolean onNavigationItemSelected(MenuItem item) {
Fragment fragment = null;
Class fragmentClass = null;
int id = item.getItemId();
if (id == R.id.nav_home) {
fragmentClass = HomeFragment.class;
} else if (id == R.id.nav_req) {
} else if (id == R.id.nav_ver) {
} else if (id == R.id.nav_atividades) {
fragmentClass = AtividadesFragment.class;
} else if (id == R.id.nav_training) {
fragmentClass = TrainingFragment.class;
} else if (id == R.id.nav_settings) {
fragmentClass = SettingsFragment.class;
}
else if (id == R.id.nav_about) {
}
else if (id == R.id.nav_sair) {
session.logOut();
System.exit(0);
}
try{
assert fragmentClass != null;
fragment = (Fragment) fragmentClass.newInstance();
}catch (Exception e) {
e.printStackTrace();
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content, fragment).commit();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
Try to align your "content" frame layout in the main.xml to be below the "app_bar".
<include
android:id="#+id/app_bar"
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<FrameLayout
android:id="#+id/content"
android:layout_below="#id/app_bar"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
I'm a little confused about your layout hierarchy, particularly about
<FrameLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
and
<include
layout="#layout/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/toolbar" />
which could ultimately be used for the same function, which is be the main fragment container.
However, I know this issue could be a real pain in the ass and I have recently struggled with this (quite a lot actually) and finally found the answer. At least in my case, it was solved by just adding a LinearLayout as a parent of both AppBarLayout and your fragment container (in this case I don't know exactly which would be, the FrameLayout or your content_main layout).
If my explanation isn't clear enough please refer to Fragment overlaps my AppCompat toolbar.
Instead of doing something so complex, to just house an AppBar and FloatingActionButton, you can do the following:
In your layout file for the Settings Activity (say, activity_settings.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SettingsActivity"
android:orientation="vertical"
android:nestedScrollingEnabled="true"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:liftOnScroll="true">
<androidx.appcompat.widget.Toolbar
...
/>
</com.google.android.material.appbar.AppBarLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/fragment_container"/>
<FloatingActionButton
...
/>
</LinearLayout>
And any regular Preference Fragment will do.

Categories

Resources