Android | Rating Bar örneği

Written by Super User. Posted in Android Anlatımlar

Ekran görüntümüz;

Android uygulamanızda, kullanıcıya oylama yaptırabilmeniz için size basit bir çözüm olarak sunulan Rating Bar örneğimizi inceleyelim;

main.xml dosyamız

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/oylaTV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Oyla"
        />

    <RatingBar
        android:id="@+id/ratingBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:numStars="5"
        android:stepSize="1.0" />

    <Button
        android:id="@+id/gonderButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Gönder" />

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/sonucTV"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Sonuç : "
             />

        <TextView
            android:id="@+id/sonucCiktiTV"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
             />
    </LinearLayout>

</LinearLayout>

MainActivity.java dosyamız

package com.gokselguren.ratingbar;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
	private RatingBar ratingBar;
	private TextView ratingDegeri;
	private Button gonderButton;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		addListenerOnRatingBar();
		addListenerOnButton();

	}

	public void addListenerOnRatingBar() {

		ratingBar = (RatingBar) findViewById(R.id.ratingBar);
		ratingDegeri = (TextView) findViewById(R.id.sonucCiktiTV);

		//Rating değiştiği anda bunu TextView'da görüntüle
		ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
			public void onRatingChanged(RatingBar ratingBar, float rating,
					boolean fromUser) {

				ratingDegeri.setText(String.valueOf(rating));

			}
		});
	}

	public void addListenerOnButton() {

		ratingBar = (RatingBar) findViewById(R.id.ratingBar);
		gonderButton = (Button) findViewById(R.id.gonderButton);

		
		gonderButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				//Buttona tıklandığında o anki rating değerini içeren bir toast mesajı göster
				Toast.makeText(MainActivity.this,
						String.valueOf(ratingBar.getRating()),
						1).show();

			}
		});

	}
}