Code Tutorial #001

activity_main.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=".MainActivity">

<TextView
android:id="@+id/counting_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="0"
android:layout_centerInParent="true"/>

<EditText
android:id="@+id/number_change"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@+id/counting_view"
android:layout_marginTop="50dp"
android:ems="10"
android:inputType="number"
android:gravity="center"
android:hint="0"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_add"
android:text="ADD"
android:layout_below="@+id/number_change"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"/>

</RelativeLayout>

 

MainActivity.java:

import androidx.appcompat.app.AppCompatActivity;

import android.animation.ValueAnimator;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

Button btnAdd;
TextView countinView;
EditText numberChange;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

btnAdd = findViewById(R.id.btn_add);
countinView = findViewById(R.id.counting_view);
numberChange = findViewById(R.id.number_change);

btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int oldValue = Integer.parseInt(countinView.getText().toString());
ValueAnimator animator = ValueAnimator.ofInt(oldValue, Integer.parseInt(countinView.getText().toString()) + Integer.parseInt(numberChange.getText().toString()));
animator.setDuration(8000); //Duration is in milliseconds
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
public void onAnimationUpdate(ValueAnimator animation) {
countinView.setText(animation.getAnimatedValue().toString());
}

});
numberChange.setText("0");
animator.start();
}
});

}
}

Leave a Reply

Your email address will not be published. Required fields are marked *