How to refresh the linear layout programmatically
Asked Answered
S

4

6

My application has one LinearLayout that LinearLayout consists of the 3 Linearlayouts and that each linear layout has one Relativelayout and one linear layout(initially the visibility of these linear layouts is gone). If you click on the relative layout then the regarding linear layout will be displayed. But how to refresh the entire linear layout after clicking on the relative layout.

Code:

private OnClickListener exit2Listener = new OnClickListener()
{
    public void onClick(View v)
    {
       if(!exit2status)
       {
          if(RB_Constant.upcomingexits_obj.response.size() > 1)
          {
             if(RB_Constant.upcomingexits_obj.response.get(1).listRestaurants.size() > 0)
             {
                 // Create the views on the fly instead of using the ListView
                 UpcomingResultsListViewAdapter2 rbupcadapter2 = new UpcomingResultsListViewAdapter2(RB_UpcomingExits.this);
                 int numItems2 = 0;

                 if(RB_Constant.upcomingexits_obj.response.get(1).listRestaurants.size() > 0)
                 {
                    numItems2 = RB_Constant.upcomingexits_obj.response.get(1).listRestaurants.size();
                 }

                 //linearLayout2
                 for(int position=0; position < numItems2; position++)
                 {
                    View convertview = null;
                    convertview = rbupcadapter2.getView(position, convertview, null);
                    listLayout2.addView(convertview);
                 }
             }              
          }
          else
          {
             //toastMsg("No results!");
          }
          listLayout2.setVisibility(View.VISIBLE);
          exit2status=true;

          if(!exit1status || exit3status || exit4status || exit5status)
          {
             //System.out.println("exit2 GONE");
             listLayout1.setVisibility(View.GONE);
             listLayout3.setVisibility(View.GONE);
             exit1status = false;
             exit3status = false;

          }

          LLExitDetails.invalidate();
       }
       else
       {
          System.out.println("exit2 GONE");
          listLayout2.setVisibility(View.GONE);
          exit2status = false;

          LLExitDetails.invalidate();
       }
    }       
};
Stative answered 6/7, 2011 at 12:39 Comment(0)
K
7

Retrieve the LinearLayout that contains everything. When you need to "refresh" it, call invalidate. Only call it in the UI thread though. If you call it in another thread (say a timer), then call postInvalidate. Both cause the View's onDraw method to be called when the OS is ready to call it.

Kopp answered 6/7, 2011 at 12:41 Comment(6)
i added the my code please see that. In the code "LLExitDetails" is main Linear Layout. and i add the invalidate but it is not working.Stative
not refreshing. means it's not show the first linear layout when i am call the invalidate.Stative
If the problem is that views don't become invisible when you call .setVisibility(View.GONE), then are you sure those methods are being called? Invalidate is how you redraw views. Another thing to try is to call LLExitDetails.invalidate() at the end of the "onClick" method outside any if-statement to ensure it's ALWAYS called. Then you can optimize when you have the logic of the method worked out.Kopp
is it possible to call invalidate without thread? because here i am calling the invalidate without thread.Stative
As stated, invalidate should always be called in UI thread (which you are doing) so don't worry about that.Kopp
i am putting the invalidate end of the onclick but it is not workingStative
F
1

I spent a lot of time to solve this problem too. And I found a simple method of refresh LinearLayout in 3 lines of code

You must set transperent color in style.xml

   <color name="transparent">#00000000</color>

And in the code just call to set background

   LinearLayout ll = (LinearLayout) findViewById(R.id.noteList);
   ll.setBackgroundColor(getResources().getColor(R.color.transparent));
   ll.invalidate();

If you has drawable background call

  ll.setBackgroundResource(R.drawable.your_drawable);
Foliate answered 20/3, 2015 at 18:29 Comment(0)
C
0

To refresh view as a graphical way use invalidate it's call onDraw() and for recalculate view dimensions and any thing related to height , width , margins and padding use requestLayout it's call onMeasure()

Clos answered 21/8, 2017 at 15:44 Comment(0)
R
-2
linearLayout.removeAllViews();
//The again load the linearlayout view
runLinearLayout();
Rendezvous answered 8/7, 2015 at 10:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.