Skip to content

Instantly share code, notes, and snippets.

@varanshu-engineer
Created July 24, 2018 06:28
Show Gist options
  • Save varanshu-engineer/98de4617bbb895f3d58113d898341fd3 to your computer and use it in GitHub Desktop.
Save varanshu-engineer/98de4617bbb895f3d58113d898341fd3 to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/name_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name"
android:inputType="textCapWords"/>
<TextView
android:text="topping"
style="@style/HeaderTextStyle"/>
<CheckBox
android:id="@+id/whipped_cream_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="24dp"
android:text="Whipped Cream"
android:textSize="16sp"
/>
<CheckBox
android:id="@+id/chocolate_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="24dp"
android:text="Chocolate"
android:textSize="16sp"
/>
<TextView
android:text="Quantity"
style="@style/HeaderTextStyle"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:id="@+id/minus"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_margin="8dp"
android:text="-"
android:onClick="decrement"/>
<TextView
android:id="@+id/quantity_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:layout_margin="8dp"
android:textColor="@android:color/black" />
<Button
android:id="@+id/plus"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_margin="8dp"
android:onClick="increment"
android:text="+" />
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Order"
android:layout_margin="8dp"
android:onClick="submitOrder"/>
</LinearLayout>
</ScrollView>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.justjava">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<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>
package com.example.android.justjava;
import java.text.NumberFormat;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
/**
* 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);
}
/**
* This method is called when the order button is clicked.
*/
int num=2;
public void submitOrder(View view) {
CheckBox whippedCreamCheckBox= (CheckBox) findViewById(R.id.whipped_cream_checkbox);
boolean hasWhippedCream=whippedCreamCheckBox.isChecked();
// Log.v("MainActivity","Has Whipped Cream: "+hasWhippedCream);
CheckBox ChocoCheckBox= (CheckBox) findViewById(R.id.chocolate_checkbox);
boolean hasChoco=ChocoCheckBox.isChecked();
EditText text = (EditText) findViewById(R.id.name_field);
String name=text.getText().toString();
int price=calculatePrice(hasWhippedCream,hasChoco);
String priceMessage=createOrderSummary(name,price,hasWhippedCream,hasChoco);
Intent intent=new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:")); //only email apps should handle this
intent.putExtra(Intent.EXTRA_SUBJECT,"Just Java order for name "+name);
intent.putExtra(Intent.EXTRA_TEXT,priceMessage);
if(intent.resolveActivity(getPackageManager())!=null)
{
startActivity(intent);
}
}
public void increment(View view) {
if(num==100)
{
Toast.makeText(this,"You cannot have more than 100 cup of coffees",Toast.LENGTH_SHORT).show();
return;
}
num=num+1;
displayQuantity(num);
}
public void decrement(View view) {
if(num==1)
{
Toast.makeText(this,"You cannot have less than 1 cup if coffee",Toast.LENGTH_SHORT).show();
return;
}
num = num-1;
displayQuantity(num);
}
/**
* This method displays the given quantity value on the screen.
*/
private void displayQuantity(int num) {
TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
quantityTextView.setText("" + num);
}
private String createOrderSummary(String name,int price, boolean addWhippedCream,boolean addChoco)
{
String priceMessage="Name = "+name;
priceMessage+="\nHas Whipped Cream: "+addWhippedCream;
priceMessage+="\nHas Chocolate: "+addChoco;
priceMessage+="\nQuantity: "+num;
priceMessage+="\nPrice $"+price ;
priceMessage+="\nThank you!";
return priceMessage;
}
/**
* Calculates the price of the order based on the current quantity.
*
* @return the price
*/
private int calculatePrice(boolean hasWhippedCream,boolean hasChoco) {
int base=5;
if(hasWhippedCream) {
base= base+1;
}
if(hasChoco) {
base=base+2;
}
return num*base;
}
}
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#009688</item>
<item name="colorPrimaryDark">#00796B</item>
<item name="colorAccent">#536DFE</item>
</style>
<!--
<style name="CustomText">
<item name="android:textSize">20sp</item>
<item name="android:textColor">#8379EE</item>
<item name="android:textAllCaps">true</item>
</style>
-->
<style name="HeaderTextStyle">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">48dp</item>
<item name="android:textSize">15sp</item>
<item name="android:textColor">#8379EE</item>
<item name="android:gravity">center_vertical</item>
<item name="android:textAllCaps">true</item>
</style>
</resources>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment