0

I created a Setting activity in my app, but it didn't work. This is my SettingFragment class:

public class SettingFragment extends PreferenceFragmentCompat
{
    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey)
    {
        setPreferencesFromResource(R.xml.pref, rootKey);
    }
}

This is my SettingActivity:

public class SettingActivity extends AppCompatActivity
{
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.content, new SettingFragment())
                .commit();
    }
}

This is MainActivity. Clicking the button will open SettingActivity.

btn.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                Intent intent = new Intent(MainActivity.this, SettingActivity.class);
                startActivity(intent);
            }
        });

And this is Preference.xml:

<PreferenceScreen 
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <Preference
        app:key="feedback"
        app:title="Send feedback"
        app:summary="Report technical issues or suggest new features"/>

</PreferenceScreen>

What is the R.id.content? It was used in the developer.android site example. What is the problem? How can I solve this?

Nestor
  • 8,194
  • 7
  • 77
  • 156
Farid
  • 158
  • 1
  • 2
  • 13
  • we will need a bit more of an error than "it didn't work" to determine the problem you are trying to solve. What error are you getting? To answer your question about `R` it is an autogenerated file that contains all your resource IDs. – Mike Poole Aug 10 '19 at 13:50
  • What is not working? What kind of error are you getting? Also include the `activity_setting.xml` layout file. – Shaiful Aug 10 '19 at 14:00

2 Answers2

1

You need to use android.R.id.content. This will replace the root content with the Fragment's content. you can find more information about android.R.id.content from this

// Display the fragment as the main content.
getSupportFragmentManager()
                .beginTransaction()
                .replace(android.R.id.content, new SettingFragment())
                .commit();
sanoJ
  • 2,935
  • 2
  • 8
  • 18
1

What is the R.id.content?

Lets assume that this is the SettingActivity layout file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SettingsActivity">

    <LinearLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="16dp" >
    </LinearLayout>

</LinearLayout>

The call to replace will swap your fragment with R.id.content. You can also call replace with android.R.id.content, read more here.

RonTLV
  • 2,376
  • 2
  • 24
  • 38