Wednesday, March 19, 2014

RatingBar madness and solution!

I've been working with Android recently and I just ran into an irritating "quark" with the RatingBar object and I couldn't find a solution through the usual googling and StackExchange, fortunately I stumbled upon a solution!

The Problem:
The ratingBar has a numStars setting which should indicate the maximum number of stars to display for  a rating. In my instance I have this value set to 5, only problem is it displays a many stars and it can possibly fit into the viewable space.. somewhere around 8 stars, uh... hello, I set it to 5!  I love it when things are easy!


This wont do, So here's the code, perfectly reasonable stuff here. (For details on how to do a custom style check out this great blog post, no point in me rewriting this one.)

 <RatingBar
    android:id="@+id/rtbDvcMgmt"
    style="@style/CustomRatingBar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="25dp"
    android:layout_marginTop="5dp"
    android:max="5"
    android:maxHeight="50dp"
    android:minHeight="28dp"
    android:numStars="5"
    android:rating="0.0"

    android:stepSize="1.0" />

This code has nothing wrong with it, so why is it no working as expected? The explanation can be found in the RatingBar source code java doc! 

When using a RatingBar that supports user interaction, placing widgets to the left or right of the RatingBar is discouraged.
What is meant by "discouraged" is it wont work! All sorts of things go wrong, the stars don't fill properly, there are more stars displayed then you requested... Yes don't do this, it doesn't work.

The cause in my case was that I was using a TextView next to the RatingBar because that's the requirement! I also had the whole form laid out in a Tablerow which doesn't play nicely with RatingBar either as I discovered when I moved the RatingBar onto its own row. 

The Solution!
The solution is surprisingly simple. The RatingBar works when it's all by itself snuggled up in a Layout with nothing to the right or left. so I wrapped it in its own LinearLayout INSIDE of the TableRow with a TextView right next to it! That did the Trick! 



<TableRow
       android:id="@+id/tableRowDvcMgmt"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:padding="5sp" >

       <TextView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           ... />
                
       <LinearLayout
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical" >
                       <RatingBar
                           android:id="@+id/rtbDvcMgmt"
                           style="@style/CustomRatingBar"
                           android:layout_width="wrap_content"
                           android:layout_height="wrap_content"
                           android:layout_marginRight="25dp"
                           android:layout_marginTop="5dp"
                           android:max="5"
                           android:maxHeight="50dp"
                           android:minHeight="28dp"
                           android:numStars="5"
                           android:rating="0.0"
                           android:stepSize="1.0" />
       </LinearLayout>
</TableRow>

61 comments:

  1. Thanks bro you are just superb.......

    ReplyDelete
  2. I would like to thank you for your nicely written post, its informative and your writing style encouraged me to read it till end. Thanks
    python training institute in chennai
    python training in Bangalore
    python training in pune

    ReplyDelete
  3. Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.

    Data Science training in Chennai
    Data science training in bangalore
    Data science online training
    Data science training in pune

    ReplyDelete
  4. Do you have a spam issue on this website; I also am a blogger, and I wanted to know your situation; many of us have developed some nice methods, and we are looking to trade methods with others, why not shoot me an e-mail if interested.
    nebosh igc courses in chennai

    ReplyDelete
  5. I read this post two times, I like it so much, please try to keep posting & Let me introduce other material that may be good for our community.
    Microsoft Azure online training
    Selenium online training
    Java online training
    Java Script online training
    Share Point online training

    ReplyDelete

  6. Your very own commitment to getting the message throughout came to be rather powerful and have consistently enabled employees just like me to arrive at their desired goals.
    Java Training in Chennai | Best Java Training in Chennai
    C C++ Training in Chennai | Best C C++ Training in Chennai

    ReplyDelete
  7. An amazing blog, it is very useful and Excellent Blog! I would like to say thanks for the efforts you have made in writing this post.


    Data Science in Bangalore

    ReplyDelete
  8. Gangaur Realtech is a professionally managed organisation specializing in real estate services where integrated services are provided by professionals to its clients seeking increased value by owning, occupying or investing in real estate.
    date analytics certification training courses
    data science courses training

    ReplyDelete
  9. I just couldn't leave your website before telling you that I truly enjoyed the top quality info you present to your visitors? Will be back again frequently to check up on new posts.
    machine learning course malaysia

    ReplyDelete



  10. Great post i must say and thanks for the information. Education is definitely a sticky subject. However, is still among the leading topics of our time. I appreciate your post and look forward to more.

    DATA SCIENCE COURSE MALAYSIA

    ReplyDelete
  11. Attend The Python training in bangalore From ExcelR. Practical Python training in bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Python training in bangalore.
    python training in bangalore

    ReplyDelete
  12. Attend The Digital Marketing courses in bangalore From ExcelR. Practical Digital Marketing courses in bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Digital Marketing courses in bangalore.
    Digital Marketing Courses in Bangalore

    ReplyDelete
  13. I really enjoy simply reading all of your weblogs. Simply wanted to inform you that you have people like me who appreciate your work. Definitely a great post. Hats off to you! The information that you have provided is very helpful.
    rpa training in malaysia

    ReplyDelete
  14. Attend The Data Science Course in Bangalore From ExcelR. Practical Data Science Course in Bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Data Science Course in Bangalore.
    ExcelR Data Science Course in Bangalore

    ReplyDelete
  15. Visit for AWS training in Bangalore :- AWS training in Bangalore

    ReplyDelete
  16. Very interesting blog Thank you for sharing such a nice and interesting blog and really very helpful article.sap mm Training in Bangalore

    ReplyDelete
  17. Very useful and information content has been shared out here, Thanks for sharing it.sap hr Training in Bangalore

    ReplyDelete
  18. I gathered a lot of information through this article.Every example is easy to undestandable and explaining the logic easily.sap sd Training in Bangalore

    ReplyDelete
  19. These provided information was really so nice,thanks for giving that post and the more skills to develop after refer that post.sap bw Training in Bangalore

    ReplyDelete
  20. Your articles really impressed for me,because of all information so nice.sap ehs Training in Bangalore

    ReplyDelete
  21. Linking is very useful thing.you have really helped lots of people who visit blog and provide them use full information.sap ehs Training in Bangalore

    ReplyDelete
  22. I know that it takes a lot of effort and hard work to write such an informative content like this.sap fico Training in Bangalore

    ReplyDelete
  23. I just got to this amazing site not long ago. I was actually captured with the piece of resources you have got here. Big thumbs up for making such wonderful blog page!
    data science course

    data science interview questions

    ReplyDelete
  24. Attend The Data Analytics Courses From ExcelR. Practical Data Analytics Courses Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Data Analytics Courses.
    ExcelR Data Analytics Courses
    Data Science Interview Questions

    ReplyDelete
  25. Awesome blog. I enjoyed reading your articles. This is truly a great read for me. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work!
    data analytics course
    business analytics course
    data science courses

    ReplyDelete
  26. This comment has been removed by the author.

    ReplyDelete
  27. I am inspired with your post writing style & how continuously you describe this topic sap bi training . After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic.

    ReplyDelete
  28. Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I’ll be subscribing to your feed and I hope you post again soon.

    Digital marketing course mumbai

    ReplyDelete
  29. I am inspired with your post writing style & how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic.

    sap tutorial

    ReplyDelete
  30. Pretty article! I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing.

    sap bw training

    ReplyDelete
  31. Great post i must say and thanks for the information. Education is definitely a sticky subject. However, is still among the leading topics of our time. I appreciate your post and look forward to more.

    Digital marketing courses in Bangalore

    ReplyDelete
  32. Informative post, i love reading such posts. Read my posts here
    Fdesports
    Laravel web development services
    Intensityesports

    ReplyDelete
  33. This is excellent information. It is amazing and wonderful to visit your site.Thanks for sharng this information,this is useful to me...thamks
    Ai & Artificial Intelligence Course in Chennai
    PHP Training in Chennai
    Ethical Hacking Course in Chennai Blue Prism Training in Chennai
    UiPath Training in Chennai

    ReplyDelete
  34. Great post i must say and thanks for the information.
    Data Science Training in Hyderabad

    ReplyDelete
  35. I would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as well. In fact your creative writing abilities has inspired me to start my own Blog Engine blog now. Really the blogging is spreading its wings rapidly. Your write up is a fine example of it.
    data science training in guwahati

    ReplyDelete

  36. It was so nice content.I was really satisfied by seeing this content.
    Sap EHS training in bangalore

    ReplyDelete
  37. Really impressive post. I read it whole and going to share it with my social circules. I enjoyed your article and planning to rewrite it on my own blog.
    data scientist training and placement in hyderabad

    ReplyDelete
  38. I need to thank you for this particularly fantastic article. I definitely really liked every part of it.Educational Institute in Visakhapatnam.

    ReplyDelete
  39. Wow! Such an amazing and helpful post this is. I really really love it. It's so good and so awesome. I am just amazed. I hope that you continue to do your work like this in the future also.
    Best Gym in Visakhapatnam

    ReplyDelete
  40. I really enjoyed reading this post, big fan. Keep up the good work and please tell me when can you publish more articles or where can I read more on the subject?
    data science course fee in hyderabad

    ReplyDelete
  41. This post is so interactive and informative.keep update more information…
    German Classes in Anna Nagar
    German Classes in chennai



    ReplyDelete
  42. I wanted to leave a little comment to support you and wish you a good continuation. Wishing you the best of luck for all your blogging efforts.
    data analytics courses in hyderabad with placements

    ReplyDelete