84

After I upgraded to the Support Library v21 my ActionBar in my PreferenceActivity is gone.

Did I miss some attributes in my theme to activate it again? I had some similar trouble with a black ActionBar.

I also tried to add it a little hackish by adding a Toolbar to the root layout, but that did not work as expected.

Community
  • 1
  • 1
rekire
  • 47,260
  • 30
  • 167
  • 264
  • You should be using Preference Fragment: http://developer.android.com/reference/android/preference/PreferenceFragment.html – Jared Burrows Oct 22 '14 at 13:58
  • 1
    @JaredBurrows you cant use PreferenceFragment pre 3.0 though – tyczj Oct 22 '14 at 13:59
  • 1
    I'm using them too. AFIK I need to link to a PreferenceActivity which uses the PreferenceFragments. However as tycyj points out I need them it also for legacy support. – rekire Oct 22 '14 at 13:59
  • @tyczj Yes, there are alternatives: http://stackoverflow.com/questions/9783368/alternatives-to-preferencefragment-with-android-support-v4 – Jared Burrows Oct 22 '14 at 14:00
  • @JaredBurrows well yeah with a 3rd party library, there is nothing native for that though – tyczj Oct 22 '14 at 14:01
  • @JaredBurrows so you mean that [`android-support-v4-preferencefragment`](https://github.com/kolavar/android-support-v4-preferencefragment) supports also the support library v21 I want that matrial like actionbar aka toolbar. – rekire Oct 22 '14 at 14:02
  • 1
    The toolbar is just a view. You can add it anywhere. – Jared Burrows Oct 22 '14 at 14:03
  • I already tried it the result looked like a normal TextView. – rekire Oct 22 '14 at 14:03
  • 1
    You can check out the example I've made here: https://github.com/AndroidDeveloperLB/MaterialPreferenceLibrary – android developer May 27 '15 at 21:47
  • @Android that library looks nice, I'll possible use it for my next project – rekire May 28 '15 at 05:09
  • @rekire Thank you. You can also see it in action on my "app manager" app. Can you show the project you'll work on? or the project you wanted to have this? – android developer May 28 '15 at 06:30
  • @androiddeveloper I'm developing on [this](https://play.google.com/store/apps/details?id=de.netmoms.cyclecalendar&referrer=utm_source%3DStackOverflow%26utm_medium%3Dtextlink%26utm_term%3Drekire%26utm_content%3Duserprofile) app. – rekire May 28 '15 at 06:47
  • @rekire This looks awesome (on the screenshots). It's for fertility ? – android developer May 28 '15 at 06:49
  • @androiddeveloper yes it is. If you have more question, please join [this](http://chat.stackoverflow.com/rooms/19132/java-and-android-era) chat. – rekire May 28 '15 at 06:50
  • @rekire Well I'm not a woman and not married, and also the app isn't available for my device for some reason, but could be useful in the future. Does it have an English language in it? If so, I might be able to translate to Hebrew. – android developer May 28 '15 at 07:02
  • Chat doesn't work well. The blocking shows as if it's the device's fault and not country. Anyway thanks and good luck ! – android developer May 28 '15 at 07:42
  • Preferences in support.v7 23 are pretty much screwed. – f470071 Nov 04 '15 at 18:53
  • I raised a defect, please vote for it: https://code.google.com/p/android/issues/detail?id=207718 – Leos Literak Apr 21 '16 at 19:26
  • in case this might help to you. https://stackoverflow.com/a/49532356/8203967 – Rk215 Tech Mar 28 '18 at 10:49
  • i case above solution not work then use my answer . https://stackoverflow.com/a/49532356/8203967 – Rk215 Tech Mar 28 '18 at 10:50

8 Answers8

169

Please find the GitHub Repo: Here


Very Similar to your own code but added xml to allow for set title:

Continuing to use PreferenceActivity:

settings_toolbar.xml :

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    app:navigationContentDescription="@string/abc_action_bar_up_description"
    android:background="?attr/colorPrimary"
    app:navigationIcon="?attr/homeAsUpIndicator"
    app:title="@string/action_settings"
    />

SettingsActivity.java :

public class SettingsActivity extends PreferenceActivity {

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);

        LinearLayout root = (LinearLayout)findViewById(android.R.id.list).getParent().getParent().getParent();
        Toolbar bar = (Toolbar) LayoutInflater.from(this).inflate(R.layout.settings_toolbar, root, false);
        root.addView(bar, 0); // insert at top
        bar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

}

Result :

example


UPDATE (Gingerbread Compatibility) :

As pointed out here, Gingerbread Devices are returning NullPointerException on this line:

LinearLayout root = (LinearLayout)findViewById(android.R.id.list).getParent().getParent().getParent();

FIX:

SettingsActivity.java :

public class SettingsActivity extends PreferenceActivity {

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        Toolbar bar;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            LinearLayout root = (LinearLayout) findViewById(android.R.id.list).getParent().getParent().getParent();
            bar = (Toolbar) LayoutInflater.from(this).inflate(R.layout.settings_toolbar, root, false);
            root.addView(bar, 0); // insert at top
        } else {
            ViewGroup root = (ViewGroup) findViewById(android.R.id.content);
            ListView content = (ListView) root.getChildAt(0);

            root.removeAllViews();

            bar = (Toolbar) LayoutInflater.from(this).inflate(R.layout.settings_toolbar, root, false);
            

            int height;
            TypedValue tv = new TypedValue();
            if (getTheme().resolveAttribute(R.attr.actionBarSize, tv, true)) {
                height = TypedValue.complexToDimensionPixelSize(tv.data, getResources().getDisplayMetrics());
            }else{
                height = bar.getHeight();
            }

            content.setPadding(0, height, 0, 0);

            root.addView(content);
            root.addView(bar);
        }

        bar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}

Any issues with the above let me know!


UPDATE 2: TINTING WORKAROUND

As pointed out in many dev notes PreferenceActivity does not support tinting of elements, however by utilising a few internal classes you CAN achieve this. That is until these classes are removed. (Works using appCompat support-v7 v21.0.3).

Add the following imports:

import android.support.v7.internal.widget.TintCheckBox;
import android.support.v7.internal.widget.TintCheckedTextView;
import android.support.v7.internal.widget.TintEditText;
import android.support.v7.internal.widget.TintRadioButton;
import android.support.v7.internal.widget.TintSpinner;

Then override the onCreateView method:

@Override
public View onCreateView(String name, Context context, AttributeSet attrs) {
    // Allow super to try and create a view first
    final View result = super.onCreateView(name, context, attrs);
    if (result != null) {
        return result;
    }

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        // If we're running pre-L, we need to 'inject' our tint aware Views in place of the
        // standard framework versions
        switch (name) {
            case "EditText":
                return new TintEditText(this, attrs);
            case "Spinner":
                return new TintSpinner(this, attrs);
            case "CheckBox":
                return new TintCheckBox(this, attrs);
            case "RadioButton":
                return new TintRadioButton(this, attrs);
            case "CheckedTextView":
                return new TintCheckedTextView(this, attrs);
        }
    }

    return null;
}

Result:

example 2


AppCompat 22.1

AppCompat 22.1 introduced new tinted elements, meaning that there is no longer a need to utilise the internal classes to achieve the same effect as the last update. Instead follow this (still overriding onCreateView):

@Override
public View onCreateView(String name, Context context, AttributeSet attrs) {
    // Allow super to try and create a view first
    final View result = super.onCreateView(name, context, attrs);
    if (result != null) {
        return result;
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        // If we're running pre-L, we need to 'inject' our tint aware Views in place of the
        // standard framework versions
        switch (name) {
            case "EditText":
                return new AppCompatEditText(this, attrs);
            case "Spinner":
                return new AppCompatSpinner(this, attrs);
            case "CheckBox":
                return new AppCompatCheckBox(this, attrs);
            case "RadioButton":
                return new AppCompatRadioButton(this, attrs);
            case "CheckedTextView":
                return new AppCompatCheckedTextView(this, attrs);
        }
    }

    return null;
}

NESTED PREFERENCE SCREENS

A lot of people are experiencing issues with including the Toolbar in nested <PreferenceScreen />s however, I have found a solution!! - After a lot of trial and error!

Add the following to your SettingsActivity:

@SuppressWarnings("deprecation")
@Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
    super.onPreferenceTreeClick(preferenceScreen, preference);

    // If the user has clicked on a preference screen, set up the screen
    if (preference instanceof PreferenceScreen) {
        setUpNestedScreen((PreferenceScreen) preference);
    }

    return false;
}

public void setUpNestedScreen(PreferenceScreen preferenceScreen) {
    final Dialog dialog = preferenceScreen.getDialog();

    Toolbar bar;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        LinearLayout root = (LinearLayout) dialog.findViewById(android.R.id.list).getParent();
        bar = (Toolbar) LayoutInflater.from(this).inflate(R.layout.settings_toolbar, root, false);
        root.addView(bar, 0); // insert at top
    } else {
        ViewGroup root = (ViewGroup) dialog.findViewById(android.R.id.content);
        ListView content = (ListView) root.getChildAt(0);

        root.removeAllViews();

        bar = (Toolbar) LayoutInflater.from(this).inflate(R.layout.settings_toolbar, root, false);

        int height;
        TypedValue tv = new TypedValue();
        if (getTheme().resolveAttribute(R.attr.actionBarSize, tv, true)) {
            height = TypedValue.complexToDimensionPixelSize(tv.data, getResources().getDisplayMetrics());
        }else{
            height = bar.getHeight();
        }

        content.setPadding(0, height, 0, 0);

        root.addView(content);
        root.addView(bar);
    }

    bar.setTitle(preferenceScreen.getTitle());

    bar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });
}

The reason that PreferenceScreen's are such a pain is because they are based as a wrapper dialog, so we need to capture the dialog layout to add the toolbar to it.


Toolbar Shadow

By design importing the Toolbar does not allow for elevation and shadowing in pre-v21 devices, so if you would like to have elevation on your Toolbar you need to wrap it in a AppBarLayout:

`settings_toolbar.xml :

<android.support.design.widget.AppBarLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

   <android.support.v7.widget.Toolbar
       .../>

</android.support.design.widget.AppBarLayout>

Not forgetting to add the add the Design Support library as a dependency in build.gradle file:

compile 'com.android.support:support-v4:22.2.0'
compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.android.support:design:22.2.0'

Android 6.0

I have investigated the reported overlapping issue and I cannot reproduce the issue.

The full code in use as above produces the following:

enter image description here

If I am missing something please let me know via this repo and I will investigate.

Community
  • 1
  • 1
David Passmore
  • 6,089
  • 4
  • 46
  • 70
  • Nice one: i have used the first approach (Gingerbread is not on my target). Thank you! – JJ86 Jan 07 '15 at 13:30
  • It appears that the up button is broken on Gingerbread using this example. – idunnololz Jan 15 '15 at 04:12
  • Update: The issue was that the ToolBar was placed **underneath** the content view due to the order in which they were added. The fix is to simply call `root.addView(bar);` after `root.addView(content);` – idunnololz Jan 15 '15 at 04:22
  • Thanks, after 3 nights of frustration I finally got it to work... :) – Tony Morello Mar 03 '15 at 14:14
  • @TonyMorello Glad to be of assistance :) – David Passmore Mar 03 '15 at 14:15
  • Amazing! I tried to solve this problem for a long time and this is by far the easiest approach. How about wrap it in a library? – David Vávra Mar 07 '15 at 13:31
  • What about with appcompact 22.1.0 ? It should be easier to theme PreferenceActivity now. Maybe you could update the code? :) – andQlimax Apr 26 '15 at 15:50
  • Dropping by to say thanks. Your comment on my question guided me here (I fixed my problem following your answer) and I'm glad you've kept this answer updated. I was getting that NPE on Gingerbread, if by any chance you were curious. ;) – Sufian Apr 29 '15 at 13:09
  • 1
    Thank you for your great answer, I gave you some days ago the accepted state. I just copied the tinting part, to make sure that it will look great :) – rekire Apr 29 '15 at 13:16
  • Thanks suffian also :) – David Passmore Apr 29 '15 at 13:24
  • I recently got a crash report in Dev console. **Optimus Pad (l06c)** running **Android 3.1** crashed and the exception was `Caused by: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.ListView` and the line it pointed to was `LinearLayout root = (LinearLayout) findViewById(android.R.id.list).getParent().getParent().getParent();`. – Sufian May 22 '15 at 13:00
  • @suffian I will create a virtual machine with 3.1 and test. – David Passmore May 22 '15 at 14:10
  • I tried this approach and it works great, thank you. But I have an issue when using nested Preferences (by defining a `` in xml: The Actionbar won't show here. Is there any workaround (I'd like to keep using a `PreferenceActivity`). – tony Jun 09 '15 at 20:40
  • @tony UPDATE: Wish I could find a solution, but as of yet there is none... `:(` – David Passmore Jun 09 '15 at 22:06
  • @David Passmore Thank you anyways for trying! There are a couple methods mentioned [here](http://stackoverflow.com/questions/26564400/), I guess I have to use another approach. Too bad, your solution is the cleanest in my opinion, but ultimately we should blame Google.. – tony Jun 09 '15 at 22:24
  • @tony I will continue to investigate the options of using a custom layout as a possible workaround – David Passmore Jun 09 '15 at 22:29
  • @DavidPassmore I'm wondering if it's possible to somehow include this: http://stackoverflow.com/a/26705551/1613083 – tony Jun 10 '15 at 00:15
  • @DavidPassmore You are a genius! It works like a charm. I had to slightly adapt it to my setup by moving the `onPreferenceTreeClick` to my PreferenceFragment and it does what it should! (My SettingsActivity inherits from Activity rather than SettingsActivity, the same code will result in the toolbar overlaying the text if I don't - but that's due to my setup and not for you to worry about I think). One note: With AppCombat v22.1.0+ you need to change `app:theme` to android:theme or the toolbar text will be black, see [here](https://chris.banes.me/2015/04/22/support-libraries-v22-1-0/). – tony Jun 11 '15 at 01:24
  • 1
    I have to correct myself. The `android:theme` workaround only fixes the black text color at the toolbar for Android 5.0+. It appears as something changed with the new support library (I'm using 22.2.0). – tony Jun 11 '15 at 02:17
  • @tony i saw the sam bug, i revertwd to 22.1 to resolve – David Passmore Jun 11 '15 at 07:13
  • We got a crash on Android 4.0.X, It appears that the `gingerbread compatibility` code needs to also be run for that version, so: `if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)` – roundhill Sep 30 '15 at 22:53
  • On my Android 6, the above code displays the list with overlapping the `Toolbar` – codelearner Dec 22 '15 at 14:29
  • @codelearner I will investigate as Android 6 has not been tested yet `;)` – David Passmore Dec 23 '15 at 20:44
  • @DavidPassmore after spending few hours on code, I finally found a very simple solution... :D – codelearner Dec 24 '15 at 12:55
  • @DavidPassmore I updated above post with my working codes for API > 10... :) – codelearner Dec 24 '15 at 13:27
  • @codelearner I have reviewed you edit.. thanks for the input. But I am deliberately trying to avoid fragments `:)` – David Passmore Dec 25 '15 at 19:47
  • How to change the Title of the PerferenceActivity to the selected preferencefragment Title? Because the toolbar is added onPostCreate() it does not excist on all the lifecycle events I tried in the fragments... – Roel Feb 10 '16 at 10:25
  • @DalvikVM Can you elaborate as my examples do not use fragments... If there is an issue please raise an issue here https://github.com/davcpas1234/MaterialSettings/issues/new Thanks! `:)` – David Passmore Feb 14 '16 at 23:23
  • I fixed it by updating the title in a handler.post( new Runnable { ... } ); I included your code in default generated code of Android studio (with fragments). – Roel Feb 15 '16 at 12:42
  • I have followed the first approach given here. But i am getting a transparent toolbar and under that my preference items are displayed. Can anybody provide a solution for this? – Karthik May 04 '16 at 10:49
  • 4
    android.R.id.list 's parent is FrameLayout in android N – zys May 28 '16 at 14:53
  • It has to be that complicated? – Simon Dec 16 '16 at 23:31
  • Just for the record. If you are using fragments you should modify this code a bit. Otherwise you will encounter overlapping problem that was mentioned. First of all you put the code in fragment's `onActivityCreated` method. Then you call `getParent()` twice instead of three times (layouts for `PreferenceActivity` and `PreferenceFragment` are slightly different). Other changes are minor and pretty obvious - because the code is in a fragment you use `getView().findViewById()` and `getActivity().getLayoutInflater()`. – Krzysztof Wołowski Apr 07 '17 at 13:43
  • @zys - it isn't. Most probably you were using fragments. Look at my comment above. – Krzysztof Wołowski Apr 07 '17 at 13:52
45

Use AppCompatActivity & PreferenceFragment to solve the issue:

AppCompatActivity:

public class SettingsActivity extends AppCompatActivity {

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    getFragmentManager().beginTransaction().replace(android.R.id.content, new SettingsFragment()).commit();
}}

PreferenceFragment:

public class SettingsFragment extends PreferenceFragment {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.settings_preferences);
}}
d.moncada
  • 16,900
  • 5
  • 53
  • 82
Abdullah
  • 9,005
  • 1
  • 23
  • 24
7

I ended in adding the Toolbar myself with this simple code:

// get the root container of the preferences list
LinearLayout root = (LinearLayout)findViewById(android.R.id.list).getParent().getParent().getParent();
Toolbar bar = (Toolbar)LayoutInflater.from(this).inflate(R.layout.preferences_toolbar, root, false);
root.addView(bar, 0); // insert at top
bar.setNavigationOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        finish();
    }
});

Here is my preferences_toolbar.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_height="wrap_content"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    app:navigationContentDescription="@string/abc_action_bar_up_description"
    android:background="?attr/colorPrimary"
    app:navigationIcon="?attr/homeAsUpIndicator"
    app:theme="@style/Theme.Toolbar" />
rekire
  • 47,260
  • 30
  • 167
  • 264
6

A better solution than "rolling your own" action bar is to use the AppCompatDelegate class, which lets you bring in an actual-factual action bar from the support library. Here is example code to use it, taken from Ľubomír Kučera's answer to this question.

...
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatDelegate;
import android.support.v7.widget.Toolbar;
...

public class SettingsActivity extends PreferenceActivity {

    private AppCompatDelegate mDelegate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        getDelegate().installViewFactory();
        getDelegate().onCreate(savedInstanceState);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        getDelegate().onPostCreate(savedInstanceState);
    }

    public ActionBar getSupportActionBar() {
        return getDelegate().getSupportActionBar();
    }

    public void setSupportActionBar(@Nullable Toolbar toolbar) {
        getDelegate().setSupportActionBar(toolbar);
    }

    @Override
    public MenuInflater getMenuInflater() {
        return getDelegate().getMenuInflater();
    }

    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        getDelegate().setContentView(layoutResID);
    }

    @Override
    public void setContentView(View view) {
        getDelegate().setContentView(view);
    }

    @Override
    public void setContentView(View view, ViewGroup.LayoutParams params) {
        getDelegate().setContentView(view, params);
    }

    @Override
    public void addContentView(View view, ViewGroup.LayoutParams params) {
        getDelegate().addContentView(view, params);
    }

    @Override
    protected void onPostResume() {
        super.onPostResume();
        getDelegate().onPostResume();
    }

    @Override
    protected void onTitleChanged(CharSequence title, int color) {
        super.onTitleChanged(title, color);
        getDelegate().setTitle(title);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        getDelegate().onConfigurationChanged(newConfig);
    }

    @Override
    protected void onStop() {
        super.onStop();
        getDelegate().onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        getDelegate().onDestroy();
    }

    public void invalidateOptionsMenu() {
        getDelegate().invalidateOptionsMenu();
    }

    private AppCompatDelegate getDelegate() {
        if (mDelegate == null) {
            mDelegate = AppCompatDelegate.create(this, null);
        }
        return mDelegate;
    }
}
Community
  • 1
  • 1
Tad
  • 4,668
  • 34
  • 35
  • 4
    a.k.a [AppCompatPreferenceActivity](https://github.com/android/platform_development/blob/master/samples/Support7Demos/src/com/example/android/supportv7/app/AppCompatPreferenceActivity.java) from official v7 samples. – Avinash R Jul 22 '15 at 09:45
3

Hi I am not if you still have this issue. But I figure I will post what I did to resolve this and hope it will help someone.

1) First off, you might have noticed that PreferenceActivity extends ListActivity which in turns extends from Activity.

According to the comments on the developers blog (http://android-developers.blogspot.com/2014/10/appcompat-v21-material-design-for-pre.html), to use v21 all your activities must inherit from ActionBarActivity. So there is your issue.

2) The steps I used to resolve are :

a) Make sure that you set the Theme of your PreferenceActivity to inherits one ot the Theme.AppCompat Themes.

b) Make your class PreferenceActivity extends ActionBarActivity.

c) Use the PreferenceFragment as your container for all your preferences.

This should resolve it.

Cheers!

  • 4
    "Make your class PreferenceActivity extends ActionBarActivity" - but HOW? – vbence Jan 01 '15 at 19:43
  • Check out this link here: http://code.hootsuite.com/tips-and-tricks-for-android-material-support-library/?utm_source=Android+Weekly&utm_campaign=624d204414-Android_Weekly_135&utm_medium=email&utm_term=0_4eb677ad19-624d204414-337844421 – AfrikAndroid Jan 11 '15 at 18:05
  • 1
    Thanks, I think this comes as close as possible. The real pain is that PreferenceActivity has built-in navigation through *loadHeadersFromResource*, with fragments you have to build the navigation yourself. – vbence Jan 11 '15 at 18:58
1

I had the same issue. just try to change the Theme of the activity to the one that has any ActionBar in it. adding the following line in the activity tag of SettingsActivity worked out for me :

android:theme="Theme.AppCompat.DayNight.DarkActionBar"

any mous
  • 11
  • 1
0

I did something similar to accepted question, but I'm using my layout in other activities as well (MainActivity too), so I cannot just hard-code an arrow in it.

What worked for me:

private void setupActionBar() {
    LinearLayout root = (LinearLayout)findViewById(android.R.id.list).getParent().getParent().getParent();
    Toolbar toolbar = (Toolbar)LayoutInflater.from(this).inflate(R.layout.app_bar, root, false);
    root.addView(toolbar, 0);
    setSupportActionBar(toolbar);
    ActionBar ab = getSupportActionBar();
    ab.setDisplayHomeAsUpEnabled(true);
}
bobbyrne01
  • 6,295
  • 19
  • 80
  • 150
Moterrola
  • 1
  • 1
-1

An easy way I figured out is overriding:

@android:style/Theme.Material.Light.DarkActionBar

In your style:

<style name="SettingsTheme" parent="@android:style/Theme.Material.Light.DarkActionBar">
    <item name="android:colorPrimary">@color/sunshine_blue</item>
    <item name="android:colorPrimaryDark">@color/sunshine_dark_blue</item>
</style>
spenibus
  • 4,339
  • 11
  • 26
  • 35