Search code examples
androidandroid-linearlayoutratingbar

setNumStars() not working in android


I have to dynamically create rating bar in which the the number of stars to be set is passed from the server. Here i'm getting the value of number of stars to be set and its assigned to setNumStars() method. My code and the design generated are shown below. Here i'm getting n number of stars.Thanks in advance. `

 if(FO.answerType[i].equals("S")){          
    TextView quest = new TextView(this);

     quest.setText(FO.question[i]);
     quest.setTextSize(textsize);
     quest.setTypeface(Typeface.DEFAULT_BOLD);                  
     quest.setTextColor(Color.BLACK);
     quest.setPadding(10, 6, 10, 6);
     quest.setGravity(Gravity.LEFT);
     lila.addView(quest);

     RatingBar rate = new RatingBar(this);                  
     rate.setNumStars(Integer.parseInt(FO.nos[i]));     

     lila.addView(rate); 


        }` 

enter image description here


Solution

  • Keep the Layout Params as WRAP_CONTENT. It will do the trick, like

    FrameLayout frameLayout = (FrameLayout) findViewById(R.id.container);
            RatingBar rate = new RatingBar(this);
            rate.setRating(2);
            rate.setNumStars(4);
            rate.setProgress(1);
            rate.setLayoutParams(new FrameLayout.LayoutParams(
                    FrameLayout.LayoutParams.WRAP_CONTENT,
                    FrameLayout.LayoutParams.WRAP_CONTENT));
            frameLayout.addView(rate);
    

    Output : enter image description here