PreferenceFragment with transparent background?
Asked Answered
M

6

7

I have created a PreferenceFragment with two categories and one checkbox, however, when I display it in my app, the background appears to be transparent.

I can see the main activities, fields and the PreferenceFragment ones are laid over top of the ones from the main activity...what is the solution to this?

In my main activity I am doing this to open the PreferenceFragment when the settings button is selected:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    //handle presses on action bar items
    switch (item.getItemId()) {
        case R.id.action_settings:
            getFragmentManager().beginTransaction().replace(android.R.id.content,
                    new SettingsFragment()).commit();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

My PreferenceFragment code looks like this: public class SettingsFragment extends PreferenceFragment {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //load the preferences from an XML resource
    addPreferencesFromResource(R.xml.preferences);
  }
}

My preferences.xml looks like this:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_height="wrap_content"
  android:layout_width="wrap_content"
  android:layout="@layout/fragment_settings">
  <PreferenceCategory
    android:title="@string/server_prefs_title"
    android:key="pref_key_server_settings" >
  <CheckBoxPreference
    android:key="pref_server_url"
    android:title="@string/pref_server_url"
    android:summary="@string/pref_server_url_summ"
    android:defaultValue="true" />
  </PreferenceCategory>
  <PreferenceCategory
    android:title="@string/company_prefs_title"
    android:key="pref_key_company_settings" >
  </PreferenceCategory>
</PreferenceScreen>     

I tried what another answer on Stack Overflow suggested and added this to my Preference Fragment, however, although it blocks the fields visible from the Main Activity, it just appears to be a mask, because the fragment fields are also being affected by the color that I set:

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

    return view;
}

Thanks for the help!!!

EDIT: I think I answered my own question while typing this out...I added:

addPreferencesFromResource(R.xml.preferences);

After doing:

 View view = super.onCreateView(inflater, container, savedInstanceState);
 view.setBackgroundColor(getResources().getColor(android.R.color.black));

Is that the proper way to do solve this?

Mandrill answered 28/4, 2014 at 21:25 Comment(0)
N
10

Add this in your Fragment (SettingsFragment) that extends PreferenceFragment:

@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;
}
Nice answered 12/6, 2015 at 21:43 Comment(1)
White is not transparent..Fumikofumitory
E
1

Just don't use both FragmentManager and SupportFragmentManager ... use one of the two when your are replacing fragments. Hope it will work

FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

If you are using PreferenceFragment then you'll have to use getFramgentManager() instead of getSupportFragmentManager();.

Erhard answered 6/6, 2018 at 5:32 Comment(1)
or just simply use PreferenceActivityErhard
A
0

Only way I could avoid those issues was to use a preferenceActivity class: How do you create Preference Activity and Preference Fragment on Android? by WannaGetHigh

Arlenarlena answered 18/3, 2015 at 8:9 Comment(0)
K
0

I'll add my two cents since I had a similar issue with PreferenceFragmentCompat and this is the top result from google, but this didn't quite answer my question.

I had the same issue where using PreferenceFragmentCompat would work, but the settings background was transparent and you could still see views in the underlying activty - changing .add() to .replace() in the getSupportFragmentManger yielded the same overlay.

The solution of changing the background color worked. However, for my build the getColor() method was deprecated. I instead use the following code.

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    getListView().setBackgroundColor(Color.WHITE);
}

This solved the transparency issue except for one button, which was still clickable. For the sake of brevity, the issue was I was trying to replace the layout that contained the activity views.

What ended up working was creating an empty layout at the highest order and using .replace() on that layout. The buttons are now covered and no longer clickable.

XML where button was still visible above preferences fragment.

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

<android.support.constraint.ConstraintLayout
    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"
    tools:context=".MainActivity">

     <!--I was replacing this layout that had all the activity views-->
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintEnd_toStartOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintTop_toBottomOf="parent">

        <TextView
            android:text="Z"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/earth_acc_z"
            app:layout_constraintTop_toBottomOf="@+id/earth_acc_y"
            app:layout_constraintStart_toStartOf="@+id/earth_acc_y"
            app:layout_constraintEnd_toEndOf="@+id/earth_acc_y"
            app:layout_constraintHorizontal_bias="1.0"/>
        <Button
            android:text="Button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/button" android:layout_marginTop="8dp"
            app:layout_constraintTop_toBottomOf="@+id/toggleRecording"
            app:layout_constraintStart_toStartOf="@+id/toggleRecording" 
            android:layout_marginStart="8dp"/>

    </FrameLayout>
</android.support.constraint.ConstraintLayout>

XML of working example with new empty constraint.

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

<android.support.constraint.ConstraintLayout
    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"
    tools:context=".MainActivity">

    <!--FrameLayout with two nested ConstraintLayouts-->
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintEnd_toStartOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintTop_toBottomOf="parent">

        <!--ConstraintLayout with acitivty views-->
        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent" android:id="@+id/frameLayout">

            <TextView
                android:text="Z"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/earth_acc_z"
                app:layout_constraintTop_toBottomOf="@+id/earth_acc_y"
                app:layout_constraintStart_toStartOf="@+id/earth_acc_y"
                app:layout_constraintEnd_toEndOf="@+id/earth_acc_y"
                app:layout_constraintHorizontal_bias="1.0"/>
            <Button
                android:text="Button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/button" android:layout_marginTop="8dp"
                app:layout_constraintTop_toBottomOf="@+id/toggleRecording"
                app:layout_constraintStart_toStartOf="@+id/toggleRecording" 
                android:layout_marginStart="8dp"/>

        </android.support.constraint.ConstraintLayout>

        <!--Preference fragment should replace this empty layout-->
        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/preferenceFragment"
            app:layout_constraintTop_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent">
        </android.support.constraint.ConstraintLayout>
    </FrameLayout>
</android.support.constraint.ConstraintLayout>
Kerguelen answered 22/2, 2019 at 16:3 Comment(0)
A
-1

When using Kotlin add the following code to your SettingsFragment:

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    val view = super.onCreateView(inflater, container, savedInstanceState)
    view?.setBackgroundColor(Color.WHITE)
    return view
}
Antoniettaantonin answered 14/1, 2020 at 10:43 Comment(0)
B
-3

Try changing all your Fragment imports to android.app.Fragment instead of android.support.v4.app.Fragment. Also, make sure that you are accessing your fragmentmanager with getFragmentManager() not getSupportFramentManager().

Reference: http://ausmarton.blogspot.com.au/2014/01/preferencefragment-shows-up-with.html

Bisque answered 12/8, 2014 at 13:3 Comment(2)
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes.Gillman
Yea you're right. Guess I was just being lazy. Fleshed out the answer a bit more.Bisque

© 2022 - 2024 — McMap. All rights reserved.