Search code examples
androidrating

what are android's UI guidelines regarding ratingbar


Are there any guidelines regarding ratingbars for android? I'm developing an android application and the way I've developed it, I can only set and edit the rating of something by adding it to a dialog box.

I wanted to know if there are any guidelines regarding the ratingbar - is it okay to have it in a dialog box? Or should I do a work-around?


Solution

  • My concern was having my rating bar within an adapter class (as seen below). I wanted to be able to get the position of the user that the rating bar is linked to. The only work-around I could think of that could do that is by putting it in a dialog box (which is the reason for my question). I solved my problem (of how to access the position from an innner class - ) by making the onRatingChanged by making position in getView to be final. Read here: Making paramters final about final attributes. Is really helpful.

    Adapter Class

    public class PersonAdapter extends ArrayAdapter<Person> {
    private Context context;
    private List<Person> people;
    
    public PersonAdapter(Context context, List<Person> people) {
        super(context, R.layout.person_layout, people);
        this.context = context;
        this.people = people;
    }
    
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) 
    {
        // if this is a new view, then inflate it
        View personView = convertView;
    
        if (personView == null) 
        {
            // get the inflater that will convert the person_layout.xml file
            // into an actual object (i.e. inflate it)
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
            // create a view to display the person's info
            personView = inflater
                    .inflate(R.layout.person_layout, parent, false);
    
            TextView txt1stTime = (TextView) personView
                    .findViewById(R.id.txt1stTime);
            txt1stTime.setText("1st time");
        } 
        else 
        {
            TextView txt1stTime = (TextView) personView
                    .findViewById(R.id.txt1stTime);
            txt1stTime.setText("Recycled");
        }
    
        // keep track of person this view is working with
        personView.setTag(people.get(position));
    
        // get text views that will hold strings
        TextView txtSurname = (TextView) personView
                .findViewById(R.id.txtSurname);
        TextView txtFirstname = (TextView) personView
                .findViewById(R.id.txtFirstname);
        RatingBar ratingBar = (RatingBar) personView
                .findViewById(R.id.ratingBar1);
    
    
         //Once selected, get the question which the rating is in 
        //Start asynctask and get all the details for that question to update
        //Send and update rating
        ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener()
        {
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser)
            {
                ratingBar.setRating(rating);
                String name = people.get(position).surname;
                Toast.makeText(getContext(),
                        "Rating set to:  " + rating + " for the position: " + position + " and user: " + name, Toast.LENGTH_SHORT).show();   
    
            }
        });
    
        // set text fields
        txtSurname.setText(people.get(position).surname);
        txtFirstname.setText(people.get(position).firstName);
    
        float rate = ratingBar.getRating();
        Toast.makeText(getContext(),
                "Rating is: " + rate, Toast.LENGTH_LONG).show();
        people.get(position).rating = rate;
        System.out.print(people.get(position).rating);
        // return view to ListView to display
        return personView;
    }
    

    I know my answer is not directly linked to my actual question, but if anyone ever comes across the same problem of rather using a dialog box, there is a solution.