본문 바로가기

노력을 이기는 재능은 없고
노력을 외면하는 결과도 없다.
- 이창호 9단

D E V E L O P M E N T/Android

[Andriod] 안드로이드 홈 버튼 효과 만들기

  Junesker   2021. 6. 24.
반응형

모바일 핸드폰에는 뒤로가기, 홈으로가기, 실행중인 앱 목록보기 버튼이 있다. 회사 자체 앱 개발중에 뒤로가기 버튼을 눌러 "프로그램을 종료하시겠습니까?" 알림창이 나타나는데 이때 취소를 누르면 홈으로 가고 Backgroud로는 어플리케이션이 돌아가야했다. 아주 간단한 기능이지만, 나만의 안드로이드 게시판에 남겨보고자 한다.

 

 

 

 

 

 

홈 버튼 효과 만들기

 

 

 

 

 

1.  버튼이 포함된 Activity

 

[activity_main.xml] Source

<?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">

    <Button
        android:id="@+id/button_home"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="158dp"
        android:layout_marginLeft="158dp"
        android:layout_marginTop="35dp"
        android:layout_marginEnd="165dp"
        android:layout_marginRight="165dp"
        android:layout_marginBottom="272dp"
        android:text="@string/button_login"
        app:backgroundTint="#1D74DF"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

android:text="@string/button_login"은 string.xml에 정의한 text "Home"이다.

 

 

 

 

 

2.  메인 클래스에서 버튼 클릭 이벤트를 선언하고 홈 버튼 효과를 만든다.

 

[MainActivity.class] Source

package com.example.buttoncontrol;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    private Button button_home;

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

        button_home = findViewById(R.id.button_home);
        button_home.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 핸드폰마다 'Home'키는 존재한다.
                // 'Home'키와 같은 효과를 만들어본다.
                Intent intent = new Intent(Intent.ACTION_MAIN);
                intent.addCategory(Intent.CATEGORY_HOME);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
        });
    }
}

여기서, Intent를 구성하는데 사용한 요소들이 있는데 자세한 내용이 궁금하여 찾아보았다.

Intent.ACTION_MAIN : 시작 Activity를 지정하기 위한 옵션이다.

Intent.CATEGORY_HOME : 보여주기 위한 Activity 화면을 Home화면으로 한다.

Intent.FLAG_ACTIVITY_NEW_TASK : 새로운 Task를 생성하고 Activity를 추가할 경우 사용한다. 사용중인 Task들중 동일한 Activity를 가지고 있는 Task가 있다면 대치된다.

 

 

 

 

 

 

결과

 

 

 

 

 

 

 

 

 

 

 

[Andriod] 안드로이드 홈 버튼 효과 만들기


@Junesker

 

반응형

댓글