ActionBar
//MainActivity
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
TextView count;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) { switch(item.getItemId()) {
case R.id.add:
//add the function to perform here
Toast.makeText(getApplicationContext(),"Add",Toast.LENGTH_SHORT).show();
return(true);
case R.id.reset:
//add the function to perform here
Toast.makeText(getApplicationContext(),"Reset",Toast.LENGTH_SHORT).show();
return(true);
case R.id.about:
//add the function to perform here
Toast.makeText(getApplicationContext(),"About",Toast.LENGTH_SHORT).show();
return(true);
case R.id.exit:
//add the function to perform here
Toast.makeText(getApplicationContext(),"Exit",Toast.LENGTH_SHORT).show();
return(true);
}
return(super.onOptionsItemSelected(item));
}}
//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"
tools:context=".MainActivity" >
</RelativeLayout>
//menu_main.xml
res -> New -> Android resource file -> File name -> menu_main.xml -> Resource type -> Menu
<?xml version="1.0" encoding="utf-8"?>
<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/add"
app:showAsAction="never"
android:title="add"/>
<item
android:id="@+id/reset"
app:showAsAction="never"
android:title="reset"/>
<item
android:id="@+id/about"
app:showAsAction="never"
android:title="about"/>
<item
android:id="@+id/exit"
app:showAsAction="never"
android:title="exit"/>
</menu>