Eclipse Discount Price App (EDPA)


EDPA:   SOLD       




In this section we learn to design an application that is useful for the general public. Try to develop applications, we start from simple problems in everyday life.

What is the most sought after thing by everyone when shopping? Well! DISCOUNTS!!. No matter how expensive an item is, if it is labeled DISCOUNT!!, people will definitely glance at it. Right?? The problem is, not everyone can calculate the price after being discounted quickly. Well, we as IT people can help these people calculate quickly (besides asking the waiter, hehehe..).


Creating an Android Discount App in Eclipse

Here is a simple idea to create a DISCOUNT application!!. The scenario is when the application is launched, 2 edit texts are provided to enter the initial price and the amount of the discount. Then a button, if clicked, the value of the price after the discount appears. See Figure 9.1 first so you know the final result later. Let's make it right away.


Figure 20.1. Final Result of DISCOUNT application!!

  1. Make a new project first, okay? 
  2. Insert any image icon that represents the DISCOUNT icon. We will make this image a button, save the icon file in the res/drawable folder, name the file diskon.png 
  3. Add resources in Strings.xml as follows.
1: <?xml version="1.0" encoding="utf-8"?>
2: <resources>
3: <string name="hello">HargaDiskon!</string>
4: <string name="app_name">Diskon!</string>
5: <color name="trans">#00330000</color>
6: <drawable name="klik">#CCFFFFFF</drawable>

4. Now let's play coding first in main.xml

1: <?xml version="1.0" encoding="utf-8"?>
2: <ScrollView android:id="@+id/scrollView1"
3: xmlns:android="http://schemas.android.com/apk/res/android"
4: android:layout_width="wrap_content"
5: android:layout_height="wrap_content">
6: <TableLayout android:orientation="vertical"
7: android:layout_height="wrap_content"
8: android:layout_width="fill_parent">
9: <TableRow android:layout_width="fill_parent"
10: android:layout_height="wrap_content"
11: android:id="@+id/tableRow1"
12: android:layout_weight="1">
13: <TextView android:id="@+id/textView1"
14: android:layout_width="fill_parent"
15: android:layout_height="wrap_content"
16: android:text="Harga Asli Rp."></TextView>
17: <EditText android:text=" "
18: android:layout_height="wrap_content"
19: android:id="@+id/harga"
20: android:inputType="numberDecimal"
21: android:layout_width="40dip"></EditText>
22: <TextView android:text=",00"
23: android:id="@+id/textView2"
24: android:layout_width="wrap_content"
25: android:layout_height="wrap_content"></TextView>
26: </TableRow>
27: <TableRow android:layout_width="fill_parent"
28: android:layout_height="wrap_content"
29: android:id="@+id/tableRow1"
30: android:layout_weight="1">
31: <TextView android:id="@+id/textView1"
32: android:layout_width="fill_parent"
33: android:layout_height="wrap_content"
34: android:text="Besar Diskon "></TextView>
35: <EditText android:text=" "
36: android:layout_height="wrap_content"
37: android:id="@+id/diskon"
38: android:inputType="numberDecimal"
39: android:layout_width="50dip"></EditText>
40: <TextView android:text="%"
41: android:id="@+id/textView2"
42: android:layout_width="wrap_content"
43: android:layout_height="wrap_content"></TextView>
44: </TableRow>
45: <View android:background="#FF909090"
46: android:layout_width="wrap_content"
47: android:id="@+id/view1"
48: android:layout_height="2dip"></View>
49: <TextView android:id="@+id/tekshitung"
50: android:layout_width="fill_parent"
51: android:layout_height="wrap_content"
52: android:gravity="center_horizontal"
53: android:text=".:: Hitung ::."></TextView>
54: <View android:background="#FF909090"
55: android:layout_width="wrap_content"
56: android:id="@+id/view1"
57: android:layout_height="2dip"></View>
58: <TableRow android:id="@+id/tableRow2"
59: android:layout_width="wrap_content"
60: android:layout_height="wrap_content">
61: <ImageButton android:layout_width="wrap_content"
62: android:layout_height="wrap_content"
63: android:src="@drawable/diskon"
64: android:id="@+id/imageButton1"
65: android:background="@color/trans"
66: android:paddingTop="10dip"
67: android:paddingBottom="10dip"></ImageButton>
68: <Button android:text="Reset"
69: android:id="@+id/reset"
70: android:layout_marginLeft="50dip"
71: android:layout_width="wrap_content"
72: android:layout_marginTop="7dip"
73: android:gravity="center_vertical|left"
74: android:tag="reset all"
75: android:layout_gravity="left|center_vertical"
76: android:layout_height="wrap_content"></Button>
77: </TableRow>
78: <View android:background="#FF909090"
79: android:layout_width="wrap_content"
80: android:id="@+id/view1"
81: android:layout_height="2dip"></View>
82:
83: <TableRow android:id="@+id/tableRow2"
84: android:layout_width="wrap_content"
85: android:layout_height="wrap_content">
86: <TextView android:id="@+id/textView1"
87: android:layout_width="fill_parent"
88: android:layout_height="wrap_content"
89: android:text="Harga sekarang :"
90: android:maxLines="2"
91: android:textStyle="bold"
92: android:textSize="15dip"></TextView>
93: <TextView android:text=""
94: android:layout_height="wrap_content"
95: android:id="@+id/hasil"
96: android:inputType="numberDecimal"
97: android:textStyle="bold"
98: android:textSize="20dip"
99: android:layout_width="wrap_content"></TextView>
100: </TableRow>
101: <TextView android:layout_width="fill_parent"
102: android:layout_height="wrap_content"
103: android:maxLines="2"
104: android:textStyle="bold"
105: android:textSize="15dip"
106: android:id="@+id/hemat"
107: android:gravity="center_horizontal"
108: android:paddingTop="10dip"></TextView>
109: </TableLayout>
110: </ScrollView>

5. Open the activity then write the following code

1: package com.diskon;
2:
3: import java.text.DecimalFormat;
4:
5: import android.app.Activity;
6: import android.app.AlertDialog;
7: import android.content.DialogInterface;
8: import android.os.Bundle;
9: import android.text.Editable;
10: import android.view.Menu;
11: import android.view.MenuItem;
12: import android.view.View;
13: import android.view.View.OnClickListener;
14: import android.widget.Button;
15: import android.widget.EditText;
16: import android.widget.ImageButton;
17: import android.widget.TextView;
18:
19: public class hargaDiskon extends Activity {
20: EditText harga, diskon;
21: TextView hargabaru, hmt;
22: Editable isiharga, isidiskon;
23: Button reset;
24: ImageButton htng;
25: String sharga, sdiskon;
26:
27: /** Called when the activity is first created. */
28: @Override
29: public void onCreate(Bundle savedInstanceState) {
30: super.onCreate(savedInstanceState);
31: setContentView(R.layout.main);
32:
33: hargabaru = (TextView) findViewById(R.id.hasil);
34: harga = (EditText) findViewById(R.id.harga);
35: diskon = (EditText) findViewById(R.id.diskon);
36: hmt = (TextView) findViewById(R.id.hemat);
37: reset = (Button) findViewById(R.id.reset);
38: reset.setOnClickListener(new reset());
39: htng = (ImageButton) findViewById(R.id.imageButton1);
40: htng.setOnClickListener(new itung());
41:
42: }
43:
44: private class itung implements OnClickListener {
45: public void onClick(View v) {
46: try {
47: Double h =
48: Double.parseDouble(harga.getText().toString());
49: Double d =
50: Double.parseDouble(diskon.getText().toString());
51: Double nd = (d / 100) * h;
52: double hsl = h - nd;
53: Number irit = h - hsl;
54: DecimalFormat df = new DecimalFormat("@@##");
55: hargabaru.setText("Rp." + df.format(hsl) + ",00");
56: hmt.setText("Wow!! Kamu Hemat Rp."
57: + df.format(irit) + " !!");
58: } catch (Exception e) {
59: };
60: }
61: }
62:
63: public class reset implements OnClickListener {
64: public void onClick(View v) {
65: hargabaru.setText("");
66: harga.setText("");
67: diskon.setText("");
68: hmt.setText("");
69: }
70: }
71:
72: public boolean onCreateOptionsMenu(Menu menu) {
73: super.onCreateOptionsMenu(menu);
74: menu.add(0, 0, 0, "Tutorial");
75: menu.add(0, 1, 0, "Info");
76: menu.add(0, 2, 0, "Keluar");
77: return true;
78: }
79:
80: public boolean onOptionsItemSelected(MenuItem item) {
81:
82: if (item.getItemId() == 0) {
83: new AlertDialog.Builder(this)
84: .setTitle("Info Aplikasi")
85: .setMessage("1. Masukkan harga asli"
86: +"\n2. Masukkan besar diskon"
87: +"\n3. Klik gambar HITUNG DISKON!"
88: +"\n4. Klik tombol Reset untuk mengosongkan data")
89: .setNeutralButton("Kembali",
90: new DialogInterface.OnClickListener() {
91: @Override
92: public void onClick(DialogInterface arg0,
93: int arg1) { //
94: // TODO Auto-generated method stub
95: }
96: }).show();
97: }else if (item.getItemId() == 1) {
98: new AlertDialog.Builder(this)
99: .setTitle("Info Aplikasi")
100: .setMessage(
101: "Aplikasi DISKON! dibuat oleh OmAyib2011."
102: + "Kunjungi www.omayib.com.")
103: .setNeutralButton("Kembali",
104: new DialogInterface.OnClickListener() {
105: @Override
106: public void onClick(DialogInterface arg0,
107: int arg1) { //
108: // TODO Auto-generated method stub
109: }}).show();
110: } else if (item.getItemId() == 2) {
111: hargaDiskon.this.finish();
112: }
113: return true;
114: }
115: }

Done! Please RUN it, hopefully there will be no errors.


Post a Comment

Previous Next

نموذج الاتصال