rana hd
rana hd

Reputation: 377

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.

Upvotes: 0

Views: 351

Answers (2)

rana hd
rana hd

Reputation: 377

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

Upvotes: 0

Leon Lu
Leon Lu

Reputation: 9234

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

Upvotes: 1

Related Questions