Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PreferenceFragment with transparent background?

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?

like image 829
user2573690 Avatar asked Apr 28 '14 21:04

user2573690


People also ask

How do I use Preferencefragment?

Use PreferenceFragmentCompat to programatically handle preferences. To load the settings into the fragment, load the preferences in the onCreatePreferences() method. To get an instance of a preference, search for a preference using its key through the PreferenceManager within onCreateView() .

What is Preferencefragment?

The preference framework handles showing these other screens from the preference hierarchy. The preference hierarchy can be formed in multiple ways: From an XML file specifying the hierarchy. From different Activities that each specify its own preferences in an XML file via Activity meta-data.

What is Preferencescreen in Android?

Represents a top-level Preference that is the root of a Preference hierarchy. A PreferenceActivity points to an instance of this class to show the preferences. To instantiate this class, use PreferenceManager#createPreferenceScreen(Context) .

How do you display the preference of your application using an activity?

Let us see in this tutorial how to integrate Android's Preference framework ( PreferenceActivity ) in any android app. Create a Hello World Android project in Eclipse. Go to New > Project > Android Project. Give the project name as Android_Preferences_example and select Android Runtime 2.1 or sdk 7.


2 Answers

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;
}
like image 191
powder366 Avatar answered Oct 31 '22 16:10

powder366


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();.

like image 34
RAINA Avatar answered Oct 31 '22 16:10

RAINA