Karlom Karlom - 6 months ago 10
Android Question

Android Actionbar shows no items

I'm new to android and have this problem for while: No item appear on action bar, as you can see in the image below:

enter image description here

Here is my 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="com.myappshop.myapp.MainActivity">

<item
android:id="@+id/action_favorite"
android:icon="@drawable/share_icon"
android:title="@string/share"
app:showAsAction="always"/>


<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="ifRoom" />

</menu>


And the MainActivity.java:

public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();

EditText mEditTextWord;
EditText mEditTextDefinition;
DictionaryDatabase mDB;
ListView mListView;

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

mDB = new DictionaryDatabase(this);
mEditTextWord = (EditText)findViewById(R.id.editTextWord);
mEditTextDefinition = (EditText)findViewById(R.id.editTextDefinition);
Button buttonAddUpdate = (Button)findViewById(R.id.buttonAddUpdate);

buttonAddUpdate.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
saveRecord();
}
});



mListView = (ListView)findViewById(R.id.listView);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View
view, int position, long id) {
Log.d(TAG, "the id is logged " + Long.toString(id));

String nextId = String.valueOf(id+1);
Intent intent = new Intent(view.getContext(),DetailActivity.class);
intent.putExtra("key" ,mDB.getWord(id)+"");
intent.putExtra("value",mDB.getDefinition(id)+"");
intent.putExtra("nextId",nextId+"");
startActivity(intent);
}
});

mListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this,
"Records deleted = " + mDB.deleteRecord(id),
Toast.LENGTH_SHORT).show();
updateWordList();
return true;
}
});
updateWordList();

}


private void saveRecord() {
mDB.saveRecord(mEditTextWord.getText().toString(),
mEditTextDefinition.getText().toString());
mEditTextWord.setText("");
mEditTextDefinition.setText("");
updateWordList();
}

private void updateWordList() {
SimpleCursorAdapter simpleCursorAdapter = new
SimpleCursorAdapter( this,
android.R.layout.simple_list_item_1,
mDB.getWordList(),
new String[]{"word"},
new int[]{android.R.id.text1},
0);
mListView.setAdapter(simpleCursorAdapter);
}


@Override //added following http://stackoverflow.com/a/27192897/5774375
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}


activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.myappshop.myapp.MainActivity">

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main" />

</android.support.design.widget.CoordinatorLayout>


The minimum API that I'v set is 15.
I've looked at similar answers on SO, like this but none resolve my probelm. So appreciate your help.

Answer

You have to inflate your menu.xml before you can call its menu items. Add this before onOptionsItemSelected()

    @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;
    }

Sorry about that. I also noticed that you didn't declare your toolbar in your activitys' onCreate(). You would have to declare your toolbar before placing menu items on it. Add this to your activity's onCreate method.

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

Also, make sure you include your toolbar/appbar layout in your main layout for this activity.