Membuat Ratingbar android


kali ini kita akan membuat ratingbar, biasanya digunakan untuk input suatu penilaian
yang perlu diketahui dari properti file xmlnya, lihat pada property Ratingbar
ratingbar2

numStars :menentukan jumlah bintang
stepsize: menetukan nilai setiap bintang
Rating :menentukan nilai awal bintang yang tepilih pada saat awal program di eksekusi

ok karena saya malas membuat kodenya kode ini saya ambil dari  http://www.mkyong.com/android/android-rating-bar-example/
file xmlnya


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/LinearLayout1"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="vertical"
 tools:context=".MainActivity" >
 <TextView
 android:id="@+id/lblRateMe"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Rate Me"
 android:textAppearance="?android:attr/textAppearanceMedium" />

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

 <Button
 android:id="@+id/btnSubmit"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Submit" />

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

 <TextView
 android:id="@+id/lblResult"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Result : "
 android:textAppearance="?android:attr/textAppearanceLarge" />

 <TextView
 android:id="@+id/txtRatingValue"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text=""
 android:textAppearance="?android:attr/textAppearanceSmall" />

 </LinearLayout>

</LinearLayout>

file javanya


package com.ratingbar;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
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 txtRatingValue;
 private Button btnSubmit;

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

addListenerOnRatingBar();
 addListenerOnButton();
 }

public void addListenerOnRatingBar() {

 ratingBar = (RatingBar) findViewById(R.id.ratingBar);
 txtRatingValue = (TextView) findViewById(R.id.txtRatingValue);

 //if rating value is changed,
 //display the current rating value in the result (textview) automatically
 ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
 public void onRatingChanged(RatingBar ratingBar, float rating,
 boolean fromUser) {

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

 }
 });
 }

 public void addListenerOnButton() {

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

 //if click on me, then display the current rating value.
 btnSubmit.setOnClickListener(new OnClickListener() {

 @Override
 public void onClick(View v) {

 Toast.makeText(MainActivity.this,
 String.valueOf(ratingBar.getRating()),
 Toast.LENGTH_SHORT).show();

 }

 });

 }
}

penampakannya

ratingbar
download kodenya
semoga berguna 🙂

Iklan

tinggalkan pesan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: