MainActivity.java

package com.bpdev.calculator_khy;

import static com.bpdev.calculator_khy.R.id.enterPercent;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.google.android.material.snackbar.Snackbar;

public class MainActivity extends AppCompatActivity {

    EditText enterPercent;
    EditText enterNumber;
    Button btnCalculator;
    TextView textResult;


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

        enterPercent = findViewById(R.id.enterPercent);
        enterNumber = findViewById(R.id.enterNumber);
        btnCalculator = findViewById(R.id.btnCalculator);
        textResult = findViewById(R.id.textResult);

        btnCalculator.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String strPercent = enterPercent.getText().toString().trim();
                String strNumber = enterNumber.getText().toString().trim();

                Log.i("CAL MAIN", "PER:"+strPercent);

                if ( strPercent.isEmpty() || strNumber.isEmpty() ){
                    Snackbar.make(btnCalculator, "필수 항목은 꼭 입력하세요.", Snackbar.LENGTH_SHORT).show();
                    return;
                }

                double percent = Double.parseDouble(strPercent);
                double number = Double.parseDouble(strNumber);

                double result = percent * number / 100;

                textResult.setText(""+result);

            }
        });
    }
}

 

 

activity_main.xml

Code

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="90dp"
        android:background="@color/black"
        android:paddingLeft="20dp"
        android:paddingTop="10dp"
        android:paddingRight="20dp"
        android:paddingBottom="10dp"
        android:text="P Calculator"
        android:textAlignment="center"
        android:textColor="@color/white"
        android:textSize="48sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="50dp"
        android:layout_marginEnd="8dp"
        android:orientation="horizontal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView">

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Enter Percent :"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/enterPercent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="Enter Percent..."
            android:inputType="number"
            android:textColorHint="#C1C1C1" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="8dp"
        android:orientation="horizontal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout2">

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Enter Your Number :"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/enterNumber"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="Enter Your Number..."
            android:inputType="number"
            android:textColorHint="#C1C1C1" />

    </LinearLayout>

    <Button
        android:id="@+id/btnCalculator"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginTop="32dp"
        android:layout_marginEnd="32dp"
        android:text="CALCULATE THE RESULT"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout" />

    <TextView
        android:id="@+id/textResult"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="70dp"
        android:background="#E91E63"
        android:gravity="center"
        android:paddingLeft="20dp"
        android:paddingTop="10dp"
        android:paddingRight="20dp"
        android:paddingBottom="10dp"
        android:text="Result"
        android:textColor="@color/white"
        android:textSize="24sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btnCalculator" />


</androidx.constraintlayout.widget.ConstraintLayout>

Design

+ Recent posts