View run-time update on Android

The example is quite simple: I want to tell the user what the application is doing by simply showing the text (canvas.drawText ()). Then my first message appears, but not others. I mean, I have a setText method, but it does not update.

onCreate(Bundle bundle) { super.onCreate(bundle); setContentView(splash); // splash is the view class loadResources(); splash.setText("this"); boundWebService(); splash.setText("that"): etc(); splash.setText("so on"); } 

The text drawing of the view works by doing only drawText in onDraw () ;, so setText changes the text but does not show it.

Someone recommended me replacing the SurfaceView, but it would be very difficult for just a few updates, SO ... how could I update the view dynamically at runtime?

It should be pretty simple, just showing the text in 2 seconds, and then the main thread making it material, and then updating the text ...

Thanks!

Update:

I tried implementing handler.onPost (), but this is the same story over and over again. Let me tell you the code:

 public class ThreadViewTestActivity extends Activity { Thread t; Splash splash; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); splash = new Splash(this); t = new Thread(splash); t.start(); splash.setTextow("OA"); try { Thread.sleep(4000); } catch (InterruptedException e) { } splash.setTextow("LALA"); } } 

and

 public class Splash implements Runnable { Activity activity; final Handler myHandler = new Handler(); public Splash(Activity activity) { this.activity=activity; } @Override public void run() { // TODO Auto-generated method stub } public synchronized void setTextow(final String textow) { // Wrap DownloadTask into another Runnable to track the statistics myHandler.post(new Runnable() { @Override public void run() { TextView t = (TextView)activity.findViewById(R.id.testo); t.setText(textow); t.invalidate(); } }); } } 

Although the surge in another thread, I put the dream in the main thread, I use the handler to control the user interface and everything, it does not change anything, it shows only the latest update.

+4
source share
3 answers

I haven't hit it yet, but I think the regular template consists of a long initialization in the background thread and uses Handler.post() to update the user interface. See http://developer.android.com/reference/android/widget/ProgressBar.html for another, but possibly related example.

Also see this answer , especially the first paragraph:

The problem is most likely that you are launching a splash screen (some kind of dialog, such as ProgressDialog I assume) in the same thread as all the work that is being done. This will continue the look of the splash screen from being updated, which can deter it from even appearing on the screen. You need to display the splash screen, start the AsyncTask instance for go, download all your data, then hide the splash screen after the task is complete.

Update (based on your update and your comment): you should not update the user interface in any thread except those where your action was created. Why can't you upload your resources to the background thread?

+5
source

First: onCreate runs in the main UI thread, so UI updates do not leave it yet. Basically, you need one thread to perform long-term tasks and some mechanism to make updates to the user interface.
The most common approach is the AsyncTask extension, to see this link for more information

+1
source

I assume your view is an extended view and you call onDraw to draw the view, so maybe the view does not update their state, so try this

 onCreate(Bundle bundle) { setContentView(splash); // splash is the view class loadResources(); splash.setText("this"); splash.invalidate(); boundWebService(); splash.setText("that"): splash.invalidate(); etc(); splash.setText("so on"); splash.invalidate(); } 
0
source

Source: https://habr.com/ru/post/1332953/


All Articles