Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use onCreateOptionsMenu() in Activity Which Extended ListActivity

I have a problem about my android app project.

I have a MainActivity which is below:

public class MainActivity extends ListActivity {

    private NotesDataSource datasource;
    List<NoteItem> notesList;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        datasource = new NotesDataSource(this);

        refreshDisplay();
    }

    private void refreshDisplay() {
        notesList = datasource.findAll();
        ArrayAdapter<NoteItem> adapter = new ArrayAdapter<NoteItem>(this, R.layout.list_item_layout, notesList);
        setListAdapter(adapter);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }


}

And Also I have a menu_main.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">

    <item android:id="@+id/action_create"
        android:title="@string/action_create"
        android:orderInCategory="100"
        app:showAsAction="always|withText"
        android:icon="@drawable/create_note"/>
</menu>

At this point the problem is starting. I have changed my superclass from ActionBarActivity to ListActivity then, when I run my app on my device, I can not see my create icon (and the top menu which include app name). What's wrong? And idea?

(Btw I am using Android Studio Ide)

like image 575
Mehmet Avatar asked Oct 20 '22 20:10

Mehmet


1 Answers

Your theme is probably still based on Theme.AppCompat. If you want to use Theme.AppCompat, you have to inherit from ActionBarActivity. If you want to use ListActivity, you cannot use Theme.AppCompat.

Note that you do not need to use ListActivity to have a ListView in an activity. Here is a sample project demonstrating the use of Theme.AppCompat with an ActionBarActivity and a ListView. Here is another sample project, the same as the previous one, except that I apply custom tints to the action bar.

like image 173
CommonsWare Avatar answered Nov 01 '22 15:11

CommonsWare