Android, how to use toolbar in ListActivity?

I have activity like

public class NoteListActivity extends ListActivity{ } 

I no longer have the following method: -

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

But I have the following: -

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

Please note that I use the material of the supported library in other activities in the same project. This means that I added the correct gradle dependency.

How to use ListActivity and android.support.v7.widget.Toolbar?

+4
source share
3 answers

You cannot do this in ListActivity .

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

My suggestion: Do not use ListActivty as you want to use ToolBar . Create an Activity , and then only a ListView inside the Activity . Everything will be fine.

+4
source
  @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); } }); 
+5
source

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

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

Just change your dependency, How to:

 compile "com.android.support:appcompat-v7:22.1.0" 
+1
source

All Articles