Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.matan.donationhomework2;
- import android.content.res.ColorStateList;
- import android.graphics.Color;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.animation.Animation;
- import android.view.animation.Transformation;
- import android.widget.Button;
- import android.widget.ImageView;
- import android.widget.NumberPicker;
- import android.widget.ProgressBar;
- import android.widget.RadioGroup;
- import android.widget.TextView;
- import android.widget.Toast;
- public class MainActivity extends AppCompatActivity {
- double payPalToDollar = 1 / 3.5;
- double sum = 0;
- double currentPercent = 0;
- double bitCoinToDollar = 3000 / 3.5;
- double target = 10000;
- private Button btnDonate;
- private RadioGroup rgMethod;
- private NumberPicker npAmount;
- private ProgressBar progressBar;
- private TextView txtPercent;
- private TextView txtAmount;
- private ImageView imageView;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- init();
- }
- private void init() {
- btnDonate = findViewById(R.id.btnDonate);
- rgMethod = findViewById(R.id.rgMethod);
- npAmount = findViewById(R.id.npAmount);
- progressBar = findViewById(R.id.progressBar);
- txtPercent = findViewById(R.id.txtPercent);
- txtAmount = findViewById(R.id.txtAmount);
- imageView = findViewById(R.id.imageView);
- npAmount.setMinValue(1);
- npAmount.setMaxValue(1000);
- btnDonate.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- int radioId = rgMethod.getCheckedRadioButtonId();
- sum += radioId == R.id.rbPaypal? payPalToDollar * (double)npAmount.getValue() : bitCoinToDollar * (double)npAmount.getValue();
- double future = (sum / target) * 100;
- ProgressBarAnimation anim = new ProgressBarAnimation(progressBar, currentPercent, future);
- anim.setDuration(500);
- progressBar.startAnimation(anim);
- currentPercent = (sum / target) * 100;
- txtPercent.setText((int)currentPercent + "%");
- txtAmount.setText((int) sum + "$/10000$");
- System.out.println(sum + " HI " + future );
- if(currentPercent >= 25){
- progressBar.setProgressTintList(ColorStateList.valueOf(Color.GREEN));
- }if(currentPercent >= 50){
- progressBar.setProgressTintList(ColorStateList.valueOf(Color.BLACK));
- }if(currentPercent >= 75) {
- progressBar.setProgressTintList(ColorStateList.valueOf(Color.BLUE));
- }if(currentPercent >= 100) {
- imageView.setVisibility(View.VISIBLE);
- }
- int amount = npAmount.getValue();
- String method = radioId == R.id.rbPaypal ? "Paypal" : "Bitcoin";
- Toast.makeText(getApplicationContext(), "You donated " + amount + " via " + method, Toast.LENGTH_SHORT).show();
- }
- });
- }
- public class ProgressBarAnimation extends Animation {
- //ProgressBar progressBar;
- private double from;
- private double to;
- public ProgressBarAnimation(ProgressBar progressBar, double from, double to) {
- super();
- //this.progressBar = progressBar;
- this.from = from;
- this.to = to;
- }
- @Override
- protected void applyTransformation(float interpolatedTime, Transformation t) {
- super.applyTransformation(interpolatedTime, t);
- double value = from + (to - from) * interpolatedTime;
- progressBar.setProgress((int) value);
- }
- }
- }
- XML:
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout 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="com.example.matan.donationhomework2.MainActivity">
- <TextView
- android:id="@+id/txtTitle"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentTop="true"
- android:layout_centerHorizontal="true"
- android:text="Welcome..."
- android:textSize="30sp"
- android:textStyle="bold" />
- <TextView
- android:id="@+id/txtSubtitle"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_below="@id/txtTitle"
- android:text="Please give me money..."
- android:textSize="16sp" />
- <RadioGroup
- android:id="@+id/rgMethod"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentStart="true"
- android:layout_below="@id/txtSubtitle">
- <RadioButton
- android:id="@+id/rbPaypal"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:checked="true"
- android:text="Paypal" />
- <RadioButton
- android:id="@+id/rbBitcoin"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Bitcoin" />
- </RadioGroup>
- <NumberPicker
- android:id="@+id/npAmount"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignEnd="@id/txtTitle"
- android:layout_alignParentEnd="true"
- android:layout_alignTop="@id/rgMethod">
- </NumberPicker>
- <ProgressBar
- android:id="@+id/progressBar"
- style="@style/Base.Widget.AppCompat.ProgressBar.Horizontal"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_above="@+id/btnDonate"
- android:layout_alignParentStart="true"
- android:layout_marginBottom="64dp" />
- <Button
- android:id="@+id/btnDonate"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentBottom="true"
- android:layout_centerHorizontal="true"
- android:text="Donate" />
- <TextView
- android:id="@+id/txtPercent"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_above="@+id/progressBar"
- android:layout_centerHorizontal="true"
- android:text="0%"
- android:textStyle="bold" />
- <TextView
- android:id="@+id/txtAmount"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_above="@+id/progressBar"
- android:layout_alignParentStart="true"
- android:text="0$/10000$"
- android:textStyle="bold" />
- <ImageView
- android:id="@+id/imageView"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:src="@drawable/congratulations"
- android:visibility="invisible" />
- <TextView
- android:id="@+id/txtPaypal"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentStart="true"
- android:layout_below="@+id/rgMethod"
- android:text="Paypal = 1₪" />
- <TextView
- android:id="@+id/txtBitcoin"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentStart="true"
- android:layout_below="@+id/txtPaypal"
- android:text="Bitcoin = 3000₪" />
- </RelativeLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement