0

I just wrote an appliation which uses a v21 Toolbar instead of the original ActionBar. It works by adding the Toolbar as "setSupportActionBar()".

Now I've created a PreferenceActivity and the problem ist that this Activity doesn't show any ActionBar or Toolbar. How can I add the SupportActionBar to a PreferenceActivity? Here's my base theme:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="windowActionBar">false</item>
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primary_dark</item>
    <item name="colorAccent">@color/accent</item>
</style>

As You can see the normal windowActionbar was turned off.

user2410644
  • 3,861
  • 6
  • 39
  • 59

2 Answers2

0

You can create a PreferenceFragment in your ActionBarActivity.

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
0

I was also struggling with this issue. I ended up inflating the Toolbar and adding it to the root layout (https://stackoverflow.com/a/27455330/1889752).

PreferenceActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings);

    LinearLayout linearLayout = (LinearLayout)findViewById(android.R.id.list).getParent().getParent().getParent();
    Toolbar toolbar = (Toolbar) LayoutInflater.from(this).inflate(R.layout.tool_bar, linearLayout, false);

    toolbar.setTitle(getString(R.string.settings_header_title));
    linearLayout.addView(toolbar, 0);

    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            overridePendingTransition(0,0);
            finish();
        }
    });
}

activity_settings.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<ListView android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/> 

tool_bar.xml

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/blue"
android:theme="@style/ThemeOverlay.AppCompat.Dark"
app:navigationIcon="?attr/homeAsUpIndicator">

You should have a look at the AppCompatPreferenceActivity

Community
  • 1
  • 1
Richard Olthuis
  • 217
  • 2
  • 10