Android setVisibility(View.Visible) not working on a layout

12,797

When you set a layout to visible from gone it's always a good idea to invalidate view to allow a redraw on the layout.

goalReminderLayout.setVisibility(View.VISIBLE);
goalReminderLayout.invalidate();

look at the android documentation for more info

http://developer.android.com/reference/android/view/View.html

Share:
12,797
Tabernaster
Author by

Tabernaster

Updated on June 14, 2022

Comments

  • Tabernaster
    Tabernaster almost 2 years

    I have a layout that I am trying to make visible and it is currently not working. The layout I want to make visible has the id "goal_reminder" below. The visibility is set to "GONE" in the xml.

    Here is the xml

    <?xml version="1.0" encoding="utf-8"?>
    <com.View.pages.ActivityPage     
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    
    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/activity_refresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <android.support.v7.widget.RecyclerView
            android:id="@+id/activitylist"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#e7e6ee"
            android:clipToPadding="false"
            android:divider="@null"
            android:paddingBottom="80dp" />
    
    </android.support.v4.widget.SwipeRefreshLayout>
    
    <RelativeLayout
        android:id="@+id/goal_reminder"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/transparent_color"
        android:orientation="vertical"
        android:visibility="gone">
    
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="145dp"
            android:src="@drawable/sunsetforgoal" />
    
        <ImageView
            android:layout_width="200dp"
            android:layout_height="50dp"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/goal_reminder_title"
            android:layout_marginEnd="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="35dp"
            android:src="@drawable/logo" />
    
        <TextView
            android:id="@+id/goal_reminder_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="10dp"
            android:layout_marginStart="10dp"
            android:layout_marginTop="10dp"
            android:text="No goal in progress"
            android:textColor="@color/text_color"
            android:textSize="26sp" />
    
        <TextView
            android:id="@+id/goal_reminder_message"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/goal_reminder_title"
            android:layout_marginLeft="20dp"
            android:layout_marginStart="20dp"
            android:layout_marginTop="20dp"
            android:text="Create a new goal in your \nprofile."
            android:textColor="@color/text_color"
            android:textSize="18sp" />
    
    </RelativeLayout>
    
    <TextView
        android:id="@+id/playground_welcome"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:autoLink="all"
        android:gravity="center_horizontal"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:text="@string/welcome_string"
        android:textSize="18sp"
        android:visibility="gone" />
    
    </com.View.pages.ActivityPage>
    

    Here is the onFinishInflate(I am using the Screenplay/Flow libraries so that is taking place of onCreate). As you can see I am trying to set goalReminderLayout.setVisibility(View.Visible) and it's not actually making it visible. I have tested this same line of code outside of the if statements and it works just fine. I have also tested to make sure it's reaching that line of code in the if statements and that part is working fine, the lastTriggerDate is being saved properly in Parse. I am lost on why it's working fine outside of the if statements in the onFinishInflate. I also tested with a Log.d("TestVisiblity", goalReminderLayout.getVisibility()); which returns a 0(Visible) so it seems like its visible but its not actually showing up on my screen.

      Date lastTriggerDate = new Date();
      Boolean noDateInParse = false;
    
      if (currentUser.getLastTriggerDate() != null) {
        lastTriggerDate = currentUser.getLastTriggerDate();
      } else {
        noDateInParse = true;
      }
    
      Boolean inToday = DateUtils.isToday(lastTriggerDate.getTime());
    
      if (!inToday) {
        currentUser.setLastTriggerDate(currentTime);
        currentUser.saveInBackground(new SaveCallback() {
          @Override
          public void done(ParseException e) {
            if (e == null) {
              goalReminderLayout.setVisibility(View.VISIBLE);
              fireGoalReminderAlert();
            } else {
              e.printStackTrace();
            }
          }
        });
      } else if (noDateInParse) {
        currentUser.setLastTriggerDate(currentTime);
        currentUser.saveInBackground(new SaveCallback() {
          @Override
          public void done(ParseException e) {
            if (e == null) {
              goalReminderLayout.setVisibility(View.VISIBLE);
    
              fireGoalReminderAlert();
            } else {
              e.printStackTrace();
            }
          }
        });
      }
    

    Here is the code for fireGoalReminderAlert(); It's just set to set the visibility back to gone after 10 seconds. I commented this line out when testing and still had no luck so I don't think this is causing the problem.

    public void fireGoalReminderAlert() {
    Runnable mRunnable;
    Handler mHandler = new Handler();
    
    mRunnable = new Runnable() {
    
      @Override
      public void run() {
        goalReminderLayout.setVisibility(View.GONE);
      }
    };
    mHandler.postDelayed(mRunnable, 10 * 1000);
    }