Search code examples
androidandroid-fragmentssharedpreferencesnavigation-drawerandroidx

Android x PreferenceFragmentCompat overlap androidx.fragment.app.Fragment


I have setting fragment which works fine as logic but whenever i switched to PreferenceFragmentCompat it's layout background becomes transparent and last fragment and setting fragment layout overlap each other.

I have navigation drawer where i have setting fragment and it's only happens with PreferenceFragmentCompat other fragment in navigation drawer works fine.

I tried to set background color in PreferenceFragmentCompat XML like this android:background="?android:attr/colorBackground" but it still overlap

Main Activity Layout

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/Navigation_Drawer_Main">

    <!--The Main Layout For Content-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <!--Toolbar for actionbar   -->
        <include
            android:id="@+id/Navigation_Drawer_toolbar"
            layout="@layout/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <RelativeLayout
            android:id="@+id/Navigation_Main_Layout"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >


        </RelativeLayout>

    
    </LinearLayout>

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nvView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@android:color/white"
        app:menu="@menu/drawer_view" />


</androidx.drawerlayout.widget.DrawerLayout>

Main Activity switching between fragments

public void selectDrawerItem(MenuItem menuItem) {
        // Create a new fragment and specify the fragment to show based on nav item clicked
        Fragment fragment = null;


        switch (menuItem.getItemId()) {

            case R.id.HeaderImageView:
                fragment = new Edit();
                break;
            
            case R.id.noti_Fragment:
            
                fragment = new Notification();
                break;

            case R.id.add_Fragment:

                fragment = new AddMoney();

                break;

            case R.id.check_Fragment:
                fragment = new Check_Fragment();

                break;

            case R.id.setting_Fragment:
                Log.d(TAG, "Setting Fragment Pressed ");
                fragment = new Setting_NavigationDrawer();
                break;

            case R.id.help_Fragment:
                Log.d(TAG, "Help Fragment Pressed ");

                fragment = new Help_Fragment();

                break;

            case R.id.signOut_Fragment:
                new Session_Logout(this).execute();
                drawerLayout.closeDrawers();
                return;

        }


        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.Navigation_Main_Layout, fragment);
        fragmentTransaction.setCustomAnimations(R.animator.enter_anim, R.animator.exit_anim);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();

        // Highlight the selected item has been done by NavigationView

        menuItem.setChecked(true);

        // Set action bar title

        setTitle(menuItem.getTitle());

        // Close the navigation drawer

        drawerLayout.closeDrawers();

    }

Preference Fragment layout

<?xml version="1.0" encoding="utf-8"?>


<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="?android:attr/colorBackground"

    android:clickable="true"
    android:focusable="true">

    <PreferenceCategory
        android:iconSpaceReserved="false"
        android:key="app_setting"
        android:summary="General account setting for app"
        android:title="Account Setting">

        <SwitchPreferenceCompat
            android:checked="true"
            android:defaultValue="true"
            android:icon="@drawable/ic_location"
            android:key="locationButton"
            android:title="Location" />

        <Preference
            android:fragment="com.elaxer.Settings_Fragmets.BackUp_Fragment"
            android:icon="@drawable/ic_loop_black_24dp"
            android:title="Reset Account" />


    </PreferenceCategory>
    <PreferenceCategory android:title="Notifications">
        <SwitchPreferenceCompat
            android:defaultValue="true"
            android:icon="@drawable/ic_vibration_black_24dp"
            android:key="Notification_vibrate"
            android:summary="Vibrate when receive new notification"
            android:title="Vibrate" />
       

    </PreferenceCategory>


    <PreferenceCategory android:title="Privacy &amp; User Setting">
        <Preference
            android:icon="@drawable/ic_backup_black_24dp"
            android:key="bkup"
            android:summary="Backup account"
            android:title="Back up my account">

        </Preference>
        <PreferenceCategory
            android:icon="@drawable/ic_phonelink_lock_black_24dp"
            android:title="Privacy Control">

            <ListPreference
                android:defaultValue="1"
                android:dialogTitle="Post Privacy"
                android:entries="@array/pref_futurepost_entries"
                android:entryValues="@array/pref_futurepost_values"
                android:key="futurePot"
                android:summary="Select who can view your next post."
                android:title="Who Can See Your Future Post">

            </ListPreference>
        
            <ListPreference
                android:defaultValue="1"
                android:dialogTitle="Allow payment From"
                android:entries="@array/pref_futurepost_entries"
                android:entryValues="@array/pref_futurepost_values"
                android:key="Postpayment"
                android:title="Who Can payment On Your Proudct">

            </ListPreference>
            <CheckBoxPreference
                android:defaultValue="true"
                android:key="dob"
                android:summary="Check to show year of opening"
                android:title="Show Complete Data Of opening " />
            <CheckBoxPreference
                android:defaultValue="true"
                android:key="email"
                android:title="Show Email Publicly " />
            <CheckBoxPreference
                android:defaultValue="true"
                android:key="distance"
                android:title="Show My Approx pricing" />
            <CheckBoxPreference
                android:defaultValue="true"
                android:key="status"
                android:title="Show My Status " />
            <CheckBoxPreference
                android:defaultValue="true"
                android:key="directMessage"
                android:title="Allow Message From Customer" />
        </PreferenceCategory>
        

    </PreferenceCategory>




</PreferenceScreen>

Solution

  • In your settings fragment, add this:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        view.setBackgroundColor(getResources().getColor(android.R.color.white));
        return view;
    }