Difference between setting text on a text view - android

What is the difference between
textView.setText("hello")
and
textView.post(new Runnable() {
#Override
public void run() {
mTxtTimer.setText(time);
}
});
If i have to set text in a loop which is beneficial for me.

textView.post(new Runnable() {
#Override
public void run() {
mTxtTimer.setText(time);
}
});
This is creating new child thread of working in UI(parent thread)
This will run in the UI thread, no matter who calls it.

Related

Android: Handler with MainLooper : "only the original thread that created a view hierarchy can touch its views."

From my generic fragment, I have this method:
protected void loadDataListWithDelay() {
Handler handler = new Handler(Looper.getMainLooper());
handler.postDelayed(new Runnable() {
#Override
public void run() {
loadDataList();
}
}, DELAY_START_LOADING);
}
This method allows to start a new loading data from server (with Volley networking).
In my loadDataList() method there are some views visibility setting:
protected void loadDataList(String url, ArrayList<BaseFilters> filters,
String query, boolean byPassSearchMode) {
...
mLoadingDataListView.setVisibility(View.INVISIBLE);
mListContainer.setVisibility(View.VISIBLE);
...
This code runs perfectly, but I have this crash this morning on mobile with Android 6.0.1.
Could you help me guys?
I will give you a good advice, if you want to do postDelayed, take a view in your fragment (or any view), and do postDelayed with it. i.e. textView.postDelayed(.. This way you can be sure you are on the ui thread.
Why not use runOnUiThread instead of creating a new handler ?
runOnUiThread(new Runnable() {
#Override
public void run() {
loadDataList();
}
});
If you need a delay, then you can use a handler inside the runOnUiThread
runOnUiThread(new Runnable() {
#Override
public void run() {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
loadDataList();
}
}, DELAY);
}
});

Updating UI In Android

I want to Update UI from background thread and if user wants to interact with UI while updating then the UI should not blocked by background thread.
Please Help me out.
This can be done using handlers to post UI updates from background thread. You can refer https://developer.android.com/training/multiple-threads/communicate-ui.html for code sample.
Using this you can update UI from backgorund thread
runOnUiThread()
runOnUiThread(new Runnable() {
#Override
public void run() {
//ui update stuff
}
});
You can use handler to serve this purpose.
A simple example:
Create a class:
class Task implements Runnable {
#Override
public void run() {
for (int i = 0; i <= 20; i++) {
final int value = i;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
handler.post(new Runnable() {
#Override
public void run() {
progressBar.setProgress(value);
}
});
}
}
}
Then call it from where you want to update the UI like this:
new Thread(new Task()).start();
Complete Example can be found here.
Or you can also go with different approach. Using runOnUiThread method will also allow you to achieve your expected result.
Example:
runOnUiThread(new Runnable() {
#Override
public void run() {
btn.setText("#" + i);
}
});

why I load long text in textView does not show me?¿

I use this new thread from a method called from onCreate().
info is a textView().
new Thread(new Runnable() {
public void run() {
info.post(new Runnable(){
public void run() {
info.setText(panel.getInfo());
}
});
}
}).start();
If info.setText(panel.getInfo()); call without creating the thread take 3-4 seconds being blocked application but showing it,
then how can I show the text without being blocked the app¿?
new Thread(new Runnable() {
public void run() {
String text = panel.getInfo();
info.post(new Runnable(){
public void run() {
info.setText(text);
}
});
}
}).start();
Actually what is taking long is your panel.getInfo() call. And your a making this call in the info.post, so you're doing it in the UI thread.
First of all, I think you should move code String text = panel.getInfo(); from info.post method, because it will be executed at UI thread. Do something like this:
new Thread(new Runnable() {
public void run() {
String text = panel.getInfo();
info.post(new Runnable(){
public void run() {
info.setText(text);
}
});
}
}).start();
Secondly, I think you should save your thread in class member, because GC may destroy your Thread, before it be in time to do something. So, do something like this:
mThread = new Thread(new Runnable() {
public void run() {
String text = panel.getInfo();
info.post(new Runnable(){
public void run() {
info.setText(text);
}
});
}
}).start();
Where mThread is a class member:
public abstract class MyActivity extends ActionBarActivity{
private Thread mThread;
/*Other code*/
}

Change ImageView for a few seconds and pause UI

Sorry, I keep on trying to adapt the tokens, but somehow I can't manage this one.
I have the following code:
timer.schedule(new TimerTask(){
runOnUiThread(new Runnable() {
public void run(){
SplashImage.setImageDrawable(aktieknop);}
});
},SplashTime);
}
Like this the code 'works':
timer.schedule(new TimerTask(){
// runOnUiThread(new Runnable() {
public void run(){
SplashImage.setImageDrawable(aktieknop);}
// });
},SplashTime);
}
Can you please help me solving this silly issue? Thanks a lot!
You must call this code line " SplashImage.setImageDrawable(nSplashImage); " from your run method in a runOnUIThread() method like this:
runOnUiThread(new Runnable() {
public void run() {
SplashImage.setImageDrawable(nSplashImage);
}
});
This is because you cannot change UI components on a non UI thread.
For the splash Screen you can use the Handler and send the delayed message.
Handler splashHandler = new Handler() {
#Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
//Here you can do what ever you want
}
};
int SPLASHTIME=2000;//your wish
splashHandler.sendMessageDelayed(msg, SPLASHTIME);

Android - How do I set View.GONE outside the UI thread?

I want to make a view disappear (to be gone) when the user pushes a button.
I can make it inside the onCreate() method (main UI thread) by doing:
findViewById(R.id.llLoadingGallery).setVisibility(View.GONE);
However, I want to be able to do the same thing inside another thread (out of the main UI thread). I tried to put the above live in my thread and it didn't work.
Thank you in advance!
## EDIT ####
To make myself more clear, I want to do something like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.teste_aba_3);
botao_tab_musica.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
...
findViewById(R.id.llLoadingGallery).setVisibility(View.GONE);
}
});
}
However, that DOESN'T work! How can I fix this?
theres a neat method called runOnUiThread
runOnUiThread(new Runnable() {
#Override
public void run() {
findViewById(R.id.llLoadingGallery).setVisibility(View.GONE);
}
});
edit: with your code
botao_tab_musica.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
runOnUiThread(new Runnable() {
#Override
public void run() {
findViewById(R.id.llLoadingGallery).setVisibility(View.GONE);
}
});
}
});
I'd suggest either using View.post(Runnable), View.postDelayed(Runnable, long), a Handler, or an AsyncTask to do this for you.
There are some good examples on how to post to the UI thread in these scenarios:
http://developer.android.com/resources/articles/painless-threading.html
I think one possibility is to pass a reference to your activity to that other thread so that your thread can access the findViewById method.
try this
findViewById(R.id.llLoadingGallery).post(new Runnable()
{
#Override
public void run()
{
findViewById(R.id.llLoadingGallery).setVisibility(View.GONE);
}
});
or create AsyncTask

Categories

Resources