본문 바로가기
안드로이드 자바

[Android][Java] 온보딩 페이지 만들기

by teamnova 2023. 5. 15.
728x90

안녕하세요 !

이번 시간에는 앱 실행 시에 앱에 관한 설명들을 페이지 형태로 넘겨가면서 보여주는 형태인

온보딩(Onboarding) 페이지를 만드는 방법에 대하여 알아보겠습니다 !

참조 문서

https://github.com/Ramotion/paper-onboarding-android

 

GitHub - Ramotion/paper-onboarding-android: PaperOnboarding is a material design slider made by @Ramotion

:octocat: PaperOnboarding is a material design slider made by @Ramotion - GitHub - Ramotion/paper-onboarding-android: PaperOnboarding is a material design slider made by @Ramotion

github.com

먼저 gradle에 라이브러리를 등록해줍니다.

 

build.gradle(Module:프로젝트명:app)
dependencies 괄호 안에 아래 코드를 넣어주세요.

implementation 'com.ramotion.paperonboarding:paper-onboarding:1.1.3'

setting.gradle
repositories 괄호 안에 아래 코드를 넣어주세요.

maven { url 'https://jitpack.io'}

 

다음으로 레이아웃 파일 입니다.

 

 activity_onboarding.xml (프래그먼트(Fragment) 보여줄 프레임 레이아웃(FrameLayout))

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:orientation="vertical"
    tools:context=".OnBoardingAcitivty">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/containers"/>

</LinearLayout>

 

fragment_onboarding.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="30sp"
        android:gravity="center"
        android:text="온보딩 테스트" />

</FrameLayout>

 

다음으로 자바 파일입니다.

OnBoardingActivity.java

import android.graphics.Color;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;

import com.ramotion.paperonboarding.PaperOnboardingFragment;
import com.ramotion.paperonboarding.PaperOnboardingPage;
import com.ramotion.paperonboarding.listeners.PaperOnboardingOnRightOutListener;

import java.util.ArrayList;

public class OnBoardingAcitivty extends AppCompatActivity {

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

        //1. 슬라이드용 화면 준비
        PaperOnboardingPage scr1 = new PaperOnboardingPage("Hotels",
                "All hotels and hostels are sorted by hospitality rating",
                Color.parseColor("#678FB4"), R.drawable.hotel, R.drawable.key);
        PaperOnboardingPage scr2 = new PaperOnboardingPage("Banks",
                "We carefully verify all banks before add them into the app",
                Color.parseColor("#65B0B4"), R.drawable.bank, R.drawable.wallet);
        PaperOnboardingPage scr3 = new PaperOnboardingPage("Stores",
                "All local stores are categorized for your convenience",
                Color.parseColor("#9B90BC"), R.drawable.store, R.drawable.cart);

        ArrayList<PaperOnboardingPage> elements = new ArrayList<>();
        elements.add(scr1);
        elements.add(scr2);
        elements.add(scr3);

        //2. 화면을 온보드 프래그먼트에 넣는다.
        PaperOnboardingFragment onBoardingFragment = PaperOnboardingFragment.newInstance(elements);

        //3. 프래그먼트 화면설정 완료
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.add(R.id.containers, onBoardingFragment);
        fragmentTransaction.commit();

        //4. 온보드 완료시에 보여줄 프래그먼트 설정
        onBoardingFragment.setOnRightOutListener(new PaperOnboardingOnRightOutListener() {
            @Override
            public void onRightOut() {
                FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
                OnBoardingFragment fm = new OnBoardingFragment();
                fragmentTransaction.replace(R.id.containers, fm);
                fragmentTransaction.commit();
            }
        });
    }//onCreate

}//OnBoardingAcitivty

 

OnBoardingFragment.java

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;

public class OnBoardingFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_onboarding, container, false);
    }
}

 

아이콘 res -> drawable

hotel.png
0.00MB
bank.png
0.00MB
key.png
0.00MB
store.png
0.00MB
wallet.png
0.00MB
cart.png
0.01MB

 

다음으로 실행화면 입니다.

 

 

궁금한 내용은 언제든지 댓글로 질문해주세요 !

감사합니다 !