Anda di halaman 1dari 20

Tutorial Android : Membuat Kalkulator Biner-Desimal

Sederhana di Android

Kondisi tubuh yang kurang fit melanda saya mulai tadi pagi,
gara-gara kemarin malam saya paksakan untuk begadang di ITSolution demi menyeselesaikan
jurnal. Tidur jam setengah 4 dan bangun lagi jam setengah 8 untuk berangkat kuliah yang
jadwalnya sangat-sangat nanggung nyelempit di akhir pekan seperti ini. Belum lagi rasa rindu
untuk pulang ke denpasar di pertengahan bulan seperti ini, namun apa daya pekerjaan dan tugas
kampus yang masih numpuk minta giliran untuk di-action. Apalagi mata kuliah machine learning
yang diajarkan oleh pak hadiq, yang selalu menjadi momok setiap kali seliweran di pikiran saya :(.
Yang terpenting di bawa hepi+santai ajalah, bila perlu tugasnya gak usah dikumpulin, dijamin
tambah santai :ngakaks:. Nah, sambil mengisi waktu kosong di malam yang hawanya panas ini,
saya akan membagikan sedikit tutorial untuk membuat sebuah kalkulator biner sederhana di
android. Yuppzzz, memang sudah banyak tutorial yang berseliweran di internet yang membahas
tentang cara konversi biner ke desimal maupun sebaliknya. Berhubung saya lagi sedang senangsenangnya membuat aplikasi sederhana di platform android, tak ada salahnya untuk dicoba.
Hitung-hitung cari pengalaman+nambah postingan :p.
Yang terpenting di aplikasi kalkulator ini hanya proses perhitungannya saja, untuk event tombol
cuma sekedar pemanis. Bagi yang sudah pernah membuat versi java-nya, syntaxnya sama saja.
Tinggal mengubah atau menambahkan desain form bagi yang awalnya hanya berupa tampilan
console. OK deh, kita langsung aja ke cara pembuatannya. Cekidot mas berooo
1. Seperti biasa, buat android project baru terlebih dahulu.

2. Disini saya menggunakan 2 activity, yaitu KalkulatorBinerActivity untuk melakukan konversi dari
desimal ke biner dan KalkulatorDesimalActivity untuk melakukan konveri dari biner ke desimal.
Kodenya seperti di bawah ini :
KalkulatorBinerActivity.java

1 public class KalkulatorBinerActivity extends Activity {


2
/** Called when the activity is first created. */
3
EditText t_in,t_out;
4
Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b_hps,b_ke_desimal,b_biner;
5
String var_in="";
6
@Override
7
public void onCreate(Bundle savedInstanceState) {
8
super.onCreate(savedInstanceState);
9
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutPara
10
ms.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
11
setContentView(R.layout.main);
12
13
t_in = (EditText) findViewById(R.id.txt_in);
14
t_out = (EditText) findViewById(R.id.txt_out);
15
b0 = (Button) findViewById(R.id.btn0);
16
b1 = (Button) findViewById(R.id.btn1);
17
b2 = (Button) findViewById(R.id.btn2);
18
b3 = (Button) findViewById(R.id.btn3);
19
b4 = (Button) findViewById(R.id.btn4);
20
b5 = (Button) findViewById(R.id.btn5);
21
b6 = (Button) findViewById(R.id.btn6);
22
b7 = (Button) findViewById(R.id.btn7);
23
b8 = (Button) findViewById(R.id.btn8);
24
b9 = (Button) findViewById(R.id.btn9);
25
b_hps = (Button) findViewById(R.id.btn_hapus);

26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76

b_ke_desimal = (Button) findViewById(R.id.btn_ke_desimal);


b_biner = (Button) findViewById(R.id.btn_biner);
b1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(var_in.trim().equals(""))
{
var_in = "1";
t_in.setText(var_in);
}
else
{
var_in = t_in.getText().toString()+"1";
t_in.setText(var_in);
}
}
});
b2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(var_in.trim().equals(""))
{
var_in = "2";
t_in.setText(var_in);
}
else
{
var_in = t_in.getText().toString()+"2";
t_in.setText(var_in);
}
}
});
b3.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(var_in.trim().equals(""))
{
var_in = "3";
t_in.setText(var_in);
}
else
{
var_in = t_in.getText().toString()+"3";
t_in.setText(var_in);
}
}
});
b4.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(var_in.trim().equals(""))

77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128

{
var_in = "4";
t_in.setText(var_in);
}
else
{
var_in = t_in.getText().toString()+"4";
t_in.setText(var_in);
}
}
});
b5.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(var_in.trim().equals(""))
{
var_in = "5";
t_in.setText(var_in);
}
else
{
var_in = t_in.getText().toString()+"5";
t_in.setText(var_in);
}
}
});
b6.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(var_in.trim().equals(""))
{
var_in = "6";
t_in.setText(var_in);
}
else
{
var_in = t_in.getText().toString()+"6";
t_in.setText(var_in);
}
}
});
b7.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(var_in.trim().equals(""))
{
var_in = "7";
t_in.setText(var_in);
}
else
{
var_in = t_in.getText().toString()+"7";

129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179

t_in.setText(var_in);
}
}
});
b8.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(var_in.trim().equals(""))
{
var_in = "8";
t_in.setText(var_in);
}
else
{
var_in = t_in.getText().toString()+"8";
t_in.setText(var_in);
}
}
});
b9.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(var_in.trim().equals(""))
{
var_in = "9";
t_in.setText(var_in);
}
else
{
var_in = t_in.getText().toString()+"9";
t_in.setText(var_in);
}
}
});
b0.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(var_in.trim().equals(""))
{
var_in = "0";
t_in.setText(var_in);
}
else
{
var_in = t_in.getText().toString()+"0";
t_in.setText(var_in);
}
}
});
b_hps.setOnClickListener(new OnClickListener() {

180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228

public void onClick(View v) {


t_in.setText("");
t_out.setText("");
}
});
b_biner.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(var_in!="")
{
String hasil =
DesimalKeBiner(Integer.parseInt(var_in));
t_out.setText(hasil);
var_in="";
}
}
});
b_ke_desimal.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
t_in.setText("");
t_out.setText("");
var_in="";
Intent i = new Intent(getApplicationContext(),
KalkulatorDesimalActivity.class);
startActivity(i);
}
});
}
public String DesimalKeBiner(int angka_desimal)
{
int sisa_bagi=0, i=0;
boolean ulang=true;
int[] arr_simpan = new int[10000];
sisa_bagi=angka_desimal;
String hasil="";
while (ulang==true)
{
sisa_bagi = angka_desimal%2;
angka_desimal= angka_desimal/2;
arr_simpan[i]=sisa_bagi;
i++;
if((angka_desimal==0)||(angka_desimal==1))
{
ulang=false;
arr_simpan[i]=angka_desimal;
}
}

229
230
231
232
233
234
235

for (int a=i; a>=0;a--)


{
hasil += arr_simpan[a];
}
return hasil;
}

236
237
238
239
240
241

public boolean onCreateOptionsMenu(Menu menu) {


MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.opt_menu, menu);

242

public boolean onOptionsItemSelected(MenuItem item) {

return true;
}

243

switch (item.getItemId()) {

244
245
246
247

case R.id.tentang:
AlertDialog alertDialog;

alertDialog = new AlertDialog.Builder(this).create();


alertDialog.setTitle("Sekilas Biner");
alertDialog.setMessage("Bilangan biner adalah salah
248
satu sistem bilangan yang digunakan pada komputer. " +
"Berbeda dengan sistem bilangan desimal yang
249
sering kita gunakan sehari-hari, " +
"sistem bilangan biner hanya menggunakan dua
250
bilangan yaitu 0 (nol) dan 1 (satu). " +
"\n\nSalam, Gede
251
Lumbung\nhttp://gedelumbung.com");
25
alertDialog.setButton("#OKOK", newDialogInterface.OnCli
2 ckListener() {
253
@Override
254
255
256
257
258
259

public void onClick(DialogInterface dialog, int


which) {
dialog.dismiss();
}
});
alertDialog.show();

return true;
260
case R.id.keluar:
26
Intent exit = new Intent(Intent.ACTION_MAIN);
1
26
exit.addCategory(Intent.CATEGORY_HOME);exit.setFlags(Int
2 ent.FLAG_ACTIVITY_NEW_TASK);
263
KalkulatorBinerActivity.this.finish();
264
startActivity(exit);
265
return true;
266
default:
267
return super.onOptionsItemSelected(item);
268
}
269
}
270
}
KalkulatorDesimalActivity.java

1 public class KalkulatorDesimalActivity extends Activity {


2
/** Called when the activity is first created. */
3
EditText t_in,t_out;
4
Button b0,b1,b_hps,b_ke_biner,b_desimal;
5
String var_in="";
6
@Override
7
public void onCreate(Bundle savedInstanceState) {
8
super.onCreate(savedInstanceState);
9
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutPara
10
ms.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
11
setContentView(R.layout.second);
12
13
t_in = (EditText) findViewById(R.id.txt_in);
14
t_out = (EditText) findViewById(R.id.txt_out);
15
b0 = (Button) findViewById(R.id.btn0);
16
b1 = (Button) findViewById(R.id.btn1);
17
b_hps = (Button) findViewById(R.id.btn_hapus);
18
b_ke_biner = (Button) findViewById(R.id.btn_ke_biner);
19
b_desimal = (Button) findViewById(R.id.btn_desimal);
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

b1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(var_in.trim().equals(""))
{
var_in = "1";
t_in.setText(var_in);
}
else
{
var_in = t_in.getText().toString()+"1";
t_in.setText(var_in);
}
}
});
b0.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(var_in.trim().equals(""))
{
var_in = "0";
t_in.setText(var_in);
}
else
{
var_in = t_in.getText().toString()+"0";
t_in.setText(var_in);
}
}
});

51

b_hps.setOnClickListener(new OnClickListener() {

52
53
54
55
56
57

public void onClick(View v) {


t_in.setText("");
t_out.setText("");
}
});

58
59
60
61
62
63
64
65
66
67
68
69

b_desimal.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(var_in!="")
{
int hasil =
BinerKeDesimal(Integer.parseInt(var_in));
t_out.setText(Double.toString(hasil));
var_in="";
}
}
});

70

b_ke_biner.setOnClickListener(new OnClickListener() {

71
72
73
74
75
76

public void onClick(View v) {


KalkulatorDesimalActivity.this.finish();
}
});
}

77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100

public int BinerKeDesimal(int angka_desimal)


{
String bil = Integer.toString(angka_desimal);
int[] arr_simpan= new int[10000];
String[] str= new String[10000];
double hasil=0;
int hasil_akhir=0;
int a=0;
char t;
for(int i=0;i < bil.length();i++)
{
t = bil.charAt(i);
str[i]=Character.toString(t);
}
for(int i=bil.length()-1; i>=0;i--)
{
arr_simpan[a]=Integer.parseInt(str[i]);
hasil= arr_simpan[a]* (Math.pow(2,a));
hasil_akhir=hasil_akhir+(int)hasil;
a++;
}
return hasil_akhir;

101
102

103
104
105
106
107
108

public boolean onCreateOptionsMenu(Menu menu) {


MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.opt_menu, menu);

109

public boolean onOptionsItemSelected(MenuItem item) {

return true;
}

110

switch (item.getItemId()) {

111
112
113
114

case R.id.tentang:
AlertDialog alertDialog;

alertDialog = new AlertDialog.Builder(this).create();


alertDialog.setTitle("Sekilas Biner");
alertDialog.setMessage("Bilangan biner adalah salah
115
satu sistem bilangan yang digunakan pada komputer. " +
"Berbeda dengan sistem bilangan desimal yang
116
sering kita gunakan sehari-hari, " +
"sistem bilangan biner hanya menggunakan dua
117
bilangan yaitu 0 (nol) dan 1 (satu). " +
"\n\nSalam, Gede
118
Lumbung\nhttp://gedelumbung.com");
11
alertDialog.setButton("#OKOK", newDialogInterface.OnCli
9 ckListener() {
120
@Override
121
122
123
124
125
126
127

which) {

public void onClick(DialogInterface dialog, int


dialog.dismiss();
}
});
alertDialog.show();
return true;
case R.id.keluar:

128
Intent exit = new Intent(Intent.ACTION_MAIN);
12
exit.addCategory(Intent.CATEGORY_HOME);exit.setFlags(Int
9 ent.FLAG_ACTIVITY_NEW_TASK);
13
KalkulatorDesimalActivity.this.finish();
0
131
startActivity(exit);
132
return true;
133
default:
134
return super.onOptionsItemSelected(item);
135
}
136
}
137
}
3. Karena menggunakan 2 buah activity, otomatis juga membutuhkan 2 buah layout yang berbeda.
Saya tambahkan satu layout untuk layout options menu, saya tempatkan pada
folder menu/opt_menu.xml. Jadi total terdapat 3 buah layout.
main.xml

1 <?xml version="1.0" encoding="utf-8"?>


2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

3
4
5
6
7
8
9
10
11
12

android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dip" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="horizontal" >

13
14
15
16
17
18
19
20
21
22

<TextView
android:layout_width="68dp"
android:layout_height="wrap_content"
android:gravity="bottom"
android:singleLine="true"
android:text="Input : "
android:textSize="18sp"
android:textStyle="normal"

23
24
25
26
27
28
29
30
31
32
33
34

<EditText
android:id="@+id/txt_in"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:editable="false"
android:gravity="bottom"
android:singleLine="true"
android:textSize="30sp"
android:textStyle="normal"

35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

android:typeface="normal" />

android:typeface="normal" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="horizontal" >
<TextView
android:layout_width="68dp"
android:layout_height="wrap_content"
android:gravity="bottom"
android:singleLine="true"
android:text="Output : "
android:textSize="18sp"
android:textStyle="normal"
android:typeface="normal" />

54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

<EditText
android:id="@+id/txt_out"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:editable="false"
android:gravity="bottom"
android:singleLine="true"
android:textSize="30sp"
android:textStyle="normal"
android:typeface="normal" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="horizontal" >

73
74
75
76
77
78
79
80
81
82
83

<Button
android:id="@+id/btn7"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:padding="0px"
android:text="7"
android:textSize="24sp"

84
85
86
87
88
89
90
91
92
93
94

<Button
android:id="@+id/btn8"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:padding="0px"
android:text="8"
android:textSize="24sp"

95
96
97
98
99
100
101
102
103
104
105

android:textStyle="bold" />

android:textStyle="bold" />
<Button
android:id="@+id/btn9"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:padding="0px"
android:text="9"
android:textSize="24sp"
android:textStyle="bold" />

106
107
108
109
110
111
112

</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="horizontal" >

113
114
115
116
117
118
119
120
121
122
123

<Button
android:id="@+id/btn4"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:padding="0px"
android:text="4"
android:textSize="24sp"

124
125
126
127
128
129
130
131
132
133
134

<Button
android:id="@+id/btn5"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:padding="0px"
android:text="5"
android:textSize="24sp"

135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157

android:textStyle="bold" />

android:textStyle="bold" />
<Button
android:id="@+id/btn6"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:padding="0px"
android:text="6"
android:textSize="24sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="horizontal" >
<Button
android:id="@+id/btn1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"

158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209

android:layout_weight="1"
android:gravity="center"
android:padding="0px"
android:text="1"
android:textSize="24sp"
android:textStyle="bold" />
<Button
android:id="@+id/btn2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:padding="0px"
android:text="2"
android:textSize="24sp"
android:textStyle="bold" />
<Button
android:id="@+id/btn3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:padding="0px"
android:text="3"
android:textSize="24sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="horizontal" >
<Button
android:id="@+id/btn0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_span="2"
android:layout_weight="1"
android:gravity="center"
android:padding="0px"
android:text="0"
android:textSize="23sp"
android:textStyle="bold" />
<Button
android:id="@+id/btn_hapus"
android:layout_width="fill_parent"
android:layout_height="fill_parent"

210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248

android:layout_weight="1"
android:gravity="center"
android:padding="0px"
android:text="#HAPUS"
android:textSize="23sp"
android:textStyle="bold" />
<Button
android:id="@+id/btn_biner"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:padding="0px"
android:text="#OKOK"
android:textSize="23sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="horizontal" >
<Button
android:id="@+id/btn_ke_desimal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_span="2"
android:layout_weight="1"
android:gravity="center"
android:padding="0px"
android:text="Biner ke Desimal"
android:textSize="23sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>

second.xml

1 <?xml version="1.0" encoding="utf-8"?>


2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3
android:layout_width="fill_parent"
4
android:layout_height="fill_parent"
5
android:orientation="vertical"
6
android:padding="10dip" >
7
8
<LinearLayout
9
android:layout_width="fill_parent"
10
android:layout_height="fill_parent"
11
android:layout_weight="1"

12

android:orientation="horizontal" >

13
14
15
16
17
18
19
20
21
22

<TextView
android:layout_width="68dp"
android:layout_height="wrap_content"
android:gravity="bottom"
android:singleLine="true"
android:text="Input : "
android:textSize="18sp"
android:textStyle="normal"

23
24
25
26
27
28
29
30
31
32
33
34

<EditText
android:id="@+id/txt_in"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:editable="false"
android:gravity="bottom"
android:singleLine="true"
android:textSize="30sp"
android:textStyle="normal"

35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63

android:typeface="normal" />

android:typeface="normal" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="horizontal" >
<TextView
android:layout_width="68dp"
android:layout_height="wrap_content"
android:gravity="bottom"
android:singleLine="true"
android:text="Output : "
android:textSize="18sp"
android:textStyle="normal"
android:typeface="normal" />
<EditText
android:id="@+id/txt_out"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:editable="false"
android:gravity="bottom"
android:singleLine="true"
android:textSize="30sp"
android:textStyle="normal"

64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114

android:typeface="normal" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="horizontal" >
<Button
android:id="@+id/btn0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:padding="0px"
android:text="0"
android:textSize="24sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="horizontal" >
<Button
android:id="@+id/btn1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:padding="0px"
android:text="1"
android:textSize="24sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="horizontal" >
<Button
android:id="@+id/btn_hapus"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"

115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152

android:gravity="center"
android:padding="0px"
android:text="#HAPUS"
android:textSize="23sp"
android:textStyle="bold" />
<Button
android:id="@+id/btn_desimal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:padding="0px"
android:text="#OKOK"
android:textSize="23sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="horizontal" >
<Button
android:id="@+id/btn_ke_biner"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_span="2"
android:layout_weight="1"
android:gravity="center"
android:padding="0px"
android:text="Desimal ke Biner"
android:textSize="23sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>

opt_menu.xml

1 <menu xmlns:android="http://schemas.android.com/apk/res/android">
2
<item android:id="@+id/tentang"
3
android:icon="@drawable/tentang"
4
android:title="Apa itu Biner?" />
5
<item android:id="@+id/keluar"
6
android:icon="@drawable/logout"
7
android:title="Keluar | Metu" />
8 </menu>
4. Yang terakhir, kita perlu mendaftarkan activity yang kedua di dalam file AndroidManifest.xml.

1 <?xml version="1.0" encoding="utf-8"?>

2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
3
package="dlmbg.pckg.kalkulator.biner"
4
android:versionCode="1"
5
android:versionName="1.0" >
6
7
8
9
10
11
12
13
14
15
16

<uses-sdk android:minSdkVersion="10" />


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

17
18
19
20
21
22
23
24

<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="KalkulatorDesimalActivity"android:label="Kalkulator
Desimal"></activity>
</application>
</manifest>

5. Jalankan aplikasi via emulator, bisa juga dijalanlan langsung ke smartphone dengan
menghubungkan via kabel usb (syaratnya, driver harus sudah terinstall)

Nahh, gampang kan cara pembuatannya??? Kalkulator di atas masih bisa ditambahkan dengan
fungsi untuk konversi ke hexadesimal dan oktal. Bisa juga ditambahkan tabel gerbang logika,
jadinya ketika rekan-rekan sedang mengerjakan ujian mata kuliah sistem digital pasti terbantu
(kalau dibolehkan bawa hape lho yaw :D). OK deh, sekian dulu postingan saya kali ini. Semoga bisa
bermanfaat untuk kita semua.
Happy Blogging and Keep Coding

Anda mungkin juga menyukai