Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PreferenceFragment is shown transparently

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?

like image 331
Sander van't Veer Avatar asked Dec 02 '11 21:12

Sander van't Veer


4 Answers

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.

like image 151
Andrea Bellitto Avatar answered Nov 13 '22 05:11

Andrea Bellitto


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>
like image 45
CjS Avatar answered Nov 13 '22 05:11

CjS


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; }
like image 6
ScreenSeer Avatar answered Nov 13 '22 03:11

ScreenSeer


In your ReaderPreferences implementation add:

public void onResume() {
    super.onResume();
    getView().setBackgroundColor(Color.WHITE);
}
like image 2
maksp Avatar answered Nov 13 '22 03:11

maksp