PreferenceFragment is shown transparently
Asked Answered
I

11

12

I'm trying to show a PreferenceFragment after I select the Preferences option in my ActionBar. However, after replacing current content with the PreferenceFragment you can see the old content below it. As in, you can see straight through the preferences.

Am I missing something here? I used an example from a book I own, which didn't use any layout files for the preferences. Do you need those?

Used code:

Actionbar menu

private boolean MenuChoice(MenuItem item) {
        switch (item.getItemId()) {
        case 0:
            FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction fragmentTransaction =
            fragmentManager.beginTransaction();
            ReaderPreferences prefs = new ReaderPreferences();
            fragmentTransaction.replace(android.R.id.content, prefs);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
            return true;

        }
        return false;
    }

PreferenceReader

public class ReaderPreferences extends PreferenceFragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // --load the preferences from an XML file---
        addPreferencesFromResource(R.xml.preference);
    }
  }

Actual result:

enter image description here

As you can see, you look straight through my preferences. What did I do wrong?

Inflammatory answered 2/12, 2011 at 21:28 Comment(1)
I have the same problem. Is there no solution to get it work with PreferenceFragments instead of PreferenceActivity?Bellini
I
0

Eventually fixed it with a very easy fix.

I simply called the PreferenceFragment in a new Intent, and it worked perfectly.

For anyone with the same problem:

Prefs.java

public class ReaderPreferences extends PreferenceActivity {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            // --load the preferences from an XML file---
            addPreferencesFromResource(R.xml.preference);
        }

In my main screen when pressing a button:

Intent i = new Intent(this, Prefs.class);
            startActivity(i);

That's all. After setting the preferences simply press the back button and you're done.

Inflammatory answered 3/12, 2011 at 11:17 Comment(1)
This solution works fine if your using PreferenceActivity, However, you cannot use this method with PreferenceFragment. You say you are calling PreferenceFragment through an Intent, but in your solution in Prefs.java you are using PreferenceActivity not PreferenceFragment.Dissent
S
8

Create your PreferenceFragment.java class like this:

    public class UserPreferenceFragment extends PreferenceFragment {

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

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

        getView().setBackgroundColor(Color.BLACK);
        getView().setClickable(true);
    }

}

the trick is:

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

            getView().setBackgroundColor(Color.BLACK);
            getView().setClickable(true);
        }

EDIT:

Edited as suggested by JDenais, even if it's not strictly necessary to the topic.

Stenopetalous answered 16/5, 2014 at 13:2 Comment(2)
I suggest that you also add : getView().setClickable(true); to prevent clicks from going through the fragment and accidentally clicking the activity below.Beverie
In case of using PreferenceActivity for older APIs in spite of PreferenceFragment one should change onActivityCreated(savedInstanceState) to onResume() and getView to getListView.Xanthophyll
B
6

I had the same problem and solved it //without having to start a new activity//. This method has the advantage that your main activity doesn't go through a pause-resume cycle. The key is to have your main UI as a fragment, and then hide it when the Pref fragment is called. The main fragment can be included statically or dynamically.

 FragmentTransaction ft = getFragmentManager().beginTransaction();
 AppSettingsFragment prefs = new AppSettingsFragment();
 // This adds the newly created Preference fragment to my main layout, shown below
 ft.add(R.id.main_layout,prefs);
 // By hiding the main fragment, transparency isn't an issue
 ft.hide(mMyMainFragment);
 ft.addToBackStack(null);
 ft.commit();

The main_layout.xml looks like:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- Uncomment for static fragment inclusion -->
<!-- fragment android:name="com.legynd.ui.MyMainFragment"
    android:id="@+id/mainfragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" / -->
</RelativeLayout>
Breuer answered 1/5, 2012 at 14:25 Comment(0)
C
6

The best and most portable solution is answered here

Which is:

Adding the following code to your PreferenceFragment will let you add a background color, image, etc.

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

    return view; }
Cammycamomile answered 4/3, 2015 at 13:17 Comment(0)
A
2

In your ReaderPreferences implementation add:

public void onResume() {
    super.onResume();
    getView().setBackgroundColor(Color.WHITE);
}
Aldas answered 7/8, 2013 at 9:42 Comment(0)
N
2

Use an empty layout for your activity like this:

activity_settings.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/settingsFragment"
   android:layout_width="match_parent"
   android:layout_height="match_parent">
</FrameLayout>

then in activity's onCreate:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings);
    ReaderPreferences mReaderPreferences = new ReaderPreferences ();
    getSupportFragmentManager().beginTransaction()
       .replace(R.id.settingsFragment, mReaderPreferences ).commit();
    }
Novikoff answered 26/12, 2013 at 10:38 Comment(1)
This seems like a reasonable solution when you also include .addToBackStack(null) before commit();Concierge
V
1

The background color can be set to the Fragment View in onStart(). As below:

public class ReaderPreferences extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.preference);
}

@Override
public void onStart() {
   super.onStart();
   View v = getView();
   v.setBackgroundColor(getActivity().getResources.getColor(R.color.my_color));
}

This works for me on all of my test devices.

The accepted answer is not the answer to what the question states here. In fact, its a workaround which creates a container activity for the preference fragment.

Vibrations answered 2/12, 2011 at 21:29 Comment(0)
H
1

Add this in your Fragment 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;
}
Homestead answered 12/6, 2015 at 21:41 Comment(0)
I
0

Eventually fixed it with a very easy fix.

I simply called the PreferenceFragment in a new Intent, and it worked perfectly.

For anyone with the same problem:

Prefs.java

public class ReaderPreferences extends PreferenceActivity {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            // --load the preferences from an XML file---
            addPreferencesFromResource(R.xml.preference);
        }

In my main screen when pressing a button:

Intent i = new Intent(this, Prefs.class);
            startActivity(i);

That's all. After setting the preferences simply press the back button and you're done.

Inflammatory answered 3/12, 2011 at 11:17 Comment(1)
This solution works fine if your using PreferenceActivity, However, you cannot use this method with PreferenceFragment. You say you are calling PreferenceFragment through an Intent, but in your solution in Prefs.java you are using PreferenceActivity not PreferenceFragment.Dissent
T
0

I was also stuck with the exact same issue, and after trying out a couple of things I found our that it was happening because in my case I had mixed up two different versions of Fragments and FragmentManagers.

The onCreate of my MainActivity looked like this:

getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new MainFragment())
                .commit();

However when I wanted to replace this MainFragment with my own PreferenceFragment, I tried using something similar to what you used:

getFragmentManager().beginTransaction()
                    .replace(R.id.container, new SettingsFragment())
                    .addToBackStack(null)
                    .commit();

The way I understand it is that, you should use the same type of FragmentManager for replace to work, so I just had to go ahead and make appropriate changes to the way I wired up the MainFragment making it look like this:

getFragmentManager().beginTransaction()
            .add(R.id.container, new MainFragment())
            .commit();

Ofcourse, I had to refactor MainFragment to extend from android.app.Fragment instead of android.support.v4.app.Fragment

Towhead answered 29/1, 2014 at 20:17 Comment(0)
J
0

I tried Shayan_Aryan's solution, but my app was dying with error:

Your content must have a ListView whose id attribute is 'android.R.id.list'

So I had to create a LinearLayout with no text in the empty case. Well I suppose there is a more elegant solution, but this was the only one that worked to me.

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

<ListView android:id="@+id/android:list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

<TextView android:id="@+id/android:empty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""/>
</LinearLayout> 
Jameson answered 22/3, 2014 at 19:32 Comment(0)
G
0

i used many techniques but no gain
Finally i fixed it by adding background of the container or the fragment layout like this

android:background="@android:color/white"    

you just need to add in your Layout or the container view of you Fragment
Hope it will help

Graphophone answered 8/10, 2016 at 20:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.