Search code examples
javaandroidxmltextwatcherratingbar

Rating bar, how to get the number of stars?


I got an edit text and also a rating bar in my program. I am checking if the user has more than 3 characters in the edit text and for rating bar it should have something greater than 0.5 star (basically to check if they at least clicked on any of them).

    final EditText commentbox = (EditText)findViewById(R.id.comment);
    final RatingBar rating = (RatingBar)findViewById(R.id.rating);
    final Button feedbackbutton = (Button)findViewById(R.id.submit);


    final TextWatcher watcher = new TextWatcher(){

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {

            if(commentbox.length() >= 3 && rating.getNumStars() >=0.5){
                feedbackbutton.setEnabled(true);
                feedbackbutton.setBackgroundColor(Color.parseColor("#369742"));
            } else  {
                feedbackbutton.setEnabled(false);
                feedbackbutton.setBackgroundColor(Color.parseColor("#ffcacaca"));
            }


        }

    };
    commentbox.addTextChangedListener(watcher);
    rating.setOnRatingBarChangeListener((this));

So as you can see it has to meet those condition in order for a button to become enabled and also to change its background colour. However soon as I write more than 3 characters in the edit text, the button enables itself and changes its colour. It's not looking for the rating bar condition.

Can someone help me please


Solution

  •  feedbackbutton.setOnClickListener(new OnClickListener(){  
    
                @Override  
                public void onClick(View arg0) {  
                    //Getting the rating and displaying it on the toast  
                    String rating=String.valueOf(rating.getRating());  
                    Toast.makeText(getApplicationContext(), rating, Toast.LENGTH_LONG).show();  
                }  
    
            });
    

    Hope it solves your problem.