Android Study

Android Study(Making App #Rain)

Box Maker 2023. 4. 5. 02:16

1. Rain 앱 만들기

- 목표

 

  • $0이라는 텍스트뷰를 만들고 make it rain 버튼을  클릭할 때마다 100,000원씩 금액이 올라간다.
  • show info 버튼을 클릭하면 Get, Rich 문구가 알람처럼 나왔다 사라진다.
  • 금액이 1,000,000원이 되면 빨간색으로 변하고 congratulations!! You get rich!! 문구가 알람처럼 나왔다 사라진다.

 

 

초기화면)

 

1) Text view와 Button 만들기

  • 사실 디자인의 경우 안드로이드 스튜디오를 사용해 만들면 gui 방식으로 편하게 만들수 있다.
  • 앱의 디자인 코드인 xml코드는 다음과 같다.
<?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"
    android:background="#C0CA33"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/you_have"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/you_have"
        android:textSize="16sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.176" />

    <TextView
        android:id="@+id/moneyValue"
        android:layout_width="236dp"
        android:layout_height="45dp"
        android:gravity="center"
        android:text="@string/money_value"
        android:textSize="34sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.497"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/you_have"
        app:layout_constraintVertical_bias="0.07"
        tools:ignore="TextSizeCheck" />

    <Button
        android:id="@+id/buttonMakeItRain"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/make_it_rain"
        android:onClick="showMoney"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/moneyValue"
        app:layout_constraintVertical_bias="0.121" />


    <Button
        android:id="@+id/buttonShowInfo"
        android:layout_width="137dp"
        android:layout_height="51dp"
        android:onClick="showInfo"
        android:text="@string/show_info"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/buttonMakeItRain"
        app:layout_constraintVertical_bias="0.137" />

</androidx.constraintlayout.widget.ConstraintLayout>

 

2) 기능 구현하기

목표에서 설명한 순서대로 다음과 같이 기능을 구현해보겠다.

 

앱의 각 버튼과 텍스트뷰의 리소스 값이다.

 

string.xml

<resources>
    <string name="app_name">MakeItRain</string>
    <string name="you_have">You Have:</string>
    <string name="money_value">$0</string>
    <string name="make_it_rain">MAKE IT RAIN</string>
    <string name="show_info">SHOW INFO</string>
    <string name="test">Hello</string>
    <string name="app_info">Get, Rich</string>
    <string name="money_info">" congratulations!! You get rich!!"</string>
</resources>

 

2-1 MAKE IT RAIN 버튼 기능 구현

MainActivity.java

package com.example.makeitrain;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

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

import java.text.NumberFormat;

public class MainActivity extends AppCompatActivity {

    private TextView moneyValue;
    private int moneyCounter = 0;

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

    }
    
    public void showMoney(View view) {
    	NumberFormat numberFormat = NumberFormat.getCurrencyInstance();
        
    	moneyCounter += 100000;
        moneyValue.setText(String.valueOf(numberFormat.format(moneyCounter)));
        Log.d("MainActivity", "onClick: " + moneyCounter);
  • 먼저 화면에 금액을 출력하기 위해 moneyValue를 텍스트뷰로 선언한다.
  • 그 다음 moneyCounter 변수를 생성해 금액이 올라갈때마다 해당 변수에 값을 담는다.
  • 텍스트뷰로 선언한 moneyValue를 findViewById()로 main.xml에서 설정한 id값인 moneyValue를 MainActivity.java로 가져온다.
  • showMoney 메소드를 생성한다.
  • NumberFormat 클래스를 이용해 numberFormat 객체를 생성한다.
  • 버튼을 클릭할 때마다 100,000원씩 누적되도록 한다.
  • 금액이 화면에 출력되도록 setText를 이용해 moneyValue를 문자열 형태로 변환한다.

 

if (moneyCounter == 1000000) {
            moneyValue.setTextColor(Color.RED);
            Toast.makeText(MainActivity.this, R.string.money_info, Toast.LENGTH_LONG)
                    .show();
        }
}
  • showMoney 메소드 안에 if문을 삽입하여 금액이 1,000,000원이 되면
  • setTextColor를 이용해 빨간색으로 바꾼다.
  • Toast기능도 사용하여 money_info 리소스를 만들고 리소스값으로 congratulations!! You get rich!!를 삽입하여 알람도 같이 뜨게 만든다.

 

2-2 SHOW INFO 버튼 기능 구현

 

MainActivity.java

public void showInfo(View view) {

        Snackbar.make(moneyValue, R.string.app_info, Snackbar.LENGTH_LONG)
                .setAction("More", view1 -> {
                    Log.d("Snack", "showInfo: Snackbar More");
                })
                .show();
    }
}
  • showInfo 메소드를 생성한다.
  • Snackbar를 이용해 app_info 리소스를 만들고 리소스값으로 Get, Rich를 삽입한다.
  • 스낵바 안에 More 버튼을 클릭할 시 Snackbar More 라는 문구가 안드로이드 스튜디오 로그로 나타나도록 한다.

 

2. 앱 실행

 

MAKE IT RAIN 버튼 기능 구현 모습

 

 

SHOW INFO 버튼 기능 구현 모습

 

 

스낵바에 More 버튼을 클릭한 모습