0

I am using PreferenceFragment to get preferences from user. I have designed a simple preference file having only one EditPreference. I have created the following preferencefragement file

public class fragact extends PreferenceFragment
{

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.fragpref);
    }

}

In my main activity i am trying to show the fragment at the click of a button.However the fragment comes transparent and is not very visible and the underlying screen is seen through it. why the fragment does not come proper. i created another activity and used it to show the fragment.it worked properly .but why can we show the fragment directly rather than showing through another activity in its oncreate and calling it through startActivity in class fragprefmain.

public class fragprefmain extends Activity
{
    EditText et;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragpref);
        et=(EditText)findViewById(R.id.editText1);
    }

    public void showp(View v)
    {
        FragmentManager fm=getFragmentManager();
        FragmentTransaction ft=fm.beginTransaction();
        ft.replace(android.R.id.content, new fragact());
        ft.commit();
    }

    public void showval(View v)
    {
        SharedPreferences sp=PreferenceManager.getDefaultSharedPreferences(this);
        et.setText(sp.getString("ekey", "no valued given"));

    }
tejinder
  • 61
  • 7

2 Answers2

0

maybe try to replace the opaque id

android.R.id.content

with your own layout id that you define in your activity xml file

it can be an empty layout but you must give the layout an id, say:

 <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/my_layout_id">

so now you get:

.replace/add(R.id.my_layout_id, new fragact());
Noam Geffen
  • 339
  • 3
  • 6
0

You might as well create a preferenceActivity as given by WannaGetHigh in How do you create Preference Activity and Preference Fragment on Android?

Community
  • 1
  • 1
Sabin
  • 61
  • 3