'How to achieve Indeterminate state for checkbox in Xamarin form In Android

I want to achieve CheckBox where it works for three state. In UWP I am able to achieve with custom Renderer with "IsThreeState" and by setting IsChecked property null. but not able to achieve in android customization. please helpenter image description here



Solution 1:[1]

Here I am answering my own question..

I got this hint from android native. Here we can set three type of image resources when native control clicked and can invoke Event handler for xamarin common control.

      bool IsChecked;
      if (Control == null)
      {
         CheckBox cb = new CheckBox(Context); //CheckBox is Android Native control
         SetNativeControl(chkBox);
      }
        
      Control.Click += Clicked;


    private void Clicked(object sender, EventArgs e)
    {
        if(IsChecked == false)
        {
            Control.SetButtonDrawable(Resource.Drawable.ic_checked);
        }
        else if(IsChecked == true)
        {
         Control.SetButtonDrawable(Resource.Drawable.ic_Inderterminate);
        }
        else if (IsChecked == null)
        {
            Control.SetButtonDrawable(Resource.Drawable.ic_unChecked);
        }
        FormCheckBox?.InvokEvent(Element, e);
    }

Solution 2:[2]

In Android and iOS you could install the plugin
Xamarin.Forms.InputKit from nuget .

Usage

<StackLayout >
  <input:CheckBox Text="Option 1" Type="Box" />
  <input:CheckBox Text="Hello World I'm Option 2" Type="Check"/>
  <input:CheckBox Text="Consetetur eum kasd eos dolore Option 3" Type="Cross"/>
  [![enter image description here][1]][1]<input:CheckBox Text="Sea sed justo" Type="Star"/>
</StackLayout>

In your case , you need to set the Type as Box to display the Inderminate state .

For more details about the plugin you could refer https://github.com/enisn/Xamarin.Forms.InputKit .

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Abhishek Maurya
Solution 2 Lucas Zhang