Tuesday, 20 January 2015

RatingBar Example1

RatingBar Example in android





strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Task1.Ratingbar</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="tv1"></string>
    <string name="title_activity_main2">Main2</string>
     <string name="tv2"></string>

</resources>

activity_main.xml

<RelativeLayout 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"
    tools:context=".MainActivity" >

    <RatingBar
        android:id="@+id/ratingBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="68dp" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/ratingBar1"
        android:layout_below="@+id/ratingBar1"
        android:layout_marginTop="33dp"
        android:text="@string/tv1"/>
  
</RelativeLayout>

MainActivity.java
import android.os.Bundle;
import android.app.Activity;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.TextView;

public class MainActivity extends Activity implements OnRatingBarChangeListener {

       TextView tv1;
      
@Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
             
              tv1=(TextView)findViewById(R.id.textView1);
             
              RatingBar ratingBar=(RatingBar)findViewById(R.id.ratingBar1);
               ratingBar.setOnRatingBarChangeListener(this);
                             
       }

       @Override
public void onRatingChanged(RatingBar ratingBar, float rating,
                     boolean fromUser) {
              // TODO Auto-generated method stub
             
             
              float rate= ratingBar.getRating();
        
              tv1.setText("Your rating is:  "+rate);
       }

}

No comments:

Post a Comment