Wednesday 17 August 2016

Rating Bar

Rating Bar

Rating Bar is used to take a rating for application or services like -ola, flipkart.  A Rating Bar show a rating in star. The user can touch/drag or use arrow keys to set the rating when using the default size RatingBar.
Code -
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
android:orientation="vertical"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
>
    <
RatingBar
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content" />
</
LinearLayout>

Java COde
public class MainActivity extends AppCompatActivity {



    LinearLayout layout1;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);



        layout1=(LinearLayout)findViewById(R.id.ll1);



        RatingBar ratingBar=new RatingBar(this);

        ratingBar.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT));



        layout1.addView(ratingBar);

    }

}


isIndicator
Rating bar is an look like as a indicator with  non-changeablity. When android:isIndicator="true" then  user pressing their thumb on rating bar, nothing will happen.
By default, isIndicator value is 'false'.
Must be a boolean value, either "true" or "false".
CODE
 
XML
android:isIndicator="true"
Java
RatingBar ratingBar1=(RatingBar)findViewById(R.id.ratingBar1);

ratingBar1.setIsIndicator(true);

Program - 1
XML
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    >

    <RatingBar

        android:id="@+id/ratingBar1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        />

    <Button

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="isIndicator"

        android:onClick="onIndicator"

        />

</LinearLayout>
Java
public class MainActivity extends AppCompatActivity {



    RatingBar ratingBar1;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);



        ratingBar1=(RatingBar)findViewById(R.id.ratingBar1);

        ratingBar1.setIsIndicator(true);





    }

    public void onIndicator(View view)

    {

        if (ratingBar1.isIndicator())

        {

            Toast.makeText(this,"isIndicator is true",Toast.LENGTH_SHORT).show();

        }else

        {

            Toast.makeText(this,"isIndicator is false",Toast.LENGTH_SHORT).show();

        }

    }

}
*******************************************************

numStars
It define number of stars (or rating items) to show to user. 
Note : if you take android:width="wrap_content" then 56 star is showing.
IF you make
Must be an integer value, such as "100".
 syntax - 
XML
 
android:numStars="5"
Java
setNumStars
Set no. of star at runtime.
RatingBar ratingBar1=(RatingBar)findViewById(R.id.ratingBar1);

ratingBar1.setNumStars(3);

getNumStars
Get no. of star define by programmer  .
int i=ratingBar1.getNumStars();
 
Program - 1
XML
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    >

    <RatingBar

        android:id="@+id/ratingBar1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        />

    <TextView

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:id="@+id/tv1"

        android:textSize="30dp"

        android:hint="Hint"

        />



</LinearLayout>

JAva
public class MainActivity extends AppCompatActivity {



    RatingBar ratingBar1;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);



        TextView textView1=(TextView)findViewById(R.id.tv1);

        RatingBar ratingBar1=(RatingBar)findViewById(R.id.ratingBar1);

        ratingBar1.setNumStars(6);

        int i=ratingBar1.getNumStars();

        String nostar=String.valueOf(i);

        textView1.setText(nostar);



    }

}
****************************************
Rating
This is use to set default rating.
Must be a floating point value, such as "1.5".
code -
XML
 android:rating="1.5"
 
JAva

Set Rating
ratingBar1.setRating(3);
 
Get Rating
float i=ratingBar1.getRating();

Program - 1
XML
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    >

    <RatingBar

        android:id="@+id/ratingBar1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        />

    <TextView

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:id="@+id/tv1"

        android:textSize="30dp"

        android:hint="Hint"

        />

    

</LinearLayout>
 
JAva
public class MainActivity extends AppCompatActivity {



    RatingBar ratingBar1;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);



        TextView textView1=(TextView)findViewById(R.id.tv1);

        RatingBar ratingBar1=(RatingBar)findViewById(R.id.ratingBar1);

        ratingBar1.setRating(3);

        float i=ratingBar1.getRating();

        String nostar=String.valueOf(i);

        textView1.setText(nostar);



    }

}
 

stepSize
The step size of the rating. It define that minimum  difference between two value of rating.
Must be a floating point value, such as "1.2".
Syntax - 
XML
android:stepSize="1"

JAva

Set Step Size
ratingBar1.setStepSize(1.5f); //1.5f is float value

Get Step Size
float i=ratingBar1.getStepSize();

Program - 1
XML
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    >

    <RatingBar

        android:id="@+id/ratingBar1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        />

    <TextView

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:id="@+id/tv1"

        android:textSize="30dp"

        android:hint="Hint"

        /> 

</LinearLayout>

JAva
public class MainActivity extends AppCompatActivity {



    RatingBar ratingBar1;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);



        TextView textView1=(TextView)findViewById(R.id.tv1);

        RatingBar ratingBar1=(RatingBar)findViewById(R.id.ratingBar1);

        ratingBar1.setStepSize(1.5f); //1.5f is float value

        float i=ratingBar1.getStepSize();

        String nostar=String.valueOf(i);

        textView1.setText(nostar);





    }

 

}
*****************************************
setOnRatingBarChangeListener
Sets the listener to be called when the rating changes.
XML
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    >

    <RatingBar

        android:id="@+id/ratingBar1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        />

    <TextView

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:id="@+id/tv1"

        android:textSize="30dp"

        android:hint="Hint"

        />

</LinearLayout>

Java
public class MainActivity extends AppCompatActivity {



    RatingBar ratingBar1;

    TextView textView1;



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);



       textView1=(TextView)findViewById(R.id.tv1);

       ratingBar1=(RatingBar)findViewById(R.id.ratingBar1);



        ratingBar1.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {

            @Override

            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {

                float i=ratingBar1.getRating();

                String strRating=String.valueOf(i);

                textView1.setText(strRating);

            }

        });

    }

}





































22

No comments:

Post a Comment