Simple Radio button code sample

HI Guys,

Can anyone suggest a simple radio button with 2 item radio button option code i a group,

Im trying to create a radio group and add 2 items to it from code behind xaml.cs, its a dynamic radio button

Thanks in advance

Tagged:

Answers

  • Ravi_chandranRavi_chandran INMember ✭✭

    im expecting radiobutton without using any extra controls, just by using Android.widgets or Xamarin elements

  • JohnHardmanJohnHardman GBUniversity mod

    @Ravi_chandran - XLabs has a BindableRadioGroup. See https://github.com/XLabs/Xamarin-Forms-Labs/tree/master/src/Forms/XLabs.Forms/Controls/RadioButton

    Disclaimer: I haven't used it :-)

  • MuditSharmaMuditSharma Member ✭✭

    I am using images better that control.

    My Xaml Code==================

                        <Frame HasShadow="True" BackgroundColor="#f8f8ff" x:Name="Frame1" HeightRequest="30" OutlineColor="Black">
              <Grid>
                <Grid.RowDefinitions>
                  <RowDefinition Height="Auto"></RowDefinition>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                  <ColumnDefinition Width="10*"></ColumnDefinition>
                  <ColumnDefinition Width="90*"></ColumnDefinition>
                </Grid.ColumnDefinitions>
                   <Image Source="{Binding Image1}" HeightRequest="25" x:Name="Image_Ans1" Grid.Row="0" Grid.Column="0">
                       <Image.GestureRecognizers>
                     <TapGestureRecognizer Command="{Binding OnButton1}" NumberOfTapsRequired="1"></TapGestureRecognizer>
                      </Image.GestureRecognizers>
                  </Image>
                                <Label x:Name="Lbl_Ans1 " Grid.Row="0" Text="{Binding IsAnswer1}"  Grid.Column="1" TextColor="Black"></Label>
              </Grid>
            </Frame>
                        <Frame HasShadow="True" BackgroundColor="#f8f8ff" x:Name="Frame2" HeightRequest="30" OutlineColor="Black" >
              <Grid>
                <Grid.RowDefinitions>
                  <RowDefinition Height="Auto"></RowDefinition>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                  <ColumnDefinition Width="10*"></ColumnDefinition>
                  <ColumnDefinition Width="90*"></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <Image Source="{Binding Image2}" HeightRequest="25" x:Name="Image_Ans2" Grid.Row="0" Grid.Column="0">
                          <Image.GestureRecognizers>
                                        <TapGestureRecognizer Command="{Binding OnButton2}" NumberOfTapsRequired="1"></TapGestureRecognizer>
                          </Image.GestureRecognizers>
                </Image>
                                <Label x:Name="Lbl_Ans2" Grid.Row="0" Text="{Binding IsAnswer2}"  Grid.Column="1" TextColor="Black"></Label>
              </Grid>
            </Frame>
          </StackLayout>
    

    Code Behind===============
    public TrueFalseQuestion(QuestionAnsPropertyTable GetQuestions)
    {
    InitializeComponent();
    currentinstance = this;
    _trueOrFalseQuesViewModel = new TrueOrFalseQuesViewModel(this.Navigation, GetQuestions);
    this.BindingContext = _trueOrFalseQuesViewModel;

        }
    

    ViewModel===================

    public class TrueOrFalseQuesViewModel : BaseViewModel
    {
    private bool radio, radio1;
    private string _QuestionTopic;
    private string _Answer1, _Answer2;
    private FileImageSource _ImageSource1, _ImageSource2;
    public string CheckedImage = "radiobuttonchecked.png";
    public string UnCheckedImage = "radiobuttonunchecked.png";
    public TrueOrFalseQuesViewModel(INavigation navigation, QuestionAnsPropertyTable GetQuestionAndAnswerValues)
    {
    QuestionTopic = GetQuestionAndAnswerValues.Question;
    radio = false;
    radio1 = false;
    Image1 = UnCheckedImage;
    Image2 = UnCheckedImage;
    OnButton1 = new Command(OnTapEvent1);
    OnButton2 = new Command(OnTapEvent2);
    validation = new Command(onvalidate);
    IsAnswer1 = GetQuestionAndAnswerValues.Option1;
    IsAnswer2 = GetQuestionAndAnswerValues.Option2;
    values = GetQuestionAndAnswerValues;
    }
    public void onvalidate()
    {
    UserAnswerTable GetDetails = new UserAnswerTable();
    GetDetails.AnswerType = values.Atype;
    GetDetails.QuestionNumber = values.QuestionNumber;
    GetDetails.TestId = values.Testid;
    if (radio)
    {
    GetDetails.Answer1 = IsAnswer1;
    }
    if (radio1)
    {
    GetDetails.Answer2 = IsAnswer2;
    }
    var item = AppData.LocalDatabase.Database.Table().Where(x => x.QuestionNumber == values.QuestionNumber).ToList();
    if (item.Count != 0)
    {
    AppData.LocalDatabase.Database.Update(GetDetails);
    }
    else
    {
    AppData.LocalDatabase.Database.Insert(GetDetails);
    }

        }
        public void OnTapEvent1()
        {
            radio = true;
            Image1 = CheckedImage;
            if (radio1.Equals(true))
            {
                Image2 = UnCheckedImage;
                radio1 = false;
            }
        }
        public void OnTapEvent2()
        {
            radio1 = true;
            Image2 = CheckedImage;
            if (radio.Equals(true))
            {
                Image1 = UnCheckedImage;
                radio = false;
            }
        }
    
        public string QuestionTopic
        {
            get
            {
                return _QuestionTopic;
            }
            set
            {
                _QuestionTopic = value;
                OnPropertyChanged(nameof(QuestionTopic));
            }
        }
    
        public FileImageSource Image1
        {
            get
            {
                return _ImageSource1;
            }
            set
            {
                _ImageSource1 = value;
                OnPropertyChanged(nameof(Image1));
            }
        }
    
        public FileImageSource Image2
        {
            get
            {
                return _ImageSource2;
            }
            set
            {
                _ImageSource2 = value;
                OnPropertyChanged(nameof(Image2));
            }
        }
    
        public Command OnButton1 { get; set; }
        public Command OnButton2 { get; set; }
    
        public string IsAnswer1
        {
            get
            {
                return _Answer1;
            }
            set
            {
                _Answer1 = value;
                OnPropertyChanged(nameof(IsAnswer1));
            }
        }
    
        public string IsAnswer2
        {
            get
            {
                return _Answer2;
            }
            set
            {
                _Answer2 = value;
                OnPropertyChanged(nameof(IsAnswer2));
            }
        }
    
        public Command validation { get; set; }
        public QuestionAnsPropertyTable values { get; }
    }
    

    =================================================

    Notice:In viewModel Extra things are also there.please leave that.

Sign In or Register to comment.