UAS Mobile Programming
Aplikasi menu makanan
A.Deskripsi
Aplikasi ini dibuat untuk memudahkan konsumen memilih menu-menu makanan yang ada di suatu kedai makanan sekaligus melihat harga masing-masing menu di sertai dengan berapa biaya makananan yg kita pesan,
apliksi ini dibuat dengan sistem cloud, di sini saya menggunakan dropbox untuk menyimpan image menu-menu tersebut, yang artinya harus terkoneksi dengan internet
B. Design dan Source Code
- Login
- Design :
- Activity_main.xml :
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="256dp"
android:ems="10"
android:inputType="textPersonName"
android:text="Username"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword"
app:layout_constraintEnd_toEndOf="@+id/username"
app:layout_constraintTop_toBottomOf="@+id/username" />
<Button
android:id="@+id/btnLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="60dp"
android:layout_marginLeft="60dp"
android:layout_marginTop="52dp"
android:onClick="login"
android:text="Login"
app:layout_constraintStart_toStartOf="@+id/password"
app:layout_constraintTop_toBottomOf="@+id/password" />
- MainActivity.java :
package com.Rezapratama.nim161021450158;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView username, password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
username = (TextView) findViewById(R.id.username);
password = (TextView) findViewById(R.id.password);
}
public void login(View view) {
Intent intent = new Intent(getBaseContext(), ListView.class);
startActivity(intent);
}
}
2. Halaman Gridview ( Dashboard )
- Design :
- Activity_main.xml :
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipeMenu"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/total"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Total : Rp. 0"
android:textAlignment="center"
android:textSize="20sp"
android:textStyle="bold" />
<GridView
android:id="@+id/gridViewMenu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:horizontalSpacing="10.0dip"
android:numColumns="2"
android:stretchMode="columnWidth"
android:verticalSpacing="0.0dip" />
</LinearLayout>
- Gridview layout.xml : di dalam layout ini terdapat komponen imageview utk menampilan gambar2 menu, textview untuk menampilkan harga menu, dan terakhir progressbar utk handle ketika load gambarnya lama.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/imgView"
android:layout_width="match_parent"
android:layout_height="200sp"
android:scaleType="centerCrop" />
<TextView
android:id="@+id/txtGrid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#FFDFDEDD"
android:maxLines="1"
android:paddingBottom="15dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="15dp"
android:textColor="@color/colorAccent"
android:textStyle="bold" />
<ProgressBar
android:id="@+id/pBarGrid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</FrameLayout>
- Listview.Java
package com.Rezapratama.nim161021450158;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
public class ListView extends AppCompatActivity {
private static final String urlListMenu = "https://www.dropbox.com/s/rnpv3j15nz8t1f8/menumakanan.xml?dl=1";
private List<Item> listData;
private GridView gridView;
private TextView txtTotal;
private SwipeRefreshLayout swipe;
private int totalHarga = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
txtTotal = (TextView) findViewById(R.id.total);
gridView = (GridView) findViewById(R.id.gridViewMenu);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
popUpItem(listData.get(position));
}
});
if (listData == null) {
new MyTask().execute(urlListMenu);
}
swipe = (SwipeRefreshLayout) findViewById(R.id.swipeMenu);
swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
txtTotal.setText("Total : Rp. 0");
new MyTask().execute(urlListMenu);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.checkout:
new MyTask().execute(urlListMenu);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
class MyTask extends AsyncTask<String, Void, Void> {
ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(ListView.this);
pDialog.setMessage("Loading Menu");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(String... params) {
listData = getData(params[0]);
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (null != pDialog && pDialog.isShowing()) {
pDialog.dismiss();
}
swipe.setRefreshing(false);
if (null == listData || listData.size() == 0) {
Toast.makeText(ListView.this, "Menu Tidak Ditemukan", Toast.LENGTH_LONG);
} else {
gridView.setAdapter(new CustomAdapterGridview(ListView.this, R.layout.gridview_layout, listData));
}
setTotal(0);
}
}
public List<Item> getData(String url) {
Item objItem;
List<Item> listItem = null;
try {
listItem = new ArrayList<>();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new URL(url).openStream());
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("item");
int batas = nList.getLength();
for (int temp = 0; temp < batas; temp++) {
{
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
objItem = new Item();
objItem.setNama(getTagValue("nama", eElement));
objItem.setHarga(getTagValue("harga", eElement));
objItem.setLink(getTagValue("link", eElement));
listItem.add(objItem);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return listItem;
}
private static String getTagValue(String sTag, Element eElement) {
NodeList nlList = eElement.getElementsByTagName(sTag).item(0)
.getChildNodes();
Node nValue = nlList.item(0);
return nValue.getNodeValue();
}
private void popUpItem(final Item data) {
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(ListView.this);
LayoutInflater inflater = getLayoutInflater();
View convertView = inflater.inflate(R.layout.item_layout, null);
ImageView imgItem = (ImageView) convertView.findViewById(R.id.imgItem);
TextView txtDesc = (TextView) convertView.findViewById(R.id.txtDesc);
final EditText txtJml = (EditText) convertView.findViewById(R.id.txtJml);
Glide.with(this).load(data.getLink()).into(imgItem);
txtDesc.setText(data.getNama() + " | Rp." + data.getHarga());
alertDialog.setView(convertView).setTitle("");
final AlertDialog mAlertDialog = alertDialog.setPositiveButton("PESAN", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
int pesanan = Integer.valueOf(data.getHarga());
if (!txtJml.getText().toString().equals("") && !txtJml.getText().toString().equals("0")) {
pesanan = Integer.valueOf(txtJml.getText().toString()) * Integer.valueOf(data.getHarga());
}
setTotal(pesanan);
}
}).create();
mAlertDialog.show();
}
private void setTotal(int nilai) {
totalHarga = totalHarga + nilai;
txtTotal.setText("Total : Rp. " + totalHarga);
}
}
3. Halaman Menu Makanan
- Design
- item_layout.xml : dalam layout ini terdapat komponen imageview utk nampilin gambar, textview untuk nampilin nama dan harga menu, serta edittext untuk menampung jumlah porsi yang akan di pesan pelanggan.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/imgItem"
android:layout_width="match_parent"
android:layout_height="200sp"
android:scaleType="centerCrop" />
<TextView
android:id="@+id/txtDesc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Sate Ayam | Rp. 10.000"
android:textAlignment="center"
android:textSize="20sp"
android:textStyle="bold" />
<EditText
android:id="@+id/txtJml"
android:hint="Masukkan Jumlah"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:textAlignment="center" />
</LinearLayout>
- Item.java : class object menu atau yang bisa kita sebut setter getternya yang berisi nama, harga, dan link dimana nama adalah nama menu, harga adalah harga menu, dan terakhir link adalah link/url gambar menu.
package com.Rezapratama.nim161021450158;
public class Item {
private String nama;
private String harga;
private String link;
public String getNama() {
return nama;
}
public void setNama(String nama) {
this.nama = nama;
}
public String getHarga() {
return harga;
}
public void setHarga(String harga) {
this.harga = harga;
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
}
- CustomAdapterGridView.java : untuk meng-handle gridview yang akan menampilan menu2 yang ada.
package com.Rezapratama.nim161021450158;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.Rezapratama.nim161021450158.R;
import java.util.List;
public class CustomAdapterGridview extends ArrayAdapter<Item> {
private List<Item> items;
private Item objBean;
private Activity activity;
private int row;
public CustomAdapterGridview(Activity ctx, int resource, List<Item> itm) {
super(ctx, resource, itm);
this.row = resource;
this.activity = ctx;
this.items = itm;
}
public View getView(int paramInt, View paramView, ViewGroup paramViewGroup) {
View view = paramView;
ViewHolder item;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(row, null);
item = new ViewHolder();
view.setTag(item);
} else {
item = (ViewHolder) view.getTag();
}
if ((items == null) || ((paramInt + 1) > items.size()))
return view;
objBean = items.get(paramInt);
item.name = (TextView) view.findViewById(R.id.txtGrid);
item.imgName = (ImageView) view.findViewById(R.id.imgView);
item.pbar = (ProgressBar) view.findViewById(R.id.pBarGrid);
if (item.name != null && null != objBean.getNama()
&& objBean.getNama().trim().length() > 0) {
item.name.setText(Html.fromHtml(objBean.getNama() + " | Rp. " + objBean.getHarga()));
}
if (item.imgName != null) {
String url = objBean.getLink();
final ProgressBar pbar = item.pbar;
if (null != url && url.trim().length() > 0) {
pbar.setVisibility(View.VISIBLE);
Glide.with(activity).load(url).asBitmap().placeholder(R.mipmap.ic_launcher)
.into(new BitmapImageViewTarget(item.imgName) {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
super.onResourceReady(resource, glideAnimation);
pbar.setVisibility(View.GONE);
}
});
} else {
item.imgName.setImageResource(R.mipmap.ic_launcher);
}
}
return view;
}
private class ViewHolder {
TextView name;
ImageView imgName;
ProgressBar pbar;
}
}
4. Konfigurasi Dropbox
<item>
<nama>Domino</nama>
<harga>10000</harga>
<link>https://www.dropbox.com/s/sxrq1l7gm94j0ve/domino.jpg?dl=1</link>
</item>
<item>
<nama>KFC</nama>
<harga>15000</harga>
<link>https://www.dropbox.com/s/92838auxcdgx4cz/kfc.jpg?dl=1</link>
</item>
<item>
<nama>Nasgor Gila</nama>
<harga>15000</harga>
<link>https://www.dropbox.com/s/tpcrmghlmpdsou2/nasgorgila.jpg?dl=1</link>
</item>
<item>
<nama>Pecel Ayam</nama>
<harga>15000</harga>
<link>https://www.dropbox.com/s/kzkil675z30ctmd/pecelayam.jpg?dl=1</link>
</item>
<item>
<nama>Pizza</nama>
<harga>25000</harga>
<link>https://www.dropbox.com/s/bumvupr0wibdj14/pizza.png?dl=1</link>
</item>
<item>
<nama>Sate Ayam</nama>
<harga>20000</harga>
<link>https://www.dropbox.com/s/5cdu7avium4n0oq/sateayam.jpg?dl=1</link>
</item>
</data>
- AndroidManfiest.xml : tambahkan permission internet untuk bisa menggunakan koneksi internet di android
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.Rezapratama.nim161021450158">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="Rezapratama-161021450158"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".ListView" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
C. Test Aplikasi
- Halaman Login
2. Setelah login sukses , selanjutnya masuk dashboard Gridview
3. Selanjutnya kita bisa memilih salah satu makanan yang akan di buka
4. setelah menu terbuka, kita tinggal memesan berapa jumlah makanan yang akan di pesan maka ketika kembali ke dashboard jumlah yang akan di bayar otomatis akan
muncul di Topbar
sekian tentang pembuatan aplikasi ini, semoga bisa bermanfaat untuk kita semua terutama yang sedang belajar mengenai android.
Komentar
Posting Komentar