-2

I am trying to add rating bar in my code. I am using onRatingBarChangeListner to get the values of rating bar. But rating bar is not responding at all. It dose not get selected or change color.

public class SurveyActivity extends AppCompatActivity implements getQuestionsAsyncTask.GetQuestions,RatingBar.OnRatingBarChangeListener{

private ArrayList<Question> mQuestionsArrayList;
private TextView mTextQuestion1,mTextQuestion2;
private RatingBar ratingbar1;


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

 //   addListenerOnButtonClick();

    mTextQuestion1 = (TextView)findViewById(R.id.textQuestion1);
    mTextQuestion2 = (TextView)findViewById(R.id.textQuestion2);

    Button next = (Button)findViewById(R.id.btn_next);

    final RatingBar ratingBar = (RatingBar)findViewById(R.id.MyRating);

   next.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            float rating=ratingBar.getRating();
            Toast.makeText(getApplicationContext(),"Your Selected Ratings  : " + String.valueOf(rating),Toast.LENGTH_LONG).show();

         //   startActivity(new Intent(SurveyActivity.this,UserDetailsActivity.class));
        }
    });



    mQuestionsArrayList = new ArrayList<>();

    getQuestionsAsyncTask getQuestionsAsyncTask = new getQuestionsAsyncTask(this,this,mQuestionsArrayList);
    getQuestionsAsyncTask.execute();
}
public void onRatingChanged(RatingBar ratingBar, float rating,
                            boolean fromUser) {

    Toast.makeText(getApplicationContext(),"Your Selected Ratings  : " + String.valueOf(rating),Toast.LENGTH_LONG).show();

}

Tried this but dose not work.

    <RatingBar
        android:id="@+id/MyRating"
        style="?android:attr/ratingBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:isIndicator="true"
        android:numStars="5"
        android:stepSize="0.1"
        android:layout_above="@+id/textQuestion2"
        android:layout_alignParentEnd="true"
        android:layout_marginLeft="10dp"
        android:layout_marginStart="10dp"
        android:layout_alignParentRight="true" />

Also tried this tutorial

What's going wrong? Can anyone help please? Thank you..

EDIT: Tried both the answers still it's not working:

public class SurveyActivity extends AppCompatActivity implements getQuestionsAsyncTask.GetQuestions,RatingBar.OnRatingBarChangeListener{

    private ArrayList<Question> mQuestionsArrayList;
    private TextView mTextQuestion1,mTextQuestion2;
    private RatingBar ratingbar1;


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

        Button next = (Button)findViewById(R.id.btn_next);

        final RatingBar ratingBar = (RatingBar)findViewById(R.id.MyRating);
        ratingBar.setOnRatingBarChangeListener(this);


       next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                float rating=ratingBar.getRating();
                Toast.makeText(getApplicationContext(),"Your Selected Ratings  : " + String.valueOf(rating),Toast.LENGTH_LONG).show();

            }
        });
    }
    @Override
    public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {

            v = ratingBar.getRating();

            Toast.makeText(getApplicationContext(), "Your Selected Ratings  : " + String.valueOf(v), Toast.LENGTH_LONG).show();

    }
Nikhil
  • 3,711
  • 8
  • 32
  • 43
Sid
  • 2,792
  • 9
  • 55
  • 111

2 Answers2

2

Your onRatingChanged should be as follows

@Override
public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {

}

Also add setOnRatingBarChangeListener on your ratingBar

ratingBar.setOnRatingBarChangeListener(this);

Edit 1 : For Toast try following

String rating=String.valueOf(ratingBar.getRating());  
Toast.makeText(getApplicationContext(), rating, Toast.LENGTH_LONG).show();  

Edit 2 : You are not able to change rating because of isIndicator property. As per android's documentation

android:isIndicator Whether this rating bar is an indicator (and non-changeable by the user).

so remove that property and change your ratingBar to following

<RatingBar
        android:id="@+id/MyRating"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:numStars="5"
        android:stepSize="0.1"
        android:layout_above="@+id/textQuestion2"
        android:layout_alignParentEnd="true"
        android:layout_marginLeft="10dp"
        android:layout_marginStart="10dp"
        android:layout_alignParentRight="true" />

Also why I removed line

style="?android:attr/ratingBarStyleSmall"

refer this

Community
  • 1
  • 1
Nikhil
  • 3,711
  • 8
  • 32
  • 43
1

you forgot to set the Change listener on the ratingBar view,

final RatingBar ratingBar = (RatingBar)findViewById(R.id.MyRating);
ratingBar.setOnRatingBarChangeListener(this);
petey
  • 16,914
  • 6
  • 65
  • 97