malam sahabat Teknokrat, apa kabar hari ini ??
di kesempatan malam ini SaungIT mau membuat Tutorial membuat kalkulator di android,
aplikasi kalkulator yang temen-temen bisa buat sendiri di hp android temen-temen, dan mudah-mudahan akan menambah pengetahuan tentang pemograman Android temen-temen,,
pasti penasaran nih temen-temen SauingIT :D
Ok, Langsung aja ya..
1, Buka eclipsenya masing" buat project baru ==> File new, android project pada eclipse . temen-temen..
2. Nah Udah Terbentuk Projectnya, Copykan Di ClassUtama.java nya kode berikut :
NB: jika ada error2, tmen-tmen jangan panik dulu, mungkin nama class sama nama package nya ga sama, jadi tmen-tmen tinggal ganti aja sesuai nama project nya,,
package org.gum.mycalculator;
import android.app.Activity;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
public class MyCalculatorActivity extends Activity {
/** Called when the activity is first created. */
private final int BUTTON_DIVIDER=4;
private final int OPERATOR_NULL=0;
private final int OPERATOR_PLUS=1;
private final int OPERATOR_MINUS=2;
private final int OPERATOR_DIVIDE=3;
private final int OPERATOR_MULTIPLY=4;
private final int OPERATOR_EQUAL=5;
EditText editText;
StringBuffer textBuffer;
float recentValue;
int operator;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textBuffer=new StringBuffer("0");
recentValue=0;
operator=OPERATOR_NULL;
editText=(EditText)findViewById(R.id.result);
editText.setText(textBuffer);
WindowManager windowManager=(WindowManager)getSystemService(WINDOW_SERVICE);
Display display=windowManager.getDefaultDisplay();
Button button0,button1,button2,button3,button4,button5,button6,button7,button8,button9;
Button buttonplus,buttonminus,buttonmultiply,buttondivide,buttonequal,buttonclear;
button0=(Button)findViewById(R.id.button0);
setButtonWidth(button0,display.getWidth()/BUTTON_DIVIDER);
button1=(Button)findViewById(R.id.button1);
setButtonWidth(button1,display.getWidth()/BUTTON_DIVIDER);
button2=(Button)findViewById(R.id.button2);
setButtonWidth(button2,display.getWidth()/BUTTON_DIVIDER);
button3=(Button)findViewById(R.id.button3);
setButtonWidth(button3,display.getWidth()/BUTTON_DIVIDER);
button4=(Button)findViewById(R.id.button4);
setButtonWidth(button4,display.getWidth()/BUTTON_DIVIDER);
button5=(Button)findViewById(R.id.button5);
setButtonWidth(button5,display.getWidth()/BUTTON_DIVIDER);
button6=(Button)findViewById(R.id.button6);
setButtonWidth(button6,display.getWidth()/BUTTON_DIVIDER);
button7=(Button)findViewById(R.id.button7);
setButtonWidth(button7,display.getWidth()/BUTTON_DIVIDER);
button8=(Button)findViewById(R.id.button8);
setButtonWidth(button8,display.getWidth()/BUTTON_DIVIDER);
button9=(Button)findViewById(R.id.button9);
setButtonWidth(button9,display.getWidth()/BUTTON_DIVIDER);
buttonplus=(Button)findViewById(R.id.buttonplus);
buttonplus.setWidth(display.getWidth()/BUTTON_DIVIDER);
buttonminus=(Button)findViewById(R.id.buttonminus);
buttonminus.setWidth(display.getWidth()/BUTTON_DIVIDER);
buttonmultiply=(Button)findViewById(R.id.buttonmultiply);
buttonmultiply.setWidth(display.getWidth()/BUTTON_DIVIDER);
buttondivide=(Button)findViewById(R.id.buttondivide);
buttondivide.setWidth(display.getWidth()/BUTTON_DIVIDER);
buttonequal=(Button)findViewById(R.id.buttonequal);
buttonequal.setWidth(display.getWidth()/BUTTON_DIVIDER);
setButtonWidth((Button)findViewById(R.id.buttondot),display.getWidth()/BUTTON_DIVIDER);
setButtonWidth((Button)findViewById(R.id.buttonclear),display.getWidth()/BUTTON_DIVIDER);
}
public void buttonClickHandler(View v){
float tempValue;
boolean isClear=false;
switch(v.getId()){
case R.id.button0:
if(!(textBuffer.length()==1&&textBuffer.toString().equalsIgnoreCase("0")))
textBuffer.append(0);
break;
case R.id.button1:
textBuffer.append(1);
break;
case R.id.button2:
textBuffer.append(2);
break;
case R.id.button3:
textBuffer.append(3);
break;
case R.id.button4:
textBuffer.append(4);
break;
case R.id.button5:
textBuffer.append(5);
break;
case R.id.button6:
textBuffer.append(6);
break;
case R.id.button7:
textBuffer.append(7);
break;
case R.id.button8:
textBuffer.append(8);
break;
case R.id.button9:
textBuffer.append(9);
break;
case R.id.buttondot:
textBuffer.append('.');
break;
case R.id.buttonplus:
tempValue=Float.parseFloat(textBuffer.toString());
textBuffer.delete(0, textBuffer.length());
setTextBufferByOperator(tempValue);
operator=OPERATOR_PLUS;
isClear=true;
break;
case R.id.buttonminus:
tempValue=Float.parseFloat(textBuffer.toString());
textBuffer.delete(0, textBuffer.length());
setTextBufferByOperator(tempValue);
operator=OPERATOR_MINUS;
isClear=true;
break;
case R.id.buttondivide:
tempValue=Float.parseFloat(textBuffer.toString());
textBuffer.delete(0, textBuffer.length());
setTextBufferByOperator(tempValue);
operator=OPERATOR_DIVIDE;
isClear=true;
break;
case R.id.buttonmultiply:
tempValue=Float.parseFloat(textBuffer.toString());
textBuffer.delete(0, textBuffer.length());
setTextBufferByOperator(tempValue);
operator=OPERATOR_MULTIPLY;
isClear=true;
break;
case R.id.buttonequal:
tempValue=Float.parseFloat(textBuffer.toString());
textBuffer.delete(0, textBuffer.length());
setTextBufferByOperator(tempValue);
operator=OPERATOR_EQUAL;
isClear=true;
break;
case R.id.buttonclear:
textBuffer.delete(0, textBuffer.length());
textBuffer.append(0);
operator=OPERATOR_NULL;
recentValue=0;
}
if(textBuffer.length()>1&&textBuffer.charAt(1)!='.'&&textBuffer.charAt(0)=='0')
textBuffer.deleteCharAt(0);
editText.setText(textBuffer);
if(isClear){
textBuffer.delete(0, textBuffer.length());
textBuffer.append(0);
}
}
private void setTextBufferByOperator(float value){
switch(operator){
case OPERATOR_NULL:
recentValue=recentValue+value;
break;
case OPERATOR_PLUS:
recentValue=recentValue+value;
break;
case OPERATOR_MINUS:
recentValue=recentValue-value;
break;
case OPERATOR_DIVIDE:
recentValue=recentValue/value;
break;
case OPERATOR_MULTIPLY:
recentValue=recentValue*value;
break;
case OPERATOR_EQUAL:
operator=OPERATOR_NULL;
break;
}
textBuffer.append(recentValue);
if(textBuffer.charAt(textBuffer.length()-2)=='.'&&textBuffer.charAt(textBuffer.length()-1)=='0')
textBuffer.delete(textBuffer.length()-2, textBuffer.length());
}
private void setButtonWidth(Button button,int buttonWidth){
button.setWidth(buttonWidth);
}
}
3. tahap selanjutnya, pada main.xml di layout temen temen copykan Kode berikut :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
style="@style/layoutff"
>
<EditText android:id="@+id/result"
style="@style/layoutfw"
android:enabled="false"
android:gravity="right"
/>
<LinearLayout
style="@style/layoutbuttoncontainer">
<Button android:id="@+id/buttonclear"
style="@style/layoutbutton"
android:text="C"/>
</LinearLayout>
<LinearLayout
style="@style/layoutbuttoncontainer">
<Button android:tag="7"
android:id="@+id/button7"
style="@style/layoutbutton"
android:text="7"
/>
<Button android:tag="8"
android:id="@+id/button8"
style="@style/layoutbutton"
android:text="8"
/>
<Button android:tag="9"
android:id="@+id/button9"
style="@style/layoutbutton"
android:text="9"
/>
<Button android:id="@+id/buttondivide"
style="@style/layoutbutton"
android:text="/"/>
</LinearLayout>
<LinearLayout
style="@style/layoutbuttoncontainer">
<Button android:id="@+id/button4"
style="@style/layoutbutton"
android:text="4"/>
<Button android:id="@+id/button5"
style="@style/layoutbutton"
android:text="5"/>
<Button android:id="@+id/button6"
style="@style/layoutbutton"
android:text="6"/>
<Button android:id="@+id/buttonmultiply"
style="@style/layoutbutton"
android:text="*"/>
</LinearLayout>
<LinearLayout
style="@style/layoutbuttoncontainer">
<Button android:id="@+id/button1"
style="@style/layoutbutton"
android:text="1"/>
<Button android:id="@+id/button2"
style="@style/layoutbutton"
android:text="2"/>
<Button android:id="@+id/button3"
style="@style/layoutbutton"
android:text="3"/>
<Button android:id="@+id/buttonminus"
style="@style/layoutbutton"
android:text="-"/>
</LinearLayout>
<LinearLayout
style="@style/layoutbuttoncontainer">
<Button android:id="@+id/button0"
style="@style/layoutbutton"
android:text="0"/>
<Button android:id="@+id/buttondot"
style="@style/layoutbutton"
android:text="."/>
<Button android:id="@+id/buttonplus"
style="@style/layoutbutton"
android:text="+"/>
<Button android:id="@+id/buttonequal"
style="@style/layoutbutton"
android:text="="/>
</LinearLayout>
</LinearLayout>
Mudah kan, :D Nah klo udah terCopy semua temen-temen bisa langsung Run aplikasi nya..
dan hasilnya akan seperti gambar di bawah ini :
salam Teknokrat..
maantapp kakaksss salam teknokrat