Android runOnUiThread not updating UI elements - android

Hi I am developing android application in which I am using grid view. I want to updated grid item dynamically.My grid item contains one title text. I tried to do it like this but it is not working for me.
((Activity)context).runOnUiThread(new Runnable()
{
public void run()
{
Debug.print("this is update progress inside thread ...");
owner.setText("Uploading...");
invalidate();
requestLayout();
}
});
So in above code its printing debug statement. But not updating my owner text which is inside grid item.
I tried this also...
Handler handler = new Handler() {
#Override
public void handleMessage(Message msg) {
owner.setText("Uploading...");
}
};
handler.sendEmptyMessage(0);

Try to refresh your adapter which you used to load the grid item.
Either you could use invalidateViews() method or adapter.notifyDataSetChanged()
This will help you to resolve your issue.
Happy coding..

So in above code its printing debug statement. But not updating my
owner text which is inside grid item.
I think it's textview does not reference to your item in grid adapter. You can check it by set tag when init and getTag() inside runonUIThread method.
Let check this textview is local or global variable. If global, it's only reference to the last item - then check the last item change?
OK if it's not. Let post full ur code.

runOnUiThread(new Runnable() {
#Override
public void run() {
Debug.print("Updating UI ...");
owner.setText("Uploading ...");
invalidate();
requestLayout();
}
});

Check if you have any Thread.sleep() in your code that might be blocking the UI.

Related

Android Add Items One By One Into a LinearLayout

I'm new to Android and I'm trying to find out how to do that:
- I have an activity with a ScrollView and inside it I have a LinearLayout (R.id.my_layout)
- I nedd to add TextView programmatically so I'm doing this:
I load the main XML layout via seContentView, I refer to my LinearLayout inside the ScrollView as "mLayout" and so on.
I load a list of names from a file and with a function called populateList()I do:
private void populateList() {
try {
for (final String team : mTeams) {
rCount++;
addRow(team);
}
}
The addRow() method just create a new LinearLayout (mRow), a TextView, 2 Button, add the TextView and the 2 Buttons to the LinearLayout, and then I use addView to add the new mRow to the mLayout.
Everything is working fine, but the ScrollView is shown only when i finished creating the list (so when the populateList() ends). What I would like to do is to show the rows one by one in sequence to give the activty a better look and a bit of animation.
Is there a way to do this?
I hope i was able to explain it :-)
Thank you
new Thread(new Runnable() { // i am creating the new thread
#Override
public void run() {
// so call populateList() function here
}
}).start();
and for your addRow(String string) method the place you call View.addView(); edit it this way and place the following code in your addRow(String string) method
View.post(new new Runnable() {// view here is mlayout the scrollView.
#Override
public void run() {
mlayout.addView(yourview); // note yourview should be final,
//eclipse might help you with that
}
});
remember to declare mlayout globally, so you do not have to attach final

Updating UI from listener - textview not getting updated

I am trying to update UI of a textview, progressbar and buttons inside a listener. Following is my code:
GetDownloadListener listener=new GetDownloadListener() {
#Override
public void onDownloaded(String path, int pos) {
//updating db
System.out.println("On downloaded"+contentCosts.get(pos).getRecoContentName());
updateDatabase(pos,path);
mainProgress.setIndeterminate(false);
mainProgress.setMax(contentCosts.size());
textViewContent.setText("Downloaded "+contentCosts.get(pos).getRecoContentName());// This textview does not get updated
downloadedList.add(contentCosts.get(pos));
downloadCount++;
mainProgress.setProgress(downloadCount);
if(downloadCount<contentCosts.size()){
callDownload(downloadCount);
}else{
DownloadingScreen.enableLaunchButton();
}
}
};
progressbar and buttons get updated based on the code inside the listener, but the line textViewContent.setText("Downloaded "+contentCosts.get(pos).getRecoContentName()); does not update the textview. What can be the reason why this textview alone does not get updated where as the other UI components get updated from the same listener.
Any help is much appreciated.

ListView doesn't scroll to end, when keyboard using

My ListView should automatically scroll to end, when new message received or sent.
But when I use Android keyboard to send message from EditText below ListView, ListView resized and new message, which I want to send, appears out of screen and I have to scroll down to see it, even if keyboard disappears and ListView resizes again.
To scroll ListView I use:
listView.postDelayed(new Runnable() {
#Override
public void run() {
listView.setSelection(listView.getCount());
}
}, 100);
but is not working correctly in my case.
Does anyone know why it may occur? Is there a way, which ALWAYS scroll ListView to end?
Thanks, your help is much appreciated!
Try this insead of your code:
listView.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
(or set it by XML layout).
You can try to add this line of code after you set selection to your list:
listView.smoothScrollToPosition(listView.getCount());
Resulting code should look like this:
listView.postDelayed(new Runnable() {
#Override
public void run() {
listView.setSelection(listView.getCount());
listView.smoothScrollToPosition(listView.getCount());
}
}, 100);
Set android:windowSoftInputMode in manifest for the activity to adjustPan|adjustResize.

How can i update listview background dynamically

I have tried various methods but none of them is working, what i what is changing the background image of cell of listview for this i have different images for cell. The content remain same. This is what i have tried yet.
CountryList.post(new Runnable(){
public void run() {
CountryList.invalidateViews();
}
});
// and this one also.
runOnUiThread(new Runnable() {
public void run() {
CountryList.invalidateViews();
}
});
As i dont content to be reloaded.
For this you can iterate over child components of list-view and get background of each one of them and change their background, for example get child at and so on.

how can move scroll bar automatically?

I am using grid view for display images, I want to scroll move automatically, whenever I add another images in gridview, is it possible?
I think you have to read a little bit more :
https://developer.android.com/reference/android/widget/AdapterView.html#setSelection(int)
ListView.setSelection(position);
https://developer.android.com/reference/android/widget/AbsListView.html#smoothScrollToPosition(int)
ListView.smoothScrollToPosition(position).
Either call setSelection() or use smoothScrollToPosition().
I got result using GridView.setSelection(GridView.getCount()),so scroll bar automatically move to last element of Grid View
Use TimerTask to do that since Adapter.notifyDataSetChanged() takes delay to update the grid or list.
Code is below:
new Timer().schedule(new TimerTask()
{
#Override
public void run() {
nameHandler.sendEmptyMessage(0);
}
},500);
//// And in handler::
Handler nameHandler = new Handler()
{
public void handleMessage(Message msg)
{
super.handleMessage(msg);
gridView.setSelection(selectorIndex);
// OR
gridView.smoothScrollToPosition(selectorIndex);
gridView.invalidate();
}
} ;

Categories

Resources