saa
saa

Reputation: 1568

PreferenceFragment display

I am new to fragment concept. In my app I have to save user preferences. I have gone through this doc. Prepared my preferences xml file and PreferenceFragment file. Everything is fine up to now. My problem is, I have to add the following code in my onCreate() method of my MainActivity

getFragmentManager().beginTransaction()
            .replace(android.R.id.content, new SettingsFragment())
            .commit();

It is showing on Main screen. But I want to launch this on a button click method

 onSettingsClicked(){
         // launch preferces screen
    }

And I want to display it as a separate screen. How can I do that?

Upvotes: 0

Views: 1661

Answers (2)

Sabin
Sabin

Reputation: 69

Instead of using fragementTransaction you can use preferenceFragement : How do you create Preference Activity and Preference Fragment on Android? by WannaGetHigh

Upvotes: 0

Pradip
Pradip

Reputation: 3177

You need to implement the concept of fragmentTransaction. You need to do the following things-

  1. Create a new Fragment.xml to display the button.
  2. Invoke the new fragment from the onCreate();
  3. Get the Button on the Fragment view.
  4. On Button onclick listener replace the the fragment with the SettingFragment().
    Done!

Check out this FragmentTransaction Tutorial, it will guide you-

Do the following changes like -

// Code not accurate, may be some syntax error  
@Override
public void onCreate()
{
// super and other stuff

getFragmentManager().beginTransaction()
            .replace(android.R.id.content, new NewFragment())
            .commit();

Button btn = (Button)findViewById(R.id.button01);
btn.setOnClickListener(new OnClickListener(){

 @override
 public void onClick(View v)
 {
 FragmentTransaction ft = getFragmentManager().beginTransaction();

            ft.replace(android.R.id.content, new new SettingsFragment())
            .commit();
 }

});


}

Upvotes: 1

Related Questions