Skip to content

Instantly share code, notes, and snippets.

@arifsetyawan
Last active August 28, 2016 08:41
Show Gist options
  • Save arifsetyawan/58454e57c630acabeb3703d51c5964e1 to your computer and use it in GitHub Desktop.
Save arifsetyawan/58454e57c630acabeb3703d51c5964e1 to your computer and use it in GitHub Desktop.
android_bekup_sample_5
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
tools:context="com.arifsetyawan.myapplication.MainActivity">
<!--<ListView
android:id="@+id/list"
android:layout_height="match_parent"
android:layout_width="wrap_content">
</ListView>-->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/counter_title"
android:text="Quantity"
android:textAllCaps="true"
android:layout_marginBottom="16dp"
android:textSize="16sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/quantity_text_view"
android:layout_margin="0dp"
android:text="0" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/counter_title"
android:text="Price"
android:textAllCaps="true"
android:layout_marginBottom="16dp"
android:layout_marginTop="16dp"
android:textSize="16sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/price_text_view"
android:text="$0" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="16dp"
android:paddingBottom="16dp"
>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="+"
android:onClick="increaseItem"
android:layout_weight="1"
/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="-"
android:onClick="decreaseItem"
android:layout_weight="1"
/>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Order"
android:onClick="orderItem"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/order_text_view"
android:text="" />
</LinearLayout>
package com.arifsetyawan.myapplication;
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
import java.text.NumberFormat;
import java.util.Currency;
import java.util.Locale;
/**
* This app displays an order form to order coffee.
*/
public class MainActivity extends Activity {
ListView listView;
protected double itemPrice = 30;
protected int qty = 0;
protected double totalPrice = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*
// Get ListView object from xml
listView = (ListView) findViewById(R.id.list);
// Defined Array values to show in ListView
String[] values = new String[] { "Android List View",
"Adapter implementation",
"Simple List View In Android",
"Create List View Android",
"Android Example",
"List View Source Code",
"List View Array Adapter",
"Android Example List View"
};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, values);
// Assign adapter to ListView
listView.setAdapter(adapter);
// ListView Item Click Listener
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// ListView Clicked item index
int itemPosition = position;
// ListView Clicked item value
String itemValue = (String) listView.getItemAtPosition(position);
// Show Alert
Toast.makeText(getApplicationContext(),
"Position :"+itemPosition+" ListItem : " +itemValue , Toast.LENGTH_LONG)
.show();
}
});*/
}
/**
* Menambah jumlah barang
* @param view
*/
public void increaseItem(View view) {
display(1);
}
/**
* Mengurangi jumlah barang
* @param view
*/
public void decreaseItem(View view){
display(-1);
}
/**
* Aksi untuk button Order
*/
public void orderItem(View view){
TextView orderTextView = (TextView) findViewById(R.id.order_text_view);
orderTextView.setText("Konfirmasi : anda telah order "+qty+" item dengan harga "+totalPrice);
}
/**
* This method displays the given quantity value on the screen.
*/
private void display(int number) {
// Ambil object TextView quantity_text_view
TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
// Ambil object TextView price_text_view
TextView priceTextView = (TextView) findViewById(R.id.price_text_view);
// Parsing quantity dari String ke Integer agar dapat masuk ke operasi matematika
int existing = Integer.parseInt(String.valueOf(quantityTextView.getText()));
// Menambahkan value number pada existing value di quantity_text_view
number = number+existing;
qty = number;
// Proteksi non-negative value
if(number < 1) number = 0;
// Kalkulasi Harga
double price = number * itemPrice;
totalPrice = price;
// Set Display ke object quantityTextView
quantityTextView.setText("" + number);
// Set DIsplay ke object priceTextView
priceTextView.setText("" + NumberFormat.getCurrencyInstance().format(price));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment