Pages

Senin, 26 Maret 2012

Mengenal Intent di Android

Pada postingan kali ini saya akan membahas tentang intent. Sebenarnya apakah intent itu? kalo menurut saya intent merupakan suatu objek yang terdapat dalam suatu activity dimana dengan objek tersebut bisa melakukan komunikasi dengan activity yang lain, baik activity yang sudah terdapat pada fungsi internal android (Browser, kamera, dll) ataupun memanggil activity yang lain baik dalam satu package ataupun beda package dalam satu projek. Itu menurut saya loh.. kalo ada koreksi, silakan dikoreksi saja...Dari Pengeritian yang saya berikan di atas. Intent itu terbagi dua, yaitu implicit intent dan explicit intent.


  • Implicit intent merupakan intent yang memanggil fungsi yang sudah ada pada fungsi internal android.
  • Explicit intent merupakan intent yang memanggil Activity lain.
Oke tanpa banyak pengertian-pengertian lagi yang membuat saya semakin bingung, langsung saja pada contoh projeknya... O ya sebelumnya Siapkan juga projek yang sebelumnya, yaitu Membuat Kalkulator Sederhana Dengan Android. Kalo sudah, langsung ikuti langkah berikut:

  • Buat projek baru --> File>new>android project
  • Isi nama projek dan yang lain-lainnya 
  • Membuat layout. Layout yang akan kita gunakan adalah main.xml dan layout yang berasal dari kalkulator sederhana. Berikut file xml untuk main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >




    <Button
    android:id="@+id/kamera"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/gambar" />

    </LinearLayout>




    <Button
    android:id="@+id/calcdroid"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/kalkulator" />

    </LinearLayout>

    dan berikut adalah kalkulator.xml yang merupakan file dari main.xml calcdroid
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <EditText
    android:id="@+id/input1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:editable="false"
    android:inputType="number"
    android:numeric="integer"
    android:text="" >

    <requestFocus />

    </EditText>


    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    />
    <LinearLayout
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1">

    >
    <Button
    android:id="@+id/satu"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="1"
    ></Button>
    <Button
    android:id="@+id/empat"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="4"
    ></Button>
    <Button
    android:id="@+id/tujuh"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="7"
    ></Button>

    <Button
    android:id="@+id/koma"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="." >
    </Button>




    <Button
    android:id="@+id/sin"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/sinus" />




    <Button
    android:id="@+id/sepertan"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/tangens" />

    </LinearLayout>

    <LinearLayout
    android:layout_weight="1"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <Button
    android:id="@+id/dua"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="2"
    ></Button>
    <Button
    android:id="@+id/lima"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="5"
    ></Button>
    <Button
    android:id="@+id/delapan"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="8"
    ></Button>
    <Button
    android:id="@+id/nol"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="0"
    ></Button>





    <Button
    android:id="@+id/cos"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/cosinus" />




    <Button
    android:id="@+id/pangkatn"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/pankatn" />

    </LinearLayout>
    <LinearLayout
    android:layout_weight="1"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <Button
    android:id="@+id/tiga"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="3"
    ></Button>
    <Button
    android:id="@+id/enam"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="6"
    ></Button>
    <Button
    android:id="@+id/sembilan"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="9"
    ></Button>
    <Button
    android:id="@+id/samadengan"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="="
    ></Button>





    <Button
    android:id="@+id/tan"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/Tangen" />





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

    </LinearLayout>
    <LinearLayout
    android:layout_weight="1"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    >
    <Button
    android:id="@+id/tambah"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="+"
    ></Button>
    <Button
    android:id="@+id/kurang"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="-"
    ></Button>
    <Button
    android:id="@+id/kali"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="*"
    ></Button>
    <Button
    android:id="@+id/bagi"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="/"
    ></Button>




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

    </LinearLayout>
    <LinearLayout
    android:layout_weight="1"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    >
    <Button
    android:id="@+id/clear"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Clear"
    ></Button>





    <Button
    android:id="@+id/faktorial"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/button" />




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




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




    <Button
    android:id="@+id/sepercos"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/cot" />

    </LinearLayout>
    </LinearLayout>

    </LinearLayout>


    • Setelah membuat layout. Sekarang buat package baru, misalnya namanya org.dharmatin.Calcdroid dan tempelkan file Calcdroid.java pada package ini dan sesuaikan sampai tidak ada tanda error. berikut adalah file Calcdroid.java yang sudah dimodifikasi:

    package org.dharmatin.Calcdroid;

    import org.dharmatin.PengenalanIntent.R;

    import android.app.Activity;
    import android.os.Bundle;
    import android.speech.SpeechRecognizer;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;

    public class CalcdroidActivity extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    Button satu,dua,tiga,empat,lima,enam,tujuh,delapan,sembilan,
    nol,tambah,kali,kurang,bagi,samadengan,akar,clear,koma,faktorial,pangkat
    ,pangkatn,akarn,sin,cos,tan,sepersin,sepercos,sepertan,kombinasi,permutasi;
    EditText input;
    int operator;
    double nilai1 , nilai2;
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.kalkulator);

    input = (EditText)findViewById(R.id.input1);
    satu = (Button)findViewById(R.id.satu);
    dua = (Button)findViewById(R.id.dua);
    tiga = (Button)findViewById(R.id.tiga);
    empat = (Button)findViewById(R.id.empat);
    lima = (Button)findViewById(R.id.lima);
    enam = (Button)findViewById(R.id.enam);
    tujuh = (Button)findViewById(R.id.tujuh);
    delapan = (Button)findViewById(R.id.delapan);
    sembilan = (Button)findViewById(R.id.sembilan);
    nol = (Button)findViewById(R.id.nol);
    tambah = (Button)findViewById(R.id.tambah);
    kali = (Button)findViewById(R.id.kali);
    kurang = (Button)findViewById(R.id.kurang);
    bagi = (Button)findViewById(R.id.bagi);
    samadengan = (Button)findViewById(R.id.samadengan);
    koma = (Button)findViewById(R.id.koma);
    clear = (Button)findViewById(R.id.clear);
    faktorial = (Button) findViewById(R.id.faktorial);
    akar = (Button) findViewById(R.id.akar);
    pangkat = (Button) findViewById(R.id.pangkat);
    akarn = (Button) findViewById(R.id.akarn);
    pangkatn = (Button) findViewById(R.id.pangkatn);
    sin = (Button) findViewById(R.id.sin);
    cos = (Button) findViewById(R.id.cos);
    tan = (Button) findViewById(R.id.tan);
    sepercos = (Button) findViewById(R.id.sepercos);
    sepersin = (Button) findViewById(R.id.sepersin);
    sepertan = (Button) findViewById(R.id.sepertan);


    satu.setOnClickListener(this);
    dua.setOnClickListener(this);
    tiga.setOnClickListener(this);
    empat.setOnClickListener(this);
    lima.setOnClickListener(this);
    enam.setOnClickListener(this);
    tujuh.setOnClickListener(this);
    delapan.setOnClickListener(this);
    sembilan.setOnClickListener(this);
    nol.setOnClickListener(this);
    tambah.setOnClickListener(this);
    kali.setOnClickListener(this);
    kurang.setOnClickListener(this);
    bagi.setOnClickListener(this);
    samadengan.setOnClickListener(this);
    koma.setOnClickListener(this);
    clear.setOnClickListener(this);
    faktorial.setOnClickListener(this);
    sin.setOnClickListener(this);
    sepersin.setOnClickListener(this);
    cos.setOnClickListener(this);
    sepercos.setOnClickListener(this);
    tan.setOnClickListener(this);
    sepertan.setOnClickListener(this);
    pangkat.setOnClickListener(this);
    pangkatn.setOnClickListener(this);
    akar.setOnClickListener(this);
    akarn.setOnClickListener(this);

    }

    public void onClick(View v) {
    switch (v.getId()) {
    case R.id.satu:
    handleNomor("1");
    break;
    case R.id.dua:
    handleNomor("2");
    break;
    case R.id.tiga:
    handleNomor("3");
    break;
    case R.id.empat:
    handleNomor("4");
    break;
    case R.id.lima:
    handleNomor("5");
    break;
    case R.id.enam:
    handleNomor("6");
    break;
    case R.id.tujuh:
    handleNomor("7");
    break;
    case R.id.delapan:
    handleNomor("8");
    break;
    case R.id.sembilan:
    handleNomor("9");
    break;
    case R.id.nol:
    handleNomor("0");
    break;
    case R.id.koma:
    handleNomor(".");
    break;
    case R.id.tambah:
    nilai1=Double.parseDouble(input.getText().toString());
    input.setText("");
    operator=1;
    break;
    case R.id.kurang:
    nilai1=Double.parseDouble(input.getText().toString());
    input.setText("");
    operator=2;
    break;
    case R.id.kali:
    nilai1=Double.parseDouble(input.getText().toString());
    input.setText("");
    operator=3;
    break;
    case R.id.bagi:
    nilai1=Double.parseDouble(input.getText().toString());
    input.setText("");
    operator=4;
    break;
    case R.id.samadengan:
    nilai2=Double.parseDouble(input.getText().toString());
    input.setText("");
    if(operator==1){
    input.setText(Double.toString(nilai1+nilai2));
    }else if(operator==2){
    input.setText(Double.toString(nilai1-nilai2));
    }else if(operator==3){
    input.setText(Double.toString(nilai1*nilai2));
    }else if(operator==4){
    input.setText(Double.toString(nilai1/nilai2));
    }else if(operator==5){
    input.setText(Double.toString(Math.pow(nilai1, 1/nilai2)));
    }else if(operator==6){
    input.setText(Double.toString(Math.pow(nilai1, nilai2)));
    }
    break;
    case R.id.clear:
    clear();
    break;
    case R.id.faktorial:
    input.setText(Integer.toString(factorial(Integer.parseInt(input.getText().toString()))));
    break;
    case R.id.akar:
    nilai1=Double.parseDouble(input.getText().toString());
    input.setText(Double.toString(Math.sqrt(nilai1)));
    break;
    case R.id.akarn:
    operator = 5;
    nilai1=Double.parseDouble(input.getText().toString());
    input.setText("");
    break;
    case R.id.pangkat:
    nilai1=Double.parseDouble(input.getText().toString());
    input.setText(Double.toString(Math.pow(nilai1, 2)));
    break;
    case R.id.pangkatn:
    nilai1=Double.parseDouble(input.getText().toString());
    input.setText("");
    operator=6;
    break;
    case R.id.sin:
    nilai1=Double.parseDouble(input.getText().toString());
    input.setText(Double.toString(Math.sin(nilai1)));
    break;
    case R.id.sepersin:
    nilai1=Double.parseDouble(input.getText().toString());
    input.setText(Double.toString(1/(Math.sin(nilai1))));
    break;
    case R.id.cos:
    nilai1 = Double.parseDouble(input.getText().toString());
    input.setText(Double.toString(Math.cos(nilai1)));
    break;
    case R.id.sepercos:
    nilai1 = Double.parseDouble(input.getText().toString());
    input.setText(Double.toString(1/Math.cos(nilai1)));
    break;
    case R.id.tan:
    nilai1 = Double.parseDouble(input.getText().toString());
    input.setText(Double.toString(Math.tan(nilai1)));
    break;
    case R.id.sepertan:
    nilai1 = Double.parseDouble(input.getText().toString());
    input.setText(Double.toString(1/Math.tan(nilai1)));
    default:
    break;
    }


    }
    private void handleNomor(String nomor) {
    // TODO Auto-generated method stub

    String txt = input.getText().toString();
    txt+=nomor;
    input.setText(txt);

    }
    private void clear(){
    input.setText("");
    }
    private static int factorial(int bilangan){
    if(bilangan==0){
    return 1;
    }else{
    bilangan = bilangan * factorial(bilangan-1);
    }
    return bilangan;
    }
    }


    • Setelah itu masuk ke main activitynya:


    package org.dharmatin.PengenalanIntent;

    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;

    public class PengenalanIntentActivity extends Activity implements OnClickListener {
        /** Called when the activity is first created. */
    private Button kamera;
    private Button calcdroid;
    private Intent intent;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
         
            kamera = (Button) findViewById(R.id.kamera);
            calcdroid = (Button) findViewById(R.id.calcdroid);
         
            kamera.setOnClickListener(this);
            calcdroid.setOnClickListener(this);
         
         
        }

    public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.kamera:
    intent = new Intent("android.media.action.IMAGE_CAPTURE");//Inten Implicit Untuk memanggil Camera
    startActivity(intent);
    break;
    case R.id.calcdroid:
    intent = new Intent(this, org.dharmatin.Calcdroid.CalcdroidActivity.class);//Inten Explicit Memanggil Activity Calcdroid
    startActivity(intent);
    break;

    default:
    break;
    }

    }
    }


    • Jangan lupa ubah file AndroidManifest.xml
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.dharmatin.PengenalanIntent"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />
    <uses-permission android:name="android.permission.CAMERA"/>

    <application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
    android:name=".PengenalanIntentActivity"
    android:label="@string/app_name" >
    <intent-filter>
    <action android:name="android.intent.action.MAIN" />

    <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    </activity>
    <activity android:name="org.dharmatin.Calcdroid.CalcdroidActivity"></activity>
    </application>

    </manifest>


    • Selesai.

    Berikut projeknya silakan di downloadSemoga bermanfaat.


    ===>>>SALAM<<<=====
    -Mengenal Intent di Android

    Rabu, 21 Maret 2012

    Membuat Calculator Sederhana Di Android

    Pada postingan kali ini saya akan mencoba membuat kalkulator yang sangat sederhana sekali. kalkulator ini hanya berisi operator (+,-,*,/) dan kalkulator ini hanya menggunakan logika yang sangat sederhana, agar bisa lebih mudah dipahami. langsung praktek saja dah..
    Yang pertama dilakukan adalah membuka IDE eclipse yang sudah terpasang ADT (Android development tools). untuk cara penginstallan di eclipse tinggal browsing saja.
    Yang Kedua buat project android baru, namanya terserah apa saja..
    Yang ketiga.... Saatnya ngoding....
    hal yang pertama dalam membuat kalkulator ini, tentunya kita membuat terlebih dahulu user interfacenya atau layout kalo di android. Buka file main.xml pada folder project res/layout dan masukan skrip xml berikut:
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent">
    
    <EditText
        android:id="@+id/input1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:editable="false"
        android:inputType="number"
        android:numeric="integer"
        android:text="" >
    
        <requestFocus />
    
    </EditText>
    
    
    <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="horizontal"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content">
      
        />
      <LinearLayout
      android:orientation="vertical"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_weight="1">
      
      >
             <Button
             android:id="@+id/satu"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:layout_weight="1"
             android:text="1"
             ></Button>
             <Button
             android:id="@+id/empat"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:layout_weight="1"
             android:text="4"
             ></Button>
             <Button
             android:id="@+id/tujuh"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:layout_weight="1"
             android:text="7"
             ></Button>
    
             <Button
                 android:id="@+id/koma"
                 android:layout_width="fill_parent"
                 android:layout_height="wrap_content"
                 android:layout_weight="1"
                 android:text="." >
    </Button></LinearLayout>
      
      <LinearLayout
      android:layout_weight="1"
      android:orientation="vertical"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content">
      
       <Button
             android:id="@+id/dua"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:layout_weight="1"
             android:text="2"
             ></Button>
             <Button
             android:id="@+id/lima"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:layout_weight="1"
             android:text="5"
             ></Button>
             <Button
             android:id="@+id/delapan"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:layout_weight="1"
             android:text="8"
             ></Button>
             <Button
             android:id="@+id/nol"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:layout_weight="1"
             android:text="0"
             ></Button></LinearLayout>
      <LinearLayout
      android:layout_weight="1"
      android:orientation="vertical"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content">
      
       <Button
             android:id="@+id/tiga"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:layout_weight="1"
             android:text="3"
             ></Button>
             <Button
             android:id="@+id/enam"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:layout_weight="1"
             android:text="6"
             ></Button>
             <Button
             android:id="@+id/sembilan"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:layout_weight="1"
             android:text="9"
             ></Button>
             <Button
             android:id="@+id/samadengan"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:layout_weight="1"
             android:text="="
             ></Button></LinearLayout>
      <LinearLayout
      android:layout_weight="1"
      android:orientation="vertical"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content">
      
      >
             <Button
             android:id="@+id/tambah"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:layout_weight="1"
             android:text="+"
             ></Button>
             <Button
             android:id="@+id/kurang"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:layout_weight="1"
             android:text="-"
             ></Button>
             <Button
             android:id="@+id/kali"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:layout_weight="1"
             android:text="*"
             ></Button>
             <Button
             android:id="@+id/bagi"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:layout_weight="1"
             android:text="/"
             ></Button></LinearLayout>
             <LinearLayout
      android:layout_weight="1"
      android:orientation="vertical"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content">
      
      >
             <Button
             android:id="@+id/clear"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:layout_weight="1"
             android:text="Clear"
             ></Button></LinearLayout>
    </LinearLayout>
    
    </LinearLayout>
    
    Setelah kita membuat layout, sekarang saatnya untuk membuat codingan java pada.
    Yang pertama Menginisialisasikan komponen-komponen dari layout dan variabel lainnya:
    Button satu,dua,tiga,empat,lima,enam,tujuh,delapan,sembilan,
    nol,tambah,kali,kurang,bagi,samadengan,akar,clear,koma;
    EditText input;
    int operator;
    double nilai1 , nilai2;
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    input = (EditText)findViewById(R.id.input1);
    satu = (Button)findViewById(R.id.satu);
    dua = (Button)findViewById(R.id.dua);
    tiga = (Button)findViewById(R.id.tiga);
    empat = (Button)findViewById(R.id.empat);
    lima = (Button)findViewById(R.id.lima);
    enam = (Button)findViewById(R.id.enam);
    tujuh = (Button)findViewById(R.id.tujuh);
    delapan = (Button)findViewById(R.id.delapan);
    sembilan = (Button)findViewById(R.id.sembilan);
    nol = (Button)findViewById(R.id.nol);
    tambah = (Button)findViewById(R.id.tambah);
    kali = (Button)findViewById(R.id.kali);
    kurang = (Button)findViewById(R.id.kurang);
    bagi = (Button)findViewById(R.id.bagi);
    samadengan = (Button)findViewById(R.id.samadengan);
    koma = (Button)findViewById(R.id.koma);
    clear = (Button)findViewById(R.id.clear);

    Yang Kedua Menginisialisasikan action pada button:
    satu.setOnClickListener(this);
    dua.setOnClickListener(this);
    tiga.setOnClickListener(this);
    empat.setOnClickListener(this);
    lima.setOnClickListener(this);
    enam.setOnClickListener(this);
    tujuh.setOnClickListener(this);
    delapan.setOnClickListener(this);
    sembilan.setOnClickListener(this);
    nol.setOnClickListener(this);
    tambah.setOnClickListener(this);
    kali.setOnClickListener(this);
    kurang.setOnClickListener(this);
    bagi.setOnClickListener(this);
    samadengan.setOnClickListener(this);
    koma.setOnClickListener(this);
    clear.setOnClickListener(this);

    }
    jang lupa mengimplement-kan onClickListener jika kita akan membuat action dengan klik.
    Yang Ketiga Membuat Method handlerNomor, metod ini berfungsi untuk memberikan display jika kita menekan button, misalkan kita menekan button satu dan dua, maka pada editteks akan muncul 12, jika kita tidak membuat metod ini, maka jika kita menekan button satu dan dua, maka pada editteks hanya akan tercetak 2. berikut metodnya:
    private void handleNomor(String nomor) {
    // TODO Auto-generated method stub

    String txt = input.getText().toString();
    txt+=nomor;
    input.setText(txt);

    }
    Yang Keempat membuat metode clear untuk membersihkan editteks:
    private void clear(){
    input.setText("");
    }
    Yang Terakhir Memberikan Aksi dan Operasi. Cari metod yang merupakan hasil implementasi dari onClickListener, yaitu public void onClick(View v) dan tambahkan kode berikut didalamnya:
    switch (v.getId()) {
    case R.id.satu:
    handleNomor("1");
    break;
    case R.id.dua:
    handleNomor("2");
    break;
    case R.id.tiga:
    handleNomor("3");
    break;
    case R.id.empat:
    handleNomor("4");
    break;
    case R.id.lima:
    handleNomor("5");
    break;
    case R.id.enam:
    handleNomor("6");
    break;
    case R.id.tujuh:
    handleNomor("7");
    break;
    case R.id.delapan:
    handleNomor("8");
    break;
    case R.id.sembilan:
    handleNomor("9");
    break;
    case R.id.nol:
    handleNomor("0");
    break;
    case R.id.koma:
    handleNomor(".");
    break;
    case R.id.tambah:
    nilai1=Double.parseDouble(input.getText().toString());
    input.setText("");
    operator=1;
    break;
    case R.id.kurang:
    nilai1=Double.parseDouble(input.getText().toString());
    input.setText("");
    operator=2;
    break;
    case R.id.kali:
    nilai1=Double.parseDouble(input.getText().toString());
    input.setText("");
    operator=3;
    break;
    case R.id.bagi:
    nilai1=Double.parseDouble(input.getText().toString());
    input.setText("");
    operator=4;
    break;
    case R.id.samadengan:
    nilai2=Double.parseDouble(input.getText().toString());
    input.setText("");
    if(operator==1){
    input.setText(Double.toString(nilai1+nilai2));
    }else if(operator==2){
    input.setText(Double.toString(nilai1-nilai2));
    }else if(operator==3){
    input.setText(Double.toString(nilai1*nilai2));
    }else if(operator==4){
    input.setText(Double.toString(nilai1/nilai2));
    }
    break;
    case R.id.clear:
    clear();
    break;
    default:
    break;
    }
    Selesai dan hasilnya akan seperti di atas...
    FULL SOURCE CODE
    package org.dharmatin.Calcdroid;

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;

    public class CalcdroidActivity extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    Button satu,dua,tiga,empat,lima,enam,tujuh,delapan,sembilan,
    nol,tambah,kali,kurang,bagi,samadengan,akar,clear,koma;
    EditText input;
    int operator;
    double nilai1 , nilai2;
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    input = (EditText)findViewById(R.id.input1);
    satu = (Button)findViewById(R.id.satu);
    dua = (Button)findViewById(R.id.dua);
    tiga = (Button)findViewById(R.id.tiga);
    empat = (Button)findViewById(R.id.empat);
    lima = (Button)findViewById(R.id.lima);
    enam = (Button)findViewById(R.id.enam);
    tujuh = (Button)findViewById(R.id.tujuh);
    delapan = (Button)findViewById(R.id.delapan);
    sembilan = (Button)findViewById(R.id.sembilan);
    nol = (Button)findViewById(R.id.nol);
    tambah = (Button)findViewById(R.id.tambah);
    kali = (Button)findViewById(R.id.kali);
    kurang = (Button)findViewById(R.id.kurang);
    bagi = (Button)findViewById(R.id.bagi);
    samadengan = (Button)findViewById(R.id.samadengan);
    koma = (Button)findViewById(R.id.koma);
    clear = (Button)findViewById(R.id.clear);

    satu.setOnClickListener(this);
    dua.setOnClickListener(this);
    tiga.setOnClickListener(this);
    empat.setOnClickListener(this);
    lima.setOnClickListener(this);
    enam.setOnClickListener(this);
    tujuh.setOnClickListener(this);
    delapan.setOnClickListener(this);
    sembilan.setOnClickListener(this);
    nol.setOnClickListener(this);
    tambah.setOnClickListener(this);
    kali.setOnClickListener(this);
    kurang.setOnClickListener(this);
    bagi.setOnClickListener(this);
    samadengan.setOnClickListener(this);
    koma.setOnClickListener(this);
    clear.setOnClickListener(this);

    }

    public void onClick(View v) {
    switch (v.getId()) {
    case R.id.satu:
    handleNomor("1");
    break;
    case R.id.dua:
    handleNomor("2");
    break;
    case R.id.tiga:
    handleNomor("3");
    break;
    case R.id.empat:
    handleNomor("4");
    break;
    case R.id.lima:
    handleNomor("5");
    break;
    case R.id.enam:
    handleNomor("6");
    break;
    case R.id.tujuh:
    handleNomor("7");
    break;
    case R.id.delapan:
    handleNomor("8");
    break;
    case R.id.sembilan:
    handleNomor("9");
    break;
    case R.id.nol:
    handleNomor("0");
    break;
    case R.id.koma:
    handleNomor(".");
    break;
    case R.id.tambah:
    nilai1=Double.parseDouble(input.getText().toString());
    input.setText("");
    operator=1;
    break;
    case R.id.kurang:
    nilai1=Double.parseDouble(input.getText().toString());
    input.setText("");
    operator=2;
    break;
    case R.id.kali:
    nilai1=Double.parseDouble(input.getText().toString());
    input.setText("");
    operator=3;
    break;
    case R.id.bagi:
    nilai1=Double.parseDouble(input.getText().toString());
    input.setText("");
    operator=4;
    break;
    case R.id.samadengan:
    nilai2=Double.parseDouble(input.getText().toString());
    input.setText("");
    if(operator==1){
    input.setText(Double.toString(nilai1+nilai2));
    }else if(operator==2){
    input.setText(Double.toString(nilai1-nilai2));
    }else if(operator==3){
    input.setText(Double.toString(nilai1*nilai2));
    }else if(operator==4){
    input.setText(Double.toString(nilai1/nilai2));
    }
    break;
    case R.id.clear:
    clear();
    break;
    default:
    break;
    }


    }
    private void handleNomor(String nomor) {
    // TODO Auto-generated method stub

    String txt = input.getText().toString();
    txt+=nomor;
    input.setText(txt);

    }
    private void clear(){
    input.setText("");
    }
    }

    Dowloan Projectnya di sini
    <
    ===>>>SALAM<<<=====
    -Membuat Calculator Sederhana Di Android

    ShareThis