Search code examples
xamarin.androidtextviewimageviewvisibility

textview visibility xamarin android


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.


Solution

  • 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);