Seperti tampak menu diaplikasi kalkulator dibawah:
Setelah dipilih tambah maka akan muncul hasil tambah :
Cara membuatnya cukup mudah, berikut cara membuat options menu di aplikasi android..
Langsung saja buat projek dengan isi program kalkulator:
Sourcecode:
Layout>menu.xml
<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=".MainActivity" ><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/hello_world" /><EditTextandroid:id="@+id/editText1"android:layout_width="wrap_content"android:hint="masukan angka 1"android:layout_height="wrap_content"android:layout_alignLeft="@+id/textView1"android:layout_alignParentRight="true"android:layout_below="@+id/textView1"android:layout_marginTop="14dp"android:ems="10" ><requestFocus /></EditText><EditTextandroid:id="@+id/editText2"android:hint="masukan angka 2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignLeft="@+id/editText1"android:layout_alignParentRight="true"android:layout_below="@+id/editText1"android:layout_marginTop="16dp"android:ems="10" /></RelativeLayout>
Values>string.xml
<?xml version="1.0" encoding="utf-8"?><resources><string name="app_name">Prak6</string><string name="action_settings">Settings</string><string name="hello_world">Hello world!</string><string name="tambah">tambah</string><string name="kurang">kurang</string><string name="kali">kali</string><string name="bagi">bagi</string><string name="keluar">keluar</string></resources>
Menu>main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" ><itemandroid:id="@+id/action_settings"android:orderInCategory="100"android:showAsAction="never"android:title="@string/action_settings"/><itemandroid:id="@+id/tambah"android:orderInCategory="100"android:showAsAction="never"android:title="@string/tambah"/><itemandroid:id="@+id/kurang"android:orderInCategory="100"android:showAsAction="never"android:title="@string/kurang"/><itemandroid:id="@+id/kali"android:orderInCategory="100"android:showAsAction="never"android:title="@string/kali"/><itemandroid:id="@+id/bagi"android:orderInCategory="100"android:showAsAction="never"android:title="@string/bagi"/><itemandroid:id="@+id/keluar"android:orderInCategory="100"android:showAsAction="never"android:title="@string/keluar"/></menu>
MainActivity.java
package com.example.prak6;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.app.AlertDialog;import android.widget.EditText;import android.content.Context;import android.content.DialogInterface;import android.widget.Toast;import android.view.MenuItem;public class MainActivity extends Activity {EditText editText1,editText2;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);editText1=(EditText)findViewById(R.id.editText1);editText2=(EditText)findViewById(R.id.editText2);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}public void keluar(){this.finish();}////@SuppressWarnings("deprecation")@Overridepublic boolean onOptionsItemSelected(MenuItem item) {switch (item.getItemId()){case R.id.tambah://tambah();//Double tam=Double.parseDouble(editText1.getText().toString())+Double.parseDouble(editText2.getText().toString());int durasi=Toast.LENGTH_LONG;Context context=getApplicationContext();Toast toast=Toast.makeText(context, Double.toString(tam), durasi);toast.show();//return true;case R.id.kurang:// kurang();Double kur=Double.parseDouble(editText1.getText().toString())-Double.parseDouble(editText2.getText().toString());int durasi2=Toast.LENGTH_LONG;Context context2=getApplicationContext();Toast toast2=Toast.makeText(context2, Double.toString(kur), durasi2);toast2.show();return true;case R.id.bagi://bagi();Double bag=Double.parseDouble(editText1.getText().toString())/Double.parseDouble(editText2.getText().toString());int durasi3=Toast.LENGTH_LONG;Context context3=getApplicationContext();Toast toast3=Toast.makeText(context3, Double.toString(bag), durasi3);toast3.show();return true;case R.id.kali://kali();Double kal=Double.parseDouble(editText1.getText().toString())*Double.parseDouble(editText2.getText().toString());int durasi4=Toast.LENGTH_LONG;Context context4=getApplicationContext();Toast toast4=Toast.makeText(context4, Double.toString(kal), durasi4);toast4.show();return true;case R.id.keluar://keluar();AlertDialog alertDialog = new AlertDialog.Builder(this).create();alertDialog.setTitle("Exit Alert");alertDialog.setMessage("Apakah benar mau keluar?");alertDialog.setButton("Ya", new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int which) {finish();return;} });alertDialog.setButton2("Tidak", new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int which) {dialog.cancel();return;}});alertDialog.show();return true;default:return super.onOptionsItemSelected(item);}}}
0 komentar:
Post a Comment