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:
As you can see, you look straight through my preferences. What did I do wrong?
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.
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>
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; }
In your ReaderPreferences implementation add:
public void onResume() {
super.onResume();
getView().setBackgroundColor(Color.WHITE);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With