Search This Blog

Monday 29 April 2019

Step to create rating-bar in Xamarin Form using Custom Control

RatingBar Control- is a cross-platform plugin used for selecting the rating.

 



How To Use

Available on NuGet: https://www.nuget.org/packages/RatingBarControl/1.0.0
Install this Plugin in your Xamarin Form Project.

Namespace:  xmlns:ratingbarcontrol="clr-namespace:RatingBarControl;assembly=RatingBarControl"

Implementation Example

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
             xmlns:ratingbarcontrol="clr-namespace:RatingBarControl;assembly=RatingBarControl"
             x:Class="TestApp.MainPage">
    <StackLayout Margin="0,10,0,0" HorizontalOptions="Center" >
        <Label Text="{Binding Source={x:Reference rtControl},Path=SelectedStarValue,StringFormat='Seleced Star: {0}'}" />
        <ratingbarcontrol:RatingBar StarHeightRequest="35" 
                                    StarWidthRequest="35"  
                                    EmptyStarImage="emptystar" 
                                    FillStarImage="fillstar" 
                                    x:Name="rtControl"/>
    </StackLayout>
</ContentPage>
 


RatingBar Control Property
  1. StarHeightRequest: Used to set the Height of the Image.
  2. StarWidthRequest: Used to set the Width of the Image.
  3. EmptyStarImage: Specify the image name of the empty star.
  4. FillStarImage: Specify the image name of the filled star.
  5. SelectedStarCommand: Use this command to get the selected star value in ViewModel.
  6. SelectedStarValue: Used to set default selected star value.
  7. FlowDirection: Used to set the Rating Bar Control Flow Direction, either Left to Right or Right to left.
  1. Spacing: Used to set the Spacing between stars.


How to display the selected star rating
Write the Following code to display the selected star rating

<Label Text="{Binding Source={x:Reference rtControl},Path=SelectedStarValue,StringFormat='Seleced Star: {0}'}" /
<ratingbarcontrol:RatingBar StarHeightRequest="35" 
          StarWidthRequest="35"  
          EmptyStarImage="emptystar" 
          FillStarImage="fillstar" 
          x:Name="rtControl"/>

How to get the selected rating
SelecedStarCommand: Use this command to get the selected star value in ViewModel.

Design Side:

<Label Text="{Binding SelectedStarValue,StringFormat='Selected Star is : {0}'}" />
<ratingbarcontrol:RatingBar StarHeightRequest="35" 
                  StarWidthRequest="35"  
                  EmptyStarImage="emptystar" 
                  FillStarImage="fillstar" 
                  SelectedStarCommand="{Binding SelectedStarCommand}"
                  x:Name="rtControl"/>

Code Behind (ViewModel):

private int _selectedStarValue;
public int SelectedStarValue
{
     get => _selectedStarValue;
     set => SetProperty(ref _selectedStarValue, value);
}
 
public ICommand SelectedStarCommand => new Command<int>((selectedStarValue) =>
{
      SelectedStarValue = selectedStarValue;
});




26 comments:

  1. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
  2. Amazing Blog.. Thanks for sharing great information about Xamarin.
    Hire Xamarin Developer

    ReplyDelete
  3. Hi,

    Thanks, that I've found this post.

    I'm trying to implement it in Android.
    i set the lines:

    private static string emptyStarImage = "emptyStarImage.png"; // string.Empty;
    private static string fillStarImage = "fillStarImage.png"; // string.Empty;

    But I'm not seeing an empty / filled star image.
    Anything I'm missing?

    ReplyDelete
    Replies
    1. Hi, Can you check the image path.
      I think your empty star image path or filename might be wrong.

      You can temporary check image by setting image on design.

      Delete
    2. This comment has been removed by the author.

      Delete
    3. RattingBarPage.Xaml

      "fillstar.png" and "emptystar.png" files paste in drawable folder.

      Delete
  4. Hi,

    Great code, thank you.

    I want to add mutliple rattingbar in the same page. Just the lastone is rendered.
    Do you know why ?

    ReplyDelete
    Replies
    1. Thank you, If you want to add multiple ratting bar, then same ratting bar control either you can use multiple time on Page or you can use ListView.

      Delete
    2. Hi Cedric, I have Fixed the issue of multiple Rattingbar. Now it will display multiple RattingBar and same code updated on my blog and github repo too.

      https://github.com/mistrypragnesh40/RattingBar-master.git

      https://github.com/mistrypragnesh40/RattingBar-master.git

      Delete
  5. hi i'm getting error in the `ImageButtonView` in the below code:
    private static readonly BindableProperty CommandParameterProperty = BindableProperty.Create(
    propertyName: "CommandParameter",
    returnType: typeof(object),
    declaringType: typeof(ImageButtonView),
    propertyChanged: CommandParameterPropertyChanged
    );

    ReplyDelete
    Replies
    1. https://xamarincodingtutorial.blogspot.com/2019/05/step-to-create-imagebutton-with-text.html

      Delete
    2. There was a minor mistake in this code. I have updated this blog content. Here declaring type will be RattingBar instead of ImageButtonView sorry for inconvenience.

      Delete
  6. ImageButtonView give error what is this please give me project

    ReplyDelete
    Replies
    1. Use Declaring Type as RattingBar Instead of ImageButtonView

      Delete
  7. please add image button view code to complete

    ReplyDelete
    Replies
    1. Hi i have added Project here please go through it.
      https://github.com/mistrypragnesh40/RattingBar

      Thank you.

      Delete
  8. when i am entering multiple rating bar its showing only last why?

    ReplyDelete
    Replies
    1. Hello Chanda, I have Fixed the issue of multiple Ratting bar now showing and same code updated on my blog and github repo too.

      https://github.com/mistrypragnesh40/RattingBar-master.git

      Delete
  9. Hi, very nice post. I want to ask what is rattingBarCommand doing inside RattingBar control in XAML?

    ReplyDelete
  10. Yeah Thank you, and rattingBarCommand command will executed on the Selection of Each Star. if you want to perform any action on the selection of each star then you can use this command. I forgot to initialize ratting bar command on RattingBarViewModal but you can initialize it.

    ReplyDelete
  11. This is so awesome, thank you

    ReplyDelete

Popular Posts