WhS4 WhS4 - 1 month ago 26
Android Question

Android Menu not Showing

I'd like to make a menu with a delete option. The actual delete functionality isn't made yet because at the moment I can't see the top bar in my app.

Main layout (activity_main.xml):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="billy.cs436.placebadgesapp.MainActivity">

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/newPlace"
android:text="@string/newPlaceButton"
/>
</RelativeLayout>


Menu layout (main.xml):

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/deleteMenu"
android:icon="@drawable/clear"
android:title="@string/deleteMenu"
app:showAsAction="ifRoom"
/>
</menu>


Main activity (MainActivity.java):

package billy.cs436.placebadgesapp;

import android.content.Intent;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends Activity {

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

newPlace = (Button) findViewById(R.id.newPlace);
newPlace.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), setLocation.class);
startActivity(intent);
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the main; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);//Menu Resource, Menu
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if(id == R.id.deleteMenu) {
// if there are no badges, toast message saying so (needs implementing)
Toast.makeText(this, "There are no badges to delete!", Toast.LENGTH_SHORT).show();
//else clear all badges
} else {
Toast.makeText(this, "Badges cleared!", Toast.LENGTH_SHORT).show();
}
return super.onOptionsItemSelected(item);
}

}


Can anyone tell my why the New Place button is the only thing that shows up when this is run?

EDIT:
@T.S has the correct answer. Thank you.

T.S T.S
Answer

Change your class to extend AppCompatActivity instead of Activity.