Check even number and their sum - XML COD
<LinearLayout 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=".Jor_Jogpol"
android:orientation="vertical"
android:background="#E4EEED"
>
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar5"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@drawable/bottom_curved"
app:title="Check even number and their sum"
android:elevation="4dp"
app:titleTextColor="@color/white"
/>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/Golam"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
app:errorTextColor="#FF0000"
app:hintTextColor="#0038FF"
android:hint="input number of terms"
app:boxStrokeColor="#000DFF"
android:textColorHint="@color/black"
app:endIconMode="clear_text"
app:endIconTint="#FF0000"
>
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/EdJor_polo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:textColor="@color/black"
/>
</com.google.android.material.textfield.TextInputLayout>
<Button
android:id="@+id/Buttonpol"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="#1100FF"
android:text="Click Here"
android:textColor="#FFFFFF"
android:textSize="20sp"
android:layout_marginRight="110dp"
android:layout_marginLeft="110dp"
android:layout_marginTop="10dp"
/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="400dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="20dp"
>
<TextView
android:id="@+id/evenNumbersTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25sp"
android:textColor="@color/black"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:visibility="gone"
/>
<TextView
android:id="@+id/sumTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25sp"
android:textColor="#0F25EA"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:visibility="gone"
/>
<com.airbnb.lottie.LottieAnimationView
android:id="@+id/AnimisonLotti1"
android:layout_width="match_parent"
android:layout_height="400dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_gravity="center"
app:lottie_autoPlay="true"
app:lottie_loop="true"
app:lottie_rawRes="@raw/search"
android:visibility="visible"
/>
</LinearLayout>
</ScrollView>
</LinearLayout>
Calculating and parsing - JAVA CODE
package com.arah.jobayerahmed;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextView;
import com.airbnb.lottie.LottieAnimationView;
import com.google.android.material.textfield.TextInputEditText;
import com.google.android.material.textfield.TextInputLayout;
public class Jor_Jogpol extends AppCompatActivity {
TextView evenNumbersTextView, sumTextView;
Button Buttonpol;
TextInputEditText EdJor_polo;
TextInputLayout Golam;
Animation AHomea1,AHomea2;
LottieAnimationView AnimisonLotti1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.jor_jogpol);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar5);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
EdJor_polo = findViewById(R.id.EdJor_polo);
Buttonpol = findViewById(R.id.Buttonpol);
Golam = findViewById(R.id.Golam);
AnimisonLotti1 = findViewById(R.id.AnimisonLotti1);
evenNumbersTextView = findViewById(R.id.evenNumbersTextView);
sumTextView = findViewById(R.id.sumTextView);
AHomea1 = AnimationUtils.loadAnimation(this, R.anim.home1);
AHomea2 = AnimationUtils.loadAnimation(this, R.anim.home3);
Golam.setAnimation(AHomea1);
Buttonpol.setAnimation(AHomea2);
Buttonpol.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String hi = EdJor_polo.getText().toString();
if (hi.isEmpty()){
EdJor_polo.setError("Enter Number");
}else {
int n = Integer.parseInt(hi);
int sum = 0;
StringBuilder evenNumbersBuilder = new StringBuilder();
for (int i = 1; i <= n; i++) {
int evenNumber = 2 * i;
evenNumbersBuilder.append(evenNumber).append(" ");
sum += evenNumber;
}
AnimisonLotti1.setVisibility(View.GONE);
evenNumbersTextView.setVisibility(View.VISIBLE);
sumTextView.setVisibility(View.VISIBLE);
evenNumbersTextView.setText("Even numbers: " + evenNumbersBuilder.toString());
sumTextView.setText("Sum of even numbers: " + sum);
}
}
});
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
}
return super.onOptionsItemSelected(item);
}
}