Hi all,
I was making an cool app, and then I had a necessity to have a RatingBar in the app, but the only example around here that I have found, doen't center in the middle on the screen. (No, it's impossible. I have questioned about it!)
Disappointed with the result of no Ratingbar around here, I have myself made a RatingBar. Because I have a helpfull spirit in me, I have worked hard and then finally made a class of it.
It is one of the simpliest classes to use. It can't be simplier! Just initialize, set position and *BANG*
It has a lot of options:
- Get and set size
- Force size
- Allow or deny sliding through stars
- Change all bitmaps and colors of the RatingBar
- Enable and disable the RatingBar
- Half Stars
(First 3 images in normal mode , the 4th in "Half Star" mode and the last in "Forced Size" mode)
To initialize the RatingBar it is simple:
B4X:
'Initialize a panel to draw the RatingBar. The Position and the Size are useless here. Just initialize and set all to '0'.
Dim Panel1 As Panel
Panel1.Initialize("")
Activity.AddView(Panel1,0,0,0,0)
'Initialize the RatingBar with '30'% of size, using 'Panel1' as target and set the event to 'RatingBar1'.
''100'% equals to 715x130!
RatingBar1.Initialize(30,Panel1,"RatingBar1")
Now the RatingBar is yet initialized and visible, but we want to center it now. We make this with:
B4X:
Panel1.SetLayout(50%x - (Panel1.Width /2),50%y - (Panel1.Height /2),RatingBar1.Width,RatingBar1.Height)
Now the RatingBar is ready. Simple huh?!
Ehmmm... You thinked right... There's no option to set the amount of the stars yet.
If you want to adjust the RatingBar to your style and colors, you can just use this code as follows:
B4X:
RatingBar1.Bitmaps(LoadBitmap(File.DirAssets,"NormalStars.png"),LoadBitmap(File.DirAssets,"PressedStars.png"),Colors.RGB(51,181,229))
If you want to disable the RatingBar for any reason, you can do it using:
B4X:
RatingBar1.Enabled=False
If you don't want that the user slides through the stars for any reason, you can disable this with:
B4X:
RatingBar1.SlidingAllowed=False
If you need to predefine the stars, you can do it using:
B4X:
''3' defines the amount of stars being showed. (Range goes from 0 to 5, even in half star mode. You can use 2.4 and something else...)
''False', if you don't want to raise an event when those stars going to be auto-selected.
RatingBar1.SetStars(3,False)
If you need to get the highlighted stars without raising various events, when the user is sliding through, use this in a button or something:
B4X:
MyLabel = RatingBar1.SelectedStars
Mentioned above, you can force the layout of the RatingBar, but it is totally not advised, as seen in "Image 4" above.
Used incorrectly, when used, it distorts the RatingBar completely!
Think twice or trice before you use it, please.
B4X:
'(X, Y) (Of course)
RatingBar1.ForceSize(500,500)
If you want to enable "half stars", you can use this code and enable it:
B4X:
'With full stars the range goes from 0 to 5. With half stars from 0 to 10.
RatingBar1.HalfStars = True
If you want to set the visibility of your RatingBar, just use:
B4X:
RatingBar1.Visible = False
RatingBar1.Visible = True
'It is the same.
RatingBar1.Hide 'Hides the bar
RatingBar1.Show 'Shows the bar
The project is attached (With the class inside, of course).
If I have saved you many headaches (Now I have), please hit thanks on my post
Please note that this is my first class. (This doesn't mean that it is bad programmed) I have suffered very much to make this a class.
And if you have any suggestion, write below. (No, I will probably not add more that 5 stars.)
EDIT: "Version 2" released. Now you can set the stars.
EDITIT: "Version 3" released. Now you can get the last selected star, without 'making a mess'.
EDITITIT: "Version 4" released. Now you can set if you want to have "Half Stars" or not.
EDITITITIT: "Version 5" released. Now you can set the Visibility of the RatingBar.
EDITITITITIT: "Version 6" released. Now probably bugfree
PS: I have made the class all for nothing, it doesn't work together with BetterDialogs (._.)
BetterDialogs only handle fixed elements (Not moving)
If anyone find a solution, it would be great!
Attachments
Last edited: