Articles in this section
Category / Section

How to add the SVG images in Xamarin.Forms button

2 mins read

Syncfusion Xamarin.Forms SfButton provides a support to get an elegant view in your application by setting background image and also display the button text along with the image. This article mainly explains how to use SVG image in it.

 

Available features to achieve it

 

Image and ImageSource properties in Xamarin.Forms SfButton to display the image along with the text.

 

BackgroundImage property to display the background image of Xamarin.Forms SfButton.

 

Needed package

 

Xamarin.Forms.Svg provides SvgImageSource to add Xamarin.Forms ImageSource.

How to use it in SfButton

By setting the SvgImageSource to the available image support properties in SfButton, you can achieve this as shown in the following code snippet.

 

[XAML]

  <!--Setting svg in Image property-->
        <buttons:SfButton BackgroundColor="##51E2F5" 
                          BorderWidth="2" 
                          BorderColor="Red" 
                          HorizontalOptions="Center"
                          VerticalOptions="Center"
                          WidthRequest="100" >
        <Image>
            <Image.Source>
                    <svg:SvgImageSource Source="Typogy1.svg" Width="40" Height="50" />
            </Image.Source>
        </Image>
        </buttons:SfButton>
        
        <!--Setting svg along with button text-->
        
        <buttons:SfButton  
            ShowIcon="True" 
            HorizontalOptions="Center" 
            VerticalOptions="Center"  
            WidthRequest="150" 
            HeightRequest="50"
            Margin="0,50,0,0"
            Text="Add to card">
            <buttons:SfButton.ImageSource>
                <svg:SvgImageSource Source="commerce-and-shopping.svg" Width="40" Height="50" Color="White"/>
            </buttons:SfButton.ImageSource>
        </buttons:SfButton>
        
        <!--Setting svg image as backgroud image of button-->
        <buttons:SfButton 
            TextColor="White"
            WidthRequest="100"
            HorizontalOptions="Center" 
            VerticalOptions="Center" 
            Margin="0,50,0,0"
            Text="Login">
            <buttons:SfButton.BackgroundImage>
                <svg:SvgImageSource Source="background.svg" Width="40" Height="50" />
            </buttons:SfButton.BackgroundImage>
        </buttons:SfButton>

Output

svg image support

Additional things to do that to make it render.

 

In Android: Add Xamarin.Forms.Svg.Droid.SvgImage.Init(this).

 

[C#]

 

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;
 
            base.OnCreate(savedInstanceState);
            global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
            Xamarin.Forms.Svg.Droid.SvgImage.Init(this);
            LoadApplication(new App());
        }
    }

In iOS: Add Xamarin.Forms.Svg.iOS.SvgImage.Init().

 

[C#]

 

public override bool FinishedLaunching(UIApplication app, NSDictionary options)
        {
            global::Xamarin.Forms.Forms.Init();
 
            Xamarin.Forms.Svg.iOS.SvgImage.Init();
            Syncfusion.XForms.iOS.Border.SfBorderRenderer.Init();
           Syncfusion.XForms.iOS.Buttons.SfButtonRenderer.Init();
 
            LoadApplication(new App());
 
            return base.FinishedLaunching(app, options);
        }

 

In PCL: Add SvgImageSource.RegisterAssembly().

 

[C#]

  public App()
        {
            InitializeComponent();
 
            SvgImageSource.RegisterAssembly();
 
            MainPage = new MainPage();
        }

 

 

View the sample in GitHub.

 

 

See also

 

How to disable Xamarin.Forms button

How to change Xamarin.Forms button style using its visual states

How to create a round or circle button in Xamarin.Forms

How to create Xamarin.Forms image button

 

 

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied