Thursday 28 October 2021

How to recover view from View.gone. setVisibility(View.VISIBLE) not working after using 'android:visibility="gone"' in xml

My issue: In my xml file, I define android:visibility="gone" in the linear layout labelled as assess_layout_list. Then, in the onClick() of course_adapter_layout, the whole view, I set the visibility back to View.VISIBLE, which does not work, even though the Log call just before it works, the LinearLayout object called assess_list_layout is not null, and it does work when I define the visibility="invisible" in the xml file. I want it to be gone at first though, and visible after clicking as this fits the design of the app.

Here is my course_adapter_view.xml file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/course_adapter_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:gravity="left"
    >

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="20dp"
        android:layout_marginTop="20dp"
        android:padding="15dp"
        android:elevation="2dp"
        android:background="@drawable/course_header_background">

        <ImageView
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:src="@drawable/course_color_circle"/>

        <Space
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.25"/>

        <TextView
            android:id="@+id/course_adapter_course_code"
            android:text="TextView1"
            android:layout_width="wrap_content"
            android:layout_height="20dp"
            android:layout_weight="0.5"/>

        <Space
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.25"/>


        <TextView
            android:id="@+id/course_adapter_course_title"
            android:text="TextView2"
            android:layout_width="wrap_content"
            android:layout_height="20dp"/>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/assess_list_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginRight="40dp"
        android:layout_marginLeft="40dp"
        android:background="@drawable/course_body_background"
        android:padding="20dp"
        android:visibility="gone"
        >


        <ListView
            android:id="@+id/course_adapter_assess_list"
            android:layout_width="match_parent"
            android:layout_height="250dp"
            android:layout_marginBottom="10dp"/>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="left"
                android:text="More" />

            <Space
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="left"
                android:text="New"/>

        </LinearLayout>


    </LinearLayout>


</LinearLayout>

Here is my CourseListAdapter.java file that I use to create each view for each course in the list of courses, minus the usual stuff:

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    // get info
    String course_code = getItem(position).getCourseCode();
    Double course_grade = getItem(position).getCurrentGrade();

    // make inflater and inflate the layout
    LayoutInflater inflater = LayoutInflater.from(context);
    View v = inflater.inflate(mResource, parent, false);


    TextView tv_course_code = v.findViewById(R.id.course_adapter_course_code);
    TextView tv_course_title = v.findViewById(R.id.course_adapter_course_title);

    tv_course_code.setText(course_code);
    tv_course_title.setText(String.valueOf(course_grade));

    // add on click to each list view element
    LinearLayout layout = v.findViewById(R.id.course_adapter_layout);
    layout.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        Log.i(TAG, "List view element has been clicked " + course_code);

       
        // expand the view to include a new fragment
        LinearLayout assess_list_layout = view.findViewById(R.id.assess_list_layout);
        assess_list_layout.setVisibility(View.VISIBLE);

        // get the list view and add each course to the course view
        ListView assessment_list_view = (ListView) view.findViewById(R.id.course_adapter_assess_list);
        AssessmentListAdapter assessAdapter = new AssessmentListAdapter(getContext(), R.layout.assessment_adapter_view, getItem(position).getAssessmentList(), getItem(position));
        assessment_list_view.setAdapter(assessAdapter);
      }
    });

    return v;
  }
}

Please let me know if there is any more information you need. Will also take suggestions on other ways of accomplishing the same thing. Thanks for your help. ~Seth.

Edit: when the assess_list_layout.setVisibility(View.VISIBLE) is outside of the onClick it does work.

Further Edit: Things I have tried so far to no avail:

  • moving the location of where I define the LinearLayout componenent
  • calling invalidate() on parent view
  • using runOnUiThread()


from How to recover view from View.gone. setVisibility(View.VISIBLE) not working after using 'android:visibility="gone"' in xml

No comments:

Post a Comment