How to add Action Bar from support library into PreferenceActivity?

Roman

Action Bar compatibility has been added into support library, revision 18. It now has ActionBarActivity class for creating activities with Action Bar on older versions of Android.

Is there any way to add Action Bar from support library into PreferenceActivity?

Previously I used ActionBarSherlock and it has SherlockPreferenceActivity.

Ľubomír Kučera

EDIT: In appcompat-v7 22.1.0 Google added the AppCompatDelegate abstract class as a delegate you can use to extend AppCompat's support to any activity.

Use it like this:

...
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;
    }
}

No more hacking. Code taken from AppCompatPreferenceActivity.java.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to change action bar background when using the Support Library?

From Dev

Error implementing Support Library Action Bar

From Dev

Settings is coming in Overflow with Action Bar Support Library

From Dev

Build errors with action bar, support library

From Dev

How to add material design library switch in the action bar

From Dev

How to add action bar

From Dev

How to add Toolbar in PreferenceActivity

From Dev

How to add a Search to an action bar?

From Dev

How to add colour to the action bar

From Dev

How to add search into action bar

From Dev

Overlay the Action bar programmatically when using the support library

From Dev

Issue on implementing action bar with tabs using support library

From Dev

How to add progress bar icon in action bar?

From Dev

Android support action bar

From Java

No ActionBar in PreferenceActivity after upgrade to Support Library v21

From Dev

how to add action bar items to second activity

From Dev

How to add menu button without action bar?

From Dev

How to add the back arrow in the action bar?

From Dev

How to add action bar to Android app?

From Dev

How to add support gd library to server

From Dev

How to get SharedPreferences from a PreferenceActivity

From Dev

Using CollapsingToolbarLayout, how can I set it as the support action bar?

From Dev

How to add action bar to my main activity from another class that extends ActionBarActivity?

From Dev

PhpStorm: How to add method stubs from a PECL library that PhpStorm doesn't currently support?

From Dev

Customizing Support Action Bar / Android

From Dev

Add action to action bar panel

From Dev

How to Refresh Fragment from Action Bar Menu

From Dev

How to Refresh Fragment from Action Bar Menu

From Dev

How To Start A Fragment From Spinner in action bar?

Related Related

  1. 1

    How to change action bar background when using the Support Library?

  2. 2

    Error implementing Support Library Action Bar

  3. 3

    Settings is coming in Overflow with Action Bar Support Library

  4. 4

    Build errors with action bar, support library

  5. 5

    How to add material design library switch in the action bar

  6. 6

    How to add action bar

  7. 7

    How to add Toolbar in PreferenceActivity

  8. 8

    How to add a Search to an action bar?

  9. 9

    How to add colour to the action bar

  10. 10

    How to add search into action bar

  11. 11

    Overlay the Action bar programmatically when using the support library

  12. 12

    Issue on implementing action bar with tabs using support library

  13. 13

    How to add progress bar icon in action bar?

  14. 14

    Android support action bar

  15. 15

    No ActionBar in PreferenceActivity after upgrade to Support Library v21

  16. 16

    how to add action bar items to second activity

  17. 17

    How to add menu button without action bar?

  18. 18

    How to add the back arrow in the action bar?

  19. 19

    How to add action bar to Android app?

  20. 20

    How to add support gd library to server

  21. 21

    How to get SharedPreferences from a PreferenceActivity

  22. 22

    Using CollapsingToolbarLayout, how can I set it as the support action bar?

  23. 23

    How to add action bar to my main activity from another class that extends ActionBarActivity?

  24. 24

    PhpStorm: How to add method stubs from a PECL library that PhpStorm doesn't currently support?

  25. 25

    Customizing Support Action Bar / Android

  26. 26

    Add action to action bar panel

  27. 27

    How to Refresh Fragment from Action Bar Menu

  28. 28

    How to Refresh Fragment from Action Bar Menu

  29. 29

    How To Start A Fragment From Spinner in action bar?

HotTag

Archive