Rating Example

Rating is important for all developers because he wants to know how the developed work useful for users. Rating has been varied from 1 to 5. We can use simple example for show rating an app.

MainActivity.java

This class is used to display rating of user.

    package com.example.codebridgeratingexample;  
      
    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.Toast;  
      
    public class MainActivity extends Activity {  
        RatingBar ratingbar1;  
        Button button;  
        @Override  
        protected void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
            setContentView(R.layout.activity_main);  
            addOnButtonClick();  
        }  
      
        public void addOnButtonClick(){  
            ratingbar1=(RatingBar)findViewById(R.id.ratingBar1);  
            button=(Button)findViewById(R.id.button1);  
            //set Listener on Button Click  
            button.setOnClickListener(new OnClickListener(){  
      
                @Override  
                public void onClick(View arg0) {  
                      
                    String rating= String.valueOf(ratingbar1.getRating());  
                    Toast.makeText(getApplicationContext(), rating, Toast.LENGTH_LONG).show();  
                }  
                  
            });  
        }  
        @Override  
        public boolean onCreateOptionsMenu(Menu menu) {  
            // Inflate the menu; this adds items to the action bar if it is present.  
            getMenuInflater().inflate(R.menu.activity_main, menu);  
            return true;  
        }  
      
    }  

activity_main.xml

This xml has one rating Bar and button. Its easily drag and drop from pallete of android studio.