AndroidでRatingBarを表示する


activity_main.xml
    <RatingBar
        android:id="@+id/rating"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:numStars="6"
        android:rating="4"
        android:stepSize="0.5"
        />
MainActivity.java
public class MainActivity extends AppCompatActivity {

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

        RatingBar rate = (RatingBar) findViewById(R.id.rating);
        rate.setOnRatingBarChangeListener(
            new RatingBar.OnRatingBarChangeListener(){
                public void onRatingChanged(
                        RatingBar ratingBar,
                        float rating,
                        boolean fromUser){
                    Toast.makeText(
                            MainActivity.this,
                            String.format("現在の評価は%f", rating),
                            Toast.LENGTH_SHORT).show();
                }
            }
        );
    }

}