How to use onCreateOptionsMenu () in activity that has Extended ListActivity

I have a problem with 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 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 begins. I changed my superclass from ActionBarActivity to ListActivity , then when I launch my app on my device, I don't see the create icon (and the top menu that includes the app name). What's wrong? And the idea?

(I'm using Android Studio Ide by the way)

+3


source to share


1 answer


Your theme is probably still based on Theme.AppCompat

. If you want to use Theme.AppCompat

, you must inherit from ActionBarActivity

. If you want to use ListActivity

, you cannot use Theme.AppCompat

.



Note that you don't need to use ListActivity

to have it ListView

in action. Here is a sample project demonstrating use Theme.AppCompat

with ActionBarActivity

and ListView

. Here's another example project , just like the previous one, except I'm applying custom tints to the action bar.

+2


source







All Articles