Home » Android » Refresh all view in android?

Refresh all view in android?

Posted by: admin June 15, 2020 Leave a comment


I working on messaging platform like whatsup.When ı send message ı must update the screen because ı am getting data in db.When i press send button ı must update View.
I googled but ı can not exact solution.Can anybody help me?


my code:

RelativeLayout layout=new RelativeLayout(this);
                LayoutParams lparams = new LayoutParams(
                // Creating a new TextView
                TextView tv = new TextView(this);
                tv.setPadding(30, 10, 0, 0); 
How to&Answers:

You will need to call either requestLayout() or invalidate() depend on what you update exactly in your view

If you just need the View to redraw so call invalidate()

If you change the View bounds (e.g. size) call requestLayout()


if you use a listview with a listadapter, then you have to use listadapter.notifyDataSetChanged(); this will update your listview with the new data


Have you try with view.invalidate()? http://developer.android.com/reference/android/view/View.html


Drawing is handled by walking the tree and rendering each view that
intersects the invalid region. Because the tree is traversed in-order,
this means that parents will draw before (i.e., behind) their
children, with siblings drawn in the order they appear in the tree. If
you set a background drawable for a View, then the View will draw it
for you before calling back to its onDraw() method.

Note that the framework will not draw views that are not in the
invalid region.

To force a view to draw, call invalidate().

you can try like the reply in this post invalidate the viewgroup:
How to force an entire layout View refresh?


Finally if you have only a TextView in your layout try this:

//Supposing that msgdesc is a class field.

void myRefreshFunction(RelativeLayout l)
                if(l != null)
                  TextView tv = new TextView(this);
                  tv.setPadding(30, 10, 0, 0); 


see Dianne’s answer:

invalidate() just causes the views to be redrawn: http://developer.android.com/reference/android/view/View.html#invalidate()

requestLayout() asks that the views go through a new measure/layout pass (and then redraw): http://developer.android.com/reference/android/view/View.html#requestLayout()


If you’re building a messaging app and items are added to some kind of list, I suggest using a RecyclerView and Recyclerview Adapter to achieve what you’re trying to do. When the list grows in size, your relative layout won’t be scrollabe, a RecyclerView however would. On top of that RecyclerView gives you more performance with very long list because from the name ~Recycle~rView, it recycles previous items and thus increases preformance.

Here’s a reasonable tutorial on how to achieve what you want:

(I’m not affiliated with SendBird or anything, it’s just the first result when you google: RecyclerView chat example)