Advertisement
ultravibez

Android Donation Homework

Nov 18th, 2017
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.96 KB | None | 0 0
  1. package com.example.matan.donationhomework2;
  2.  
  3. import android.content.res.ColorStateList;
  4. import android.graphics.Color;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.view.animation.Animation;
  9. import android.view.animation.Transformation;
  10. import android.widget.Button;
  11. import android.widget.ImageView;
  12. import android.widget.NumberPicker;
  13. import android.widget.ProgressBar;
  14. import android.widget.RadioGroup;
  15. import android.widget.TextView;
  16. import android.widget.Toast;
  17.  
  18. public class MainActivity extends AppCompatActivity {
  19.  
  20.     double payPalToDollar = 1 / 3.5;
  21.     double sum = 0;
  22.     double currentPercent = 0;
  23.     double bitCoinToDollar = 3000 / 3.5;
  24.     double target = 10000;
  25.  
  26.     private Button btnDonate;
  27.     private RadioGroup rgMethod;
  28.     private NumberPicker npAmount;
  29.     private ProgressBar progressBar;
  30.     private TextView txtPercent;
  31.     private TextView txtAmount;
  32.     private ImageView imageView;
  33.  
  34.  
  35.     @Override
  36.     protected void onCreate(Bundle savedInstanceState) {
  37.         super.onCreate(savedInstanceState);
  38.         setContentView(R.layout.activity_main);
  39.         init();
  40.     }
  41.  
  42.     private void init() {
  43.         btnDonate = findViewById(R.id.btnDonate);
  44.         rgMethod = findViewById(R.id.rgMethod);
  45.         npAmount = findViewById(R.id.npAmount);
  46.         progressBar = findViewById(R.id.progressBar);
  47.         txtPercent = findViewById(R.id.txtPercent);
  48.         txtAmount = findViewById(R.id.txtAmount);
  49.         imageView = findViewById(R.id.imageView);
  50.  
  51.  
  52.         npAmount.setMinValue(1);
  53.         npAmount.setMaxValue(1000);
  54.  
  55.         btnDonate.setOnClickListener(new View.OnClickListener() {
  56.  
  57.             @Override
  58.             public void onClick(View view) {
  59.                 int radioId = rgMethod.getCheckedRadioButtonId();
  60.                 sum += radioId == R.id.rbPaypal? payPalToDollar * (double)npAmount.getValue() : bitCoinToDollar * (double)npAmount.getValue();
  61.                 double future = (sum / target) * 100;
  62.                 ProgressBarAnimation anim = new ProgressBarAnimation(progressBar, currentPercent, future);
  63.                 anim.setDuration(500);
  64.                 progressBar.startAnimation(anim);
  65.                 currentPercent = (sum / target) * 100;
  66.                 txtPercent.setText((int)currentPercent + "%");
  67.                 txtAmount.setText((int) sum  + "$/10000$");
  68.                 System.out.println(sum + " HI   " + future );
  69.  
  70.                 if(currentPercent >= 25){
  71.                     progressBar.setProgressTintList(ColorStateList.valueOf(Color.GREEN));
  72.                 }if(currentPercent >= 50){
  73.                     progressBar.setProgressTintList(ColorStateList.valueOf(Color.BLACK));
  74.                 }if(currentPercent >= 75) {
  75.                     progressBar.setProgressTintList(ColorStateList.valueOf(Color.BLUE));
  76.                 }if(currentPercent >= 100) {
  77.                     imageView.setVisibility(View.VISIBLE);
  78.                 }
  79.  
  80.                 int amount = npAmount.getValue();
  81.                 String method = radioId == R.id.rbPaypal ? "Paypal" : "Bitcoin";
  82.                 Toast.makeText(getApplicationContext(), "You donated " + amount + " via " + method, Toast.LENGTH_SHORT).show();
  83.             }
  84.         });
  85.     }
  86.  
  87.     public class ProgressBarAnimation extends Animation {
  88.         //ProgressBar progressBar;
  89.         private double from;
  90.         private double to;
  91.  
  92.         public ProgressBarAnimation(ProgressBar progressBar, double from, double to) {
  93.             super();
  94.             //this.progressBar = progressBar;
  95.             this.from = from;
  96.             this.to = to;
  97.         }
  98.  
  99.         @Override
  100.         protected void applyTransformation(float interpolatedTime, Transformation t) {
  101.             super.applyTransformation(interpolatedTime, t);
  102.             double value = from + (to - from) * interpolatedTime;
  103.             progressBar.setProgress((int) value);
  104.         }
  105.     }
  106. }
  107.  
  108.  
  109.  
  110. XML:
  111.  
  112. <?xml version="1.0" encoding="utf-8"?>
  113. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  114.     xmlns:app="http://schemas.android.com/apk/res-auto"
  115.     xmlns:tools="http://schemas.android.com/tools"
  116.     android:layout_width="match_parent"
  117.     android:layout_height="match_parent"
  118.     tools:context="com.example.matan.donationhomework2.MainActivity">
  119.  
  120.     <TextView
  121.         android:id="@+id/txtTitle"
  122.         android:layout_width="wrap_content"
  123.         android:layout_height="wrap_content"
  124.         android:layout_alignParentTop="true"
  125.         android:layout_centerHorizontal="true"
  126.         android:text="Welcome..."
  127.         android:textSize="30sp"
  128.         android:textStyle="bold" />
  129.  
  130.     <TextView
  131.         android:id="@+id/txtSubtitle"
  132.         android:layout_width="wrap_content"
  133.         android:layout_height="wrap_content"
  134.         android:layout_below="@id/txtTitle"
  135.         android:text="Please give me money..."
  136.         android:textSize="16sp" />
  137.  
  138.     <RadioGroup
  139.         android:id="@+id/rgMethod"
  140.         android:layout_width="wrap_content"
  141.         android:layout_height="wrap_content"
  142.         android:layout_alignParentStart="true"
  143.         android:layout_below="@id/txtSubtitle">
  144.  
  145.         <RadioButton
  146.             android:id="@+id/rbPaypal"
  147.             android:layout_width="wrap_content"
  148.             android:layout_height="wrap_content"
  149.             android:checked="true"
  150.             android:text="Paypal" />
  151.  
  152.         <RadioButton
  153.             android:id="@+id/rbBitcoin"
  154.             android:layout_width="wrap_content"
  155.             android:layout_height="wrap_content"
  156.             android:text="Bitcoin" />
  157.  
  158.     </RadioGroup>
  159.  
  160.     <NumberPicker
  161.         android:id="@+id/npAmount"
  162.         android:layout_width="wrap_content"
  163.         android:layout_height="wrap_content"
  164.         android:layout_alignEnd="@id/txtTitle"
  165.         android:layout_alignParentEnd="true"
  166.         android:layout_alignTop="@id/rgMethod">
  167.  
  168.     </NumberPicker>
  169.  
  170.     <ProgressBar
  171.         android:id="@+id/progressBar"
  172.         style="@style/Base.Widget.AppCompat.ProgressBar.Horizontal"
  173.         android:layout_width="match_parent"
  174.         android:layout_height="wrap_content"
  175.         android:layout_above="@+id/btnDonate"
  176.         android:layout_alignParentStart="true"
  177.         android:layout_marginBottom="64dp" />
  178.  
  179.     <Button
  180.         android:id="@+id/btnDonate"
  181.         android:layout_width="wrap_content"
  182.         android:layout_height="wrap_content"
  183.         android:layout_alignParentBottom="true"
  184.         android:layout_centerHorizontal="true"
  185.         android:text="Donate" />
  186.  
  187.     <TextView
  188.         android:id="@+id/txtPercent"
  189.         android:layout_width="wrap_content"
  190.         android:layout_height="wrap_content"
  191.         android:layout_above="@+id/progressBar"
  192.         android:layout_centerHorizontal="true"
  193.         android:text="0%"
  194.         android:textStyle="bold" />
  195.  
  196.     <TextView
  197.         android:id="@+id/txtAmount"
  198.         android:layout_width="wrap_content"
  199.         android:layout_height="wrap_content"
  200.         android:layout_above="@+id/progressBar"
  201.         android:layout_alignParentStart="true"
  202.         android:text="0$/10000$"
  203.         android:textStyle="bold" />
  204.  
  205.     <ImageView
  206.         android:id="@+id/imageView"
  207.         android:layout_width="match_parent"
  208.         android:layout_height="wrap_content"
  209.         android:src="@drawable/congratulations"
  210.         android:visibility="invisible" />
  211.  
  212.     <TextView
  213.         android:id="@+id/txtPaypal"
  214.         android:layout_width="wrap_content"
  215.         android:layout_height="wrap_content"
  216.         android:layout_alignParentStart="true"
  217.         android:layout_below="@+id/rgMethod"
  218.         android:text="Paypal = 1₪" />
  219.  
  220.     <TextView
  221.         android:id="@+id/txtBitcoin"
  222.         android:layout_width="wrap_content"
  223.         android:layout_height="wrap_content"
  224.         android:layout_alignParentStart="true"
  225.         android:layout_below="@+id/txtPaypal"
  226.         android:text="Bitcoin = 3000₪" />
  227.  
  228. </RelativeLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement