1

I am trying to set the visibility of a Textview depending on the response returned by Retrofit . onFailure, I am setting the visibility to visible tv_no_cat.setVisibility(View.VISIBLE); However, this does not work. I am doing the same with a ContentLoadingProgressBar and it's working appropriately. Here is my code

public void onFailure(Call<List<MoviesCategory>> call, Throwable t) {
            tv_no_cat.setVisibility(View.VISIBLE); ////This does not work
            Boolean x = tv_no_cat.getVisibility() == View.VISIBLE;

            Toast.makeText(getContext(), "Network Error "+x, Toast.LENGTH_LONG).show();    //This shows true
            tv_no_cat.setTextColor(Color.BLUE);
            videoLoadingPb.setVisibility(View.GONE); //This works
        }

My guess is that am trying to set the textview which is on UI thread from another thread. If so, why is it working for the ContentLoadingProgressBar?

Here is my xml for the 2 views

<android.support.v4.widget.ContentLoadingProgressBar
        android:id="@+id/loading_categories"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_gravity="center"
        android:visibility="visible" />
    <TextView
        android:layout_below="@id/loading_categories"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="No Movie Category Found"
        android:id="@+id/tv_no_cat"
        android:textAlignment="center"
        android:layout_gravity="bottom"
        android:visibility="gone"
        />

Any help on how I can set the textview Visibility to VISIBLE is welcome.

E_K
  • 2,159
  • 23
  • 39
  • android:id="@+id/loading_categories" try to print its visibility like .getVisibility() – Adil Jan 29 '18 at 12:41

2 Answers2

1

From the docs:

On Android, callbacks will be executed on the main thread...

So this is not your issue, and I'm doubtful that it's related to Retrofit, especially when it works for another view. Please make sure that there are no problem in your xml design, and that there are no other methods that affect this view's visibility.

Neria Nachum
  • 1,519
  • 1
  • 20
  • 37
  • I have added code for the affected views. Please help me have a look at them – E_K Jan 29 '18 at 11:33
  • Your xml contains both `RelativeLayout` and `LinearLayout` attributes so it's unclear what you actually use. If it's `RelativeLayout`, replace `layout_gravity` with the 'layout_alignParentBottom'. – Neria Nachum Jan 29 '18 at 11:40
  • Okey. ill do that. However, if I do `tv_no_cat.setVisibility(View.VISIBLE);` inside the `onCreateView`, it shows the textview – E_K Jan 29 '18 at 11:45
  • So my previous suggestion is unnecessary. Anyway, it's definitely not related to Retrofit - you provided 2 evidences (the fact that another view "works" as expected and the Toast check). It's not something that I can help you within a SO question scope, but my general advice is to look for flaws in the UI (whether in xml or in your Activity code). Maybe another view is covering your TextView or pushing it off screen. – Neria Nachum Jan 29 '18 at 11:49
  • Thanks man. I have tried doing this on an Activity and it is working. Would the reason be that I am doing this on a fragment?? – E_K Jan 29 '18 at 11:54
  • Not directly. Perhaps your fragment does not have enough room to show its content. – Neria Nachum Jan 29 '18 at 11:56
0

are registering Textview inside Oncreate or some where else ? And this code you are using inside activity of fragment ?

Please check this ...

tv_no_cat=(TextView)findViewById(R.id.tv_no_cat);(Inside Activity)

tv_no_cat=(TextView)rootView.findViewById(R.id.tv_no_cat);(Inside Fragment )

  • Thanks a lot but In this case, casting is just adding redundancy and I don't think it will help. – E_K Jan 29 '18 at 12:08