Friday, 17 February 2017

Nested View and Quantity Picker 3rd project

input

xml code

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:id="@+id/activity_main"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:orientation="vertical"
  8. android:paddingBottom="@dimen/activity_vertical_margin"
  9. android:paddingLeft="@dimen/activity_horizontal_margin"
  10. android:paddingRight="@dimen/activity_horizontal_margin"
  11. android:paddingTop="@dimen/activity_vertical_margin"
  12. tools:context="com.example.saketh.firstprojectman.MainActivity">
  13. <TextView
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:layout_marginBottom="16dp"
  17. android:text="quantity"
  18. android:textAllCaps="true" />
  19. <LinearLayout
  20. android:layout_width="match_parent"
  21. android:layout_height="wrap_content"
  22. android:orientation="horizontal">
  23. <Button
  24. android:layout_width="48dp"
  25. android:layout_height="48dp"
  26. android:onClick="decrement"
  27. android:text="-" />
  28. <TextView
  29. android:id="@+id/quantity_text_view"
  30. android:layout_width="wrap_content"
  31. android:layout_height="wrap_content"
  32. android:layout_marginBottom="16dp"
  33. android:layout_marginLeft="8dp"
  34. android:text="0"
  35. android:textAllCaps="true"
  36. android:textColor="@android:color/black"
  37. android:textSize="16sp"
  38. android:paddingLeft="8dp"
  39. android:paddingRight="8dp"/>
  40. <Button
  41. android:layout_width="48dp"
  42. android:layout_height="48dp"
  43. android:layout_marginLeft="8dp"
  44. android:onClick="increment"
  45. android:text="+" />
  46. </LinearLayout>
  47. <TextView
  48. android:layout_width="wrap_content"
  49. android:layout_height="wrap_content"
  50. android:layout_marginBottom="16dp"
  51. android:layout_marginTop="16dp"
  52. android:text="price"
  53. android:textAllCaps="true" />
  54. <TextView
  55. android:id="@+id/price_text_view"
  56. android:layout_width="wrap_content"
  57. android:layout_height="wrap_content"
  58. android:layout_marginBottom="16dp"
  59. android:text="0"
  60. android:textAllCaps="true"
  61. android:textColor="@android:color/black"
  62. android:textSize="16sp" />
  63. <Button
  64. android:layout_width="wrap_content"
  65. android:layout_height="wrap_content"
  66. android:onClick="submitOrder"
  67. android:text="order" />
  68. </LinearLayout>
java code
  1. package com.example.saketh.firstprojectman; /**
  2. * Add your package below. Package name can be found in the project's AndroidManifest.xml file.
  3. * This is the package name our example uses:
  4. *
  5. * package com.example.android.justjava;
  6. */
  7. import android.icu.text.NumberFormat;
  8. import android.os.Bundle;
  9. import android.support.v7.app.AppCompatActivity;
  10. import android.view.View;
  11. import android.widget.TextView;
  12. import com.example.saketh.firstprojectman.R;
  13. import static android.icu.text.NumberFormat.getCurrencyInstance;
  14. /**
  15. * This app displays an order form to order coffee.
  16. */
  17. public class MainActivity extends AppCompatActivity {
  18. int quantity=0;
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.activity_main);
  23. }
  24. /**
  25. * This method is called when the order button is clicked.
  26. */
  27. public void submitOrder(View view) {
  28. int numberOfCoffee=quantity;
  29. display(numberOfCoffee);
  30. priceBox(numberOfCoffee*2);
  31. }
  32. /**
  33. * This method displays the given quantity value on the screen.
  34. */
  35. private void display(int number) {
  36. TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
  37. quantityTextView.setText("" + number);
  38. }
  39. private void priceBox(int number){
  40. TextView pricetext=(TextView) findViewById(R.id.price_text_view);
  41. pricetext.setText(NumberFormat.getCurrencyInstance().format(number));
  42. }
  43. public void increment(View view){
  44. quantity=quantity+1;
  45. display(quantity);
  46. }
  47. public void decrement(View view){
  48. quantity=quantity-1;
  49. display(quantity);
} }



output




No comments:

Post a Comment