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

[Java][Android] RatingBar을 사용해서 별점만들기

by teamnova 2024. 9. 20.
728x90

RatingBar는 앱 사용시 주로 사용자가 특정항목이나 경험에 점수를 주는것을 볼 수 있습니다.

이는 별점으로 표시되고 사용자가 직관적으로 해당 항목에 만족도를 표현 할 수 있는 기능입니다.

 

예를들면 상품리뷰의 별점을 줄 수 있습니다

  • 전자상거래 앱에서 사용자들이 구매한 상품에 대해 별점을 부여할 수 있습니다. 예를 들어, 사용자가 구매한 전자기기에 대해 1~5개의 별을 부여하여 만족도를 나타낼 수 있습니다. 이 별점은 다른 잠재 구매자들에게 해당 제품의 품질에 대한 인사이트를 제공합니다.

영화 또는 TV 프로그램 평가를 할 수 있습니다.

  • 스트리밍 서비스나 영화 리뷰 애플리케이션에서, 사용자들이 영화를 시청한 후 별점을 매길 수 있습니다. 이는 다른 사용자가 영화를 선택할 때 참고할 수 있는 중요한 정보가 됩니다.

설문 또는 피드백 점수를 줄수 있습니다.

  • 고객 설문 조사에서 사용자에게 별점을 통해 특정 항목에 대한 만족도를 평가하도록 요구할 수 있습니다. 예를 들어, "이번 상담에 대해 얼마나 만족하셨습니까?"라는 질문에 별점을 매기도록 할 수 있습니다.

RatingBar는 사용자 경험을 반영하는 중요한 도구로, 사용자 피드백을 시각적이고 직관적인 방법으로 수집할 수 있는 방법입니다. 사용자 참여를 유도하고, 다른 사용자나 관리자에게 유용한 정보를 제공하는 데 효과적입니다.

 

MainActivity

 


import android.os.Bundle;
import android.widget.RatingBar;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

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

        // RatingBar와 TextView를 가져옴
        RatingBar ratingBar = findViewById(R.id.ratingBar);
        TextView ratingText = findViewById(R.id.ratingText);

        // RatingBar의 초기 값을 3점으로 설정
        ratingBar.setRating(3.0f);
        ratingText.setText("사용자 평가 점수: " + ratingBar.getRating());

        // RatingBar의 값이 변경될 때마다 호출되는 리스너 설정
        ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                // 별점이 변경되면 TextView에 새로운 값을 표시
                ratingText.setText("사용자 평가 점수: " + rating);
            }
        });
    }
}

 

activity_main.xml

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

    <!-- RatingBar 추가 -->
    <!-- true로 설정하면 별점을 변경할 수 없습니다. -->
    <!-- 별점 증가 단위 -->
    <!-- 초기 별점 -->
    <!-- 총 별 개수 -->
    <RatingBar
        android:id="@+id/ratingBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:isIndicator="false"
        android:numStars="5"
        android:rating="3"
        android:stepSize="1" />

    <!-- 별점 표시 TextView -->
    <TextView
        android:id="@+id/ratingText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/ratingBar"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:textSize="18sp" />

</RelativeLayout>

 

 시연 영상