4

I have an android app with preference enter image description here

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;
}
RoCkDevstack
  • 3,517
  • 7
  • 34
  • 56
  • Could you add the code for the `fragmentTransaction` to add `SettingsFragment`? – tynn Feb 08 '16 at 22:33
  • Possible duplicate of [Drawerlayout children views overlapping](http://stackoverflow.com/questions/16694657/drawerlayout-children-views-overlapping) – muratgu Feb 08 '16 at 22:39

3 Answers3

0

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"/>
Helix
  • 144
  • 6
  • When all else fails... Change the include within the app_bar_main.xml to have a margin equal to the size of the actionbar. `` – Helix Feb 09 '16 at 01:38
0

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.

Community
  • 1
  • 1
acrespo
  • 1,134
  • 1
  • 10
  • 33
0

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.

Harsh Modani
  • 205
  • 1
  • 11