Xamarin (C#) doesn't show images on a linearLayout

73 Views Asked by At

I'm using Xamarin to get a number from a user and show the number of requested cards, visual studio does not throw any errors before/during runtime, but the images are not displayed.

The images are all named like: "img{num (1-13)}{'S', 'H', 'D', 'C'}

Here is the code that suppose to display the images:

private void ShowBtn_Click(object sender, EventArgs e)
{
        main.RemoveAllViews();
        int imgToShow = int.Parse(editTxt.Text);
        char[] types = { 'S', 'C', 'H', 'D' };
        LinearLayout layout = new LinearLayout(this);

        for (int i = 0; i < imgToShow; i++)
        {
            if (i % 5 == 0)
            {
                main.AddView(layout);
                layout = new LinearLayout(this);
                continue;
            }

            // add image
            var imgv = new ImageView(this);
            imgv.SetImageResource(Resources.GetIdentifier($"img{rnd.Next(1, 14)}{types[rnd.Next(4)]}", "drawable", this.PackageName));
            imgv.LayoutParameters = new LinearLayout.LayoutParams(0, 200, 1);
            layout.AddView(imgv);
        }

        main.AddView(layout);
}

Here is the initiation of the elements on the xml:

void Init()
{
        main = FindViewById<LinearLayout>(Resource.Id.mainLayout);
        closeBtn = FindViewById<Button>(Resource.Id.close);
        showBtn = FindViewById<Button>(Resource.Id.show);
        clsBtn = FindViewById<Button>(Resource.Id.cls);
        editTxt = FindViewById<EditText>(Resource.Id.editTxt);
}

And the xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="220dp"
            android:layout_height="50dp"
            android:text="Cards to reveal"
            android:textSize="30sp"/>
        <EditText
            android:layout_width="150dp"
            android:layout_height="match_parent"
            android:inputType="number"
            android:id="@+id/editTxt"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:orientation="horizontal">
        <Button
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="5dp"
            android:text="Show Images"
            android:textSize="13sp"
            android:id="@+id/show"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="5dp"
            android:text="Clear"
            android:textSize="13sp"
            android:id="@+id/cls"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="5dp"
            android:text="Close"
            android:textSize="13sp"
            android:id="@+id/close"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:id="@+id/mainLayout"/>

</LinearLayout>
1

There are 1 best solutions below

0
Jianwei Sun - MSFT On BEST ANSWER

I tested the code you provided and found that it is necessary to keep the image file name in lowercase.

Here is my image file:

enter image description here

You can find that the end of the file name is uppercase letters.

The following is my code:

private void ShowBtn_Click(object sender, EventArgs e)
{
    //keep types in lowercase
    char[] types = { 's', 'c'};

    Random rnd=new Random();
    var a = $"img{rnd.Next(1, 2)}{types[rnd.Next(1)]}";
    main.RemoveAllViews();
    //int imgToShow = int.Parse(editTxt.Text);
    LinearLayout layout = new LinearLayout(this);
    var imgv = new ImageView(this);

   imgv.SetImageResource(Resources.GetIdentifier("img2c", "drawable", this.PackageName));

   imgv.LayoutParameters = new LinearLayout.LayoutParams(0, 200, 1);
    layout.AddView(imgv);
    
    main.AddView(layout);
}

Here is the effect.

So you should change char[] types to this:

     char[] types = { 's', 'c', 'h', 'd' };