android - How to add ActionBar to an activity which already extends ListActivity? -
i have activity extends listactivity
.
can add actionbar
without extending actionbaractivity
?
you can use new appcompatdelegate component provided support library.
actionbar deprecated , should use toolbar
, compliant material design. can use toolbar provided support library.
add xml layout this:
<android.support.v7.widget.toolbar android:id="@+id/my_awesome_toolbar" android:layout_height="wrap_content" android:layout_width="match_parent" android:minheight="56dp" android:background="?attr/colorprimary" />
be sure use noactionbar
theme in styles.xml. use material design color tags.
<style name="apptheme" parent="theme.appcompat.noactionbar"> </style>
then, add appcompatdelegate
activity, in oncreate(), this.
@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) { } }; appcompatdelegate delegate = appcompatdelegate.create(this,callback); delegate.oncreate(savedinstancestate); delegate.setcontentview(r.layout.activity_main); toolbar toolbar= (toolbar) findviewbyid(r.id.my_awesome_toolbar); delegate.setsupportactionbar(toolbar); }
note: create appcompatdelegate need pass activity , callback, practice should implementing callback in activity, shortening reasons created instance in oncreate().