Android 계산기앱 3

JAVA / 기초

Posted by ETHAN KIM on April 13, 2022 · 21 mins read
Android Java

주요 개념

  • ImageView Gif 넣어보기


실습 환경

  • Winodws / Android Studio / JDK


구현 결과


./java/com.example.helloapp/MainActivity.java

  • glide import 사전 작업 필요 (build app 내 dependeicies 추가)
  • 각 ImageView와 ./res/drawble 내 실제 img파일 매칭
package com.example.helloapp;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;

public class MainActivity extends AppCompatActivity {

    EditText answer, formula;

    Button pl,min,mul,div,rem,del;
    Button eq;

    String history = "";
    String number1 = "";
    String number2 = "";

    int type;

    int PLUS = 0;
    int MIN = 1;
    int MUL = 2;
    int DIV = 3;
    int REM = 4;
    double fir;
    double sec;

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

        ImageView imageView = findViewById(R.id.and_gif);
        ImageView fi1 = findViewById(R.id.f_img1);
        ImageView fi2 = findViewById(R.id.f_img2);
        ImageView fi3 = findViewById(R.id.f_img3);
        ImageView ai1 = findViewById(R.id.a_img1);
        ImageView ai2 = findViewById(R.id.a_img2);
        ImageView ai3 = findViewById(R.id.a_img3);
        ImageView banana1 = findViewById(R.id.banana1);

        Glide.with(this)
                .load(R.drawable.mini1)
                .into(imageView);
        Glide.with(this)
                .load(R.drawable.mini1)
                .into(fi1);
        Glide.with(this)
                .load(R.drawable.mini1)
                .into(fi2);
        Glide.with(this)
                .load(R.drawable.mini1)
                .into(fi3);
        Glide.with(this)
                .load(R.drawable.mini1)
                .into(ai2);
        Glide.with(this)
                .load(R.drawable.mini1)
                .into(ai3);
        Glide.with(this)
                .load(R.drawable.jump_min)
                .into(banana1);



        answer = findViewById(R.id.answer);
        formula = findViewById(R.id.formula);
        formula.setText("");
        pl = findViewById(R.id.btn_pl);
        min = findViewById(R.id.btn_min);
        mul = findViewById(R.id.btn_mul);
        div = findViewById(R.id.btn_div);
        rem = findViewById(R.id.btn_rem);
        del = findViewById(R.id.btn_del);
        eq = findViewById(R.id.btn_eq);

        pl.setOnClickListener(mListener);
        min.setOnClickListener(mListener);
        mul.setOnClickListener(mListener);
        div.setOnClickListener(mListener);
        rem.setOnClickListener(mListener);
        del.setOnClickListener(mListener);
        eq.setOnClickListener(mListener);

        Button clear = findViewById(R.id.btn_clear);
        clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view){
                answer.setText("");
                formula.setText("");
                fir = sec = 0;
                history = number1 = number2 = "";
            }
        });

        Button plus_minus = findViewById(R.id.btn_plmi);
        plus_minus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if( (Double.parseDouble(answer.getText().toString()) - (int)Double.parseDouble(answer.getText().toString())) == 0.0){
                    answer.setText( "" + (Integer.parseInt(answer.getText().toString()) * -1) );
                }else{
                    answer.setText( "" + (Double.parseDouble(answer.getText().toString()) * -1) );
                }
            }
        });
    }

    Button.OnClickListener mListener = new Button.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(answer.getText().toString() == null){
                Toast.makeText(MainActivity.this, "Please type any number", Toast.LENGTH_SHORT).show();
            }
            switch (view.getId()){
                case R.id.btn_pl:
                    number1 = answer.getText().toString();
                    history = answer.getText().toString() + "+";
                    formula.setText(history);
                    answer.setText("");
                    type = PLUS;
                    break;
                case R.id.btn_min:
                    number1 = answer.getText().toString();
                    history = answer.getText().toString() + "-";
                    formula.setText(history);
                    answer.setText("");
                    type = MIN;
                    break;
                case R.id.btn_mul:
                    number1 = answer.getText().toString();
                    history = answer.getText().toString() + "*";
                    formula.setText(history);
                    answer.setText("");
                    type = MUL;
                    break;
                case R.id.btn_div:
                    number1 = answer.getText().toString();
                    history = answer.getText().toString() + "/";
                    formula.setText(history);
                    answer.setText("");
                    type = DIV;
                    break;
                case R.id.btn_rem:
                    number1 = answer.getText().toString();
                    history = answer.getText().toString() + "%";
                    formula.setText(history);
                    answer.setText("");
                    type = REM;
                    break;
                case R.id.btn_del:
                    String del_number = answer.getText().toString();
                    Toast.makeText(MainActivity.this, del_number, Toast.LENGTH_SHORT).show();
                    answer.setText(del_number.substring(0,del_number.length()-1));
                    break;
                case R.id.btn_eq:
                    double result = 0;
                    Toast.makeText(MainActivity.this, "RESULT", Toast.LENGTH_SHORT).show();
                    number2 = answer.getText().toString();
                    history = history + number2;
                    formula.setText(history);

                    fir = Double.parseDouble(number1);
                    sec = Double.parseDouble(number2);

                    if(type == PLUS){
                        result = fir + sec;
                    }else if(type == MIN){
                        result = fir - sec;
                    }else if(type == MUL){
                        result = fir * sec;
                    }else if(type == DIV){
                        result = fir / sec;
                    }else if(type == REM){
                        result = fir % sec;
                    }
                    answer.setText("" + result);
                    number1 = answer.getText().toString();
                    break;
            }
        }
    };

    public void onClick(View view){
        switch (view.getId()){
            case R.id.btn0 : answer.setText(answer.getText().toString() + 0); break;
            case R.id.btn1 : answer.setText(answer.getText().toString() + 1); break;
            case R.id.btn2 : answer.setText(answer.getText().toString() + 2); break;
            case R.id.btn3 : answer.setText(answer.getText().toString() + 3); break;
            case R.id.btn4 : answer.setText(answer.getText().toString() + 4); break;
            case R.id.btn5 : answer.setText(answer.getText().toString() + 5); break;
            case R.id.btn6 : answer.setText(answer.getText().toString() + 6); break;
            case R.id.btn7 : answer.setText(answer.getText().toString() + 7); break;
            case R.id.btn8 : answer.setText(answer.getText().toString() + 8); break;
            case R.id.btn9 : answer.setText(answer.getText().toString() + 9); break;
            case R.id.btn_dot : answer.setText(answer.getText().toString() + "."); break;
        }
    }
}


./res/layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:background="#FAFAFA"
    android:orientation="vertical">
    <EditText
        android:id="@+id/formula"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_alignParentTop="true"
        android:hint="formula" />
    <LinearLayout
        android:id="@+id/f_line"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <ImageView
        android:id="@+id/f_img1"
        android:layout_width="wrap_content"
        android:layout_marginLeft="70dp"
        android:layout_height="80dp" />
    <ImageView
        android:id="@+id/f_img2"
        android:layout_width="wrap_content"
        android:layout_height="80dp" />
    <ImageView
        android:id="@+id/f_img3"
        android:layout_width="wrap_content"
        android:layout_height="80dp" />
    </LinearLayout>
    <EditText
        android:id="@+id/answer"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_below="@+id/formula"
        android:hint="Answer" />
    <LinearLayout
        android:id="@+id/a_line"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/formula">
        <ImageView
            android:id="@+id/a_img1"
            android:layout_width="wrap_content"
            android:layout_marginLeft="70dp"
            android:layout_height="80dp" />
        <ImageView
            android:id="@+id/a_img2"
            android:layout_width="wrap_content"
            android:layout_height="80dp" />
        <ImageView
            android:id="@+id/a_img3"
            android:layout_width="wrap_content"
            android:layout_height="80dp" />
    </LinearLayout>

    <ImageView
        android:id="@+id/banana1"
        android:layout_width="wrap_content"
        android:layout_height="80dp" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="80dp"
        android:weightSum="4">
        <Button
            android:onClick="onClick"
            android:id="@+id/btn1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="#FFD700"
            android:textColor="#1478CD"
            android:layout_margin="2dp"
            android:text="1"/>
        <Button
            android:onClick="onClick"
            android:id="@+id/btn2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="2dp"
            android:backgroundTint="#FFD700"
            android:textColor="#1478CD"
            android:text="2"/>
        <Button
            android:onClick="onClick"
            android:id="@+id/btn3"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="2dp"
            android:backgroundTint="#FFD700"
            android:textColor="#1478CD"
            android:text="3"/>
        <Button
            android:id="@+id/btn_pl"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="2dp"
            android:text="+"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="140dp"
        android:weightSum="4">
        <Button
            android:onClick="onClick"
            android:id="@+id/btn4"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="2dp"
            android:backgroundTint="#FFD700"
            android:textColor="#1478CD"
            android:text="4"/>
        <Button
            android:onClick="onClick"
            android:id="@+id/btn5"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="2dp"
            android:backgroundTint="#FFD700"
            android:textColor="#1478CD"
            android:text="5"/>
        <Button
            android:onClick="onClick"
            android:id="@+id/btn6"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="2dp"
            android:backgroundTint="#FFD700"
            android:textColor="#1478CD"
            android:text="6"/>
        <Button
            android:id="@+id/btn_min"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="2dp"
            android:text="-"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="200dp"
        android:weightSum="4">
        <Button
            android:onClick="onClick"
            android:id="@+id/btn7"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="2dp"
            android:backgroundTint="#FFD700"
            android:textColor="#1478CD"
            android:text="7"/>
        <Button
            android:onClick="onClick"
            android:id="@+id/btn8"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="2dp"
            android:backgroundTint="#FFD700"
            android:textColor="#1478CD"
            android:text="8"/>
        <Button
            android:onClick="onClick"
            android:id="@+id/btn9"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="2dp"
            android:backgroundTint="#FFD700"
            android:textColor="#1478CD"
            android:text="9"/>
        <Button
            android:id="@+id/btn_mul"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="2dp"
            android:text="*"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="20dp"
        android:weightSum="4">
        <Button
            android:id="@+id/btn_plmi"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="2dp"
            android:layout_weight="1"
            android:text="±"/>
        <Button
            android:onClick="onClick"
            android:id="@+id/btn0"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="2dp"
            android:layout_weight="1"
            android:text="0"/>
        <Button
            android:onClick="onClick"
            android:id="@+id/btn_dot"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="2dp"
            android:layout_weight="1"
            android:text="."/>
        <Button
            android:id="@+id/btn_eq"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="2dp"
            android:layout_weight="1"
            android:text="="/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="260dp"
        android:weightSum="4">
        <Button
            android:id="@+id/btn_clear"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="2dp"
            android:layout_weight="1"
            android:text="C"/>
        <Button
            android:id="@+id/btn_del"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="2dp"
            android:layout_weight="1"
            android:text="DEL"/>
        <Button
            android:id="@+id/btn_rem"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="2dp"
            android:layout_weight="1"
            android:text="%"/>
        <Button
            android:id="@+id/btn_div"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="2dp"
            android:layout_weight="1"
            android:text="/"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="330dp"
        android:weightSum="3">

        <ImageView
            android:id="@+id/and_gif"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="10dp"
            android:layout_weight="2" />
    </LinearLayout>

</RelativeLayout>