Reputation: 1783
I have followed the official Android documentation on how to create a settings activity using a fragment, but I got this result:
I have tried setting a top margin to the settings contents but that didn't change the result, the settings would still end up below the app bar as you can see in the picture.
So, this are the relevant parts of my code:
My settings activity
public class SettingsActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Display the settings fragment as the main content.
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingsFragment())
.commit();
}
}
Nothing fancy here, I just load the layout file and insert the fragment with the list of settings.
The settings fragment
public class SettingsFragment extends PreferenceFragment {
public SettingsFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
}
}
Again, nothing fancy, I simply load the preferences from their XML file.
The settings activity layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.mydomain.myapp.SettingsActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_settings" />
</android.support.design.widget.CoordinatorLayout>
The settings activity content layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.mydomain.myapp.SettingsActivity"
tools:showIn="@layout/activity_settings">
</RelativeLayout>
This is empty for now as I am loading everything from the XML preferences file anyway.
How can I make sure that the content of my settings activity ends up right below the activity's app bar?
Upvotes: 0
Views: 590
Reputation: 1564
Add an id to your RelativeLayout
and use that as the id in your FragmentManager.replace
. This makes it use the RelativeLayout
for you fragment container instead of the whole system window.
Upvotes: 4