C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Android RatingBar ExampleAndroid RatingBar can be used to get the rating from the user. The Rating returns a floating-point number. It may be 2.0, 3.5, 4.0 etc. Android RatingBar displays the rating in stars. Android RatingBar is the subclass of AbsSeekBar class. The getRating() method of android RatingBar class returns the rating number. Android RatingBar ExampleLet's see the simple example of rating bar in android. activity_main.xmlDrag the RatingBar and Button from the pallete, now the activity_main.xml file will like this: File: activity_main.xml
Activity classLet's write the code to display the rating of the user. File: MainActivity.java
package example.TheDeveloperBlog.com.ratingbar; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.RatingBar; import android.widget.Toast; public class MainActivity extends AppCompatActivity { RatingBar ratingbar; Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); addListenerOnButtonClick(); } public void addListenerOnButtonClick(){ ratingbar=(RatingBar)findViewById(R.id.ratingBar); button=(Button)findViewById(R.id.button); //Performing action on Button Click button.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View arg0) { //Getting the rating and displaying it on the toast String rating=String.valueOf(ratingbar.getRating()); Toast.makeText(getApplicationContext(), rating, Toast.LENGTH_LONG).show(); } }); } } Output:
Next TopicAndroid WebView Example
|