Android how to use Toolbar in ListActivity?
Asked Answered
W

4

5

I made a ListActivity:

 public class NoteListActivity extends ListActivity{
 }

and the following methods are no longer available:

    setSupportActionBar(toolbar);
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

But I can use:

    setActionBar(toolbar);
    getActionBar().setHomeButtonEnabled(true);
    getActionBar().setDisplayHomeAsUpEnabled(true);

Note that I used Support library in other activities in the same project. It means I have the correct gradle dependency added.

How do I use ListActivity and android.support.v7.widget.Toolbar ?

Wheaten answered 4/8, 2015 at 12:1 Comment(0)
M
5

You can't do that in ListActivity.

If you want to access getSupportActionBar(), you need to extend your class with AppCompatActivity.

My Suggestion : Don't use ListActivty as you want to use ToolBar. Create an Activity and then only have ListView within that Activity. It'll work just fine.

Malcolmmalcom answered 4/8, 2015 at 12:7 Comment(0)
O
5
     @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    AppCompatCallback callback = new AppCompatCallback() {
        @Override
        public void onSupportActionModeStarted(ActionMode actionMode) {
        }

        @Override
        public void onSupportActionModeFinished(ActionMode actionMode) {
        }

        @Nullable
        @Override
        public ActionMode onWindowStartingSupportActionMode(ActionMode.Callback callback) {
            return null;
        }
    };

    AppCompatDelegate delegate = AppCompatDelegate.create(this, callback);

    delegate.onCreate(savedInstanceState);
    delegate.setContentView(R.layout.saved_report_activity);

    Toolbar toolbar= (Toolbar) findViewById(R.id.toolbar);
    delegate.setSupportActionBar(toolbar);
    delegate.getSupportActionBar().setDisplayShowHomeEnabled(true);

    toolbar.setNavigationIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            NavUtils.navigateUpFromSameTask(SavedReportActivity.this);
        }
    });
Outofdoors answered 26/1, 2016 at 19:37 Comment(0)
A
1

AppCompatActivity is the direct child class of FragmentActivity of support v4 .Read this article

http://android-developers.blogspot.it/2015/04/android-support-library-221.html so for your requirement, public class NoteListActivity extends AppCompatActivity {...}

Just change your dependency,Like :

compile "com.android.support:appcompat-v7:22.1.0"
Atalie answered 4/8, 2015 at 12:39 Comment(0)
S
0

I was able to solve this by adding a support library toolbar style and then adding the style to my list activity in the manifest.

In styles.xml:

     <style name="FullscreenActionBarStyle" parent="Widget.AppCompat.ActionBar">

In Manifest

    <activity android:theme="@style/FullscreenActionBarStyle"
              android:name="@string/my_activity_name"
              android:label="@string/my_activity_label"/>

This should add a toolbar to your activity that you can add items to and process events with, etc...

Sinister answered 23/7, 2018 at 20:49 Comment(1)
Yes, actionBar is deprecated and you should not use ListActivity but for smooth migration of legacy application. This code worksHepcat

© 2022 - 2024 — McMap. All rights reserved.