Reputation: 100
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);
}`
Upvotes: 2
Views: 1202
Reputation: 7560
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);
Upvotes: 4
Reputation: 9267
Try this :
rate.setRating(Float.valueOf(FO.nos[i]));
Edit for Dynamic Seekbar :
public class MovieAdapter extends RecyclerView.Adapter<MovieAdapter.ViewHolder> {
Context context;
List<PostVO> movieList;
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
LinearLayout ll;
RatingBar rb;
public ViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
ll = (LinearLayout) itemView.findViewById(R.id.ll);
rb = new RatingBar(context);
rb.setNumStars(10);
rb.setStepSize(1);
ll.addView(rb);
}
@Override
public void onClick(View v) {
Snackbar.make(v, movieList.get(getAdapterPosition()).getTitle(), Snackbar.LENGTH_SHORT).show();
}
}
public MovieAdapter(Context context,List<PostVO> movieList)
{
this.context = context;
this.movieList = movieList;
}
@Override
public MovieAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_movie_list, viewGroup, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(MovieAdapter.ViewHolder viewHolder, int i) {
viewHolder.rb.setRating(Float.valueOf(movieList.get(i).getRating()));
}
@Override
public int getItemCount() {
if(movieList != null && !movieList.isEmpty())
{
return movieList.size();
}
else {
return 0;
}
}
}
Upvotes: 1
Reputation: 109
You need to put following code for this-
RatingBar ratingBar = new RatingBar(this);
ratingBar.setRating(5);
Upvotes: 0
Reputation: 1355
Just RatingBar should be placed in the layout, which has a property wrap_content. Magic does not exist. Check that returns:
Integer.parseInt(FO.nos[i])
Suddenly there is 0. See it so.
Upvotes: 1
Reputation: 7686
Try .setRating(float value) and here your have a related question: Set value in RatingBar
There you have the correct implementation of a RatingBar and the setRating method.
Upvotes: 1