Skip to content

Instantly share code, notes, and snippets.

@ssindher11
Created December 16, 2017 13:28
Show Gist options
  • Save ssindher11/509e7550e9b2ec86c2856957e15b8855 to your computer and use it in GitHub Desktop.
Save ssindher11/509e7550e9b2ec86c2856957e15b8855 to your computer and use it in GitHub Desktop.
<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=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:text="Quantity"
android:textAllCaps="true" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginRight="8dp"
android:onClick="decrement"
android:text="-" />
<TextView
android:id="@+id/quantity_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textAllCaps="true"
android:textColor="#000000"
android:textSize="16sp" />
<Button
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginLeft="8dp"
android:onClick="increment"
android:text="+" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="price"
android:textAllCaps="true" />
<TextView
android:id="@+id/price_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginTop="16dp"
android:text=""
android:textColor="#000000"
android:textSize="16sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="submitOrder"
android:text="Order" />
</LinearLayout>
/**
* IMPORTANT: Make sure you are using the correct package name.
* This example uses the package name:
* package com.example.android.justjava
* If you get an error when copying this code into Android studio, update it to match teh package name found
* in the project's AndroidManifest.xml file.
**/
package com.example.android.justjava;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import java.text.NumberFormat;
/**
* This app displays an order form to order coffee.
*/
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
int quantity = 0;
//ORDER button
public void submitOrder(View view) {
String priceMessage = "Amount Due = $ ";
displayMessage(priceMessage + (quantity * 15) + "\nThank You :)");
}
public void increment(View view)
{
quantity++;
display(quantity);
}
public void decrement(View view)
{
quantity--;
display(quantity);
}
/**
* This method displays the given quantity value on the screen.
*/
private void display(int number) {
TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
quantityTextView.setText("" + number);
}
/**
* This method displays the given price on the screen.
*/
private void displayPrice(int number) {
TextView priceTextView = (TextView) findViewById(R.id.price_text_view);
priceTextView.setText(NumberFormat.getCurrencyInstance().format(number));
}
/**
* This method displays the given text on the screen.
*/
private void displayMessage(String message) {
TextView priceTextView = (TextView) findViewById(R.id.price_text_view);
priceTextView.setText(message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment