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?