androidtoolbarlistactivity

Android how to use Toolbar in ListActivity?


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 ?


Solution

  • 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.