0

I have created an imageview and above it I created a textview. my aim is that I want to show my imageview if there's an image and if not I want to remove the imageview and display the textview that says "no image", this is my xml code:

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp">
        <TextView
             android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Item Image: "
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textView9"
             android:textColor="@android:color/holo_blue_dark"/>
    <ImageView
            android:src="@android:drawable/ic_menu_gallery"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:minWidth="25px"
            android:minHeight="25px"
            android:id="@+id/imageView1"
             android:layout_toRightOf="@id/textView9"/>  
            
            <TextView
             android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="No Image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textView10"
             
         android:layout_toRightOf="@id/textView9"
         />

        
        </RelativeLayout>

and this is my code:

ImageView imgv = view.FindViewById<ImageView>(Resource.Id.imageView1);
textview10 = view.FindViewById<TextView>(Resource.Id.textView10);
            textview10.Visibility = ViewStates.Invisible;

 private void Ws_getimg_clrCompleted(object sender, WSitems.getimg_clrCompletedEventArgs e)
        {
            Byte[] data = e.Result.datab;
            if (data != null)
            {
               
                MemoryStream mem = new MemoryStream(data);
                Android.Graphics.Bitmap originBitmap = BitmapFactory.DecodeStream(mem);
                imgv.SetImageBitmap(originBitmap);
            } 

            else
            {
                
                imgv.Visibility = ViewStates.Gone;
                textview10.Visibility = ViewStates.Visible;
                
            }
}

I get my image from a webservice. the problem is that only the imageview is disappearing when there's no image but the textview isn't becoming visible. why? what should I do? thanks in advance.

rana hd
  • 355
  • 3
  • 18

2 Answers2

1

You can use FFImageLoading to replace your webservice to load Image on the intnet.

  ImageView imageView1 = FindViewById<ImageView>(Resource.Id.imageView1);
            TextView textView10 = FindViewById<TextView>(Resource.Id.textView10);
            textView10.Visibility = ViewStates.Invisible;
           // string urlToImage = "http://www.123raw.com/includes/templates/custom/images/123raw_mainpic_01.jpg";
            string urlToImage = "http://www.123raw.com/includes/templates/custom/images/12311raw_mainpic_01.jpg";

            var config = new FFImageLoading.Config.Configuration()
            {
                ExecuteCallbacksOnUIThread = true
            };
            ImageService.Instance.Initialize(config);
            ImageService.Instance.LoadUrl(urlToImage).Error(exception =>
            {
                imageView1.Visibility = ViewStates.Gone;
                textView10.Visibility = ViewStates.Visible;
            }).Into(imageView1);

Here is running GIF, if the image cannot be loaded.

enter image description here

Leon
  • 8,404
  • 2
  • 9
  • 52
  • If above answer is helpful, please do not forget to accept it as answer(click the “✔” in the upper left corner of this answer), it will help others who have similar issue – Leon Aug 18 '20 at 02:44
  • thanks sir, but my image is found in a database in sql server and it is binary, that's why I use MemoryStream mem = new MemoryStream(data); and the line after it to recreate my image, this is why i use a webservice, can i still use the code you suggested? – rana hd Aug 18 '20 at 12:06
  • @ranahdr Can you provide a test demo(could reproduce this issue) to us? – Leon Aug 19 '20 at 00:51
  • When you set `imgv.Visibility = ViewStates.Gone; textview10.Visibility = ViewStates.Visible;` please try to wrap the `MainThread.BeginInvokeOnMainThread` like this thread: https://learn.microsoft.com/en-us/xamarin/essentials/main-thread – Leon Aug 19 '20 at 01:00
  • thanks a lot sir for your help. from your answer I was capable of doing some research and I figured out that ImageService.Instance also has LoadStream element. so with the help of your answer and the following answer https://stackoverflow.com/questions/49959103/ffimageloading-load-bitmap-into-imageviewasync I solved the issue. i'm gonna write my answer. – rana hd Aug 19 '20 at 15:00
  • Thanks for your sharing, please accpet helpful answer, it will help others who have similar issue. – Leon Aug 20 '20 at 00:39
0

this is my code:

 Task<Stream> GetStreamFromImageByte(CancellationToken ct)
        {
                       

            //Since we need to return a Task<Stream> we will use a TaskCompletionSource>
            TaskCompletionSource<Stream> tcs = new TaskCompletionSource<Stream>();
            if (data != null)
            {
                tcs.TrySetResult(new MemoryStream(data));
            }
            return tcs.Task;
        }
        private void Ws_getimg_clrCompleted(object sender, WSitems.getimg_clrCompletedEventArgs e)
        {
            data = e.Result.datab;
            
            var config = new FFImageLoading.Config.Configuration()
            {
                ExecuteCallbacksOnUIThread = true
            };
            ImageService.Instance.Initialize(config);
            ImageService.Instance.LoadStream(GetStreamFromImageByte).Error(exception =>
            {
                imgv.Visibility = ViewStates.Gone;
                textview10.Visibility = ViewStates.Visible;
            }).Into(imgv);
rana hd
  • 355
  • 3
  • 18