Vasilis Greece
Vasilis Greece

Reputation: 907

Xamarin android fail to load images

I am a new developer of Xamarin. I am trying to build a simple button image and the problem is that the image, it didn't appear in my phone. I use also for debug xamarin live from VS2017 and my phone. When I built and run live there are no warnings or errors. So is difficult to look what is wrong.

What I did so far:

I checked the images in Android folder resources and checked if icons have the right dimensions.

I do the steps: clean solution, rebuilt solution, built solution and restart VS2017

App.xaml.cs:

public App ()
        {
            InitializeComponent();

            MainPage = new MainPage();
        }

MainPage.xaml:

<StackLayout VerticalOptions="Center" Spacing="50">

        <Button Text="Hello" BackgroundColor="Wheat" BorderRadius="20" TextColor="Black">
            <Button.Image>
                <OnPlatform x:TypeArguments="FileImageSource" Android="icon.png"/>
            </Button.Image>
        </Button>

        <StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand">
            <StackLayout Orientation="Vertical" HorizontalOptions="FillAndExpand">
                <Button Text="Left" BackgroundColor="IndianRed" BorderRadius="22" TextColor="Black">
                    <Button.Image>
                        <OnPlatform x:TypeArguments="FileImageSource" Android="icon.png"/>
                    </Button.Image>
                </Button>
            </StackLayout>
            <StackLayout Orientation="Vertical" HorizontalOptions="FillAndExpand">
                <Button Text="Right" BackgroundColor="IndianRed" BorderRadius="22" TextColor="Black">
                    <Button.Image>
                        <OnPlatform x:TypeArguments="FileImageSource" Android="icon.png"/>
                    </Button.Image>
                </Button>

            </StackLayout>

        </StackLayout>
    </StackLayout>

I load the default icon.png and I checked hdpi(72x72), mdpi(48x48),xhdpi(96x96),xxhdpi(144x144),xxxhdpi(192x192) have the right dimensions.

I saw the buttons in right position with a text inside but with no image. What could be wrong?

Upvotes: 1

Views: 95

Answers (1)

Hichame Yessou
Hichame Yessou

Reputation: 2708

Try this in your xaml:

<Button Image="icon.png" Text="Hello" BackgroundColor="Wheat" BorderRadius="20" TextColor="Black"/>

Or in your code:

yourButton.Image = "icon.png";

or

yourButton.Image = ImageSource.FromFile("icon.png");

Upvotes: 1

Related Questions