Android: Updating the UI from an adapter inside a While Loop

Scott

I'm using a GridView to display a list of words in a 4-column table. The getView method of my custom adapter checks the width of the word and shrinks it if it doesn't fit. It does this using a recursive check that keeps scaling the text down until it fits.

private void shrinkText(final TextView wv, final String word) {
    wv.setTextSize(defaultTextSize);
    new Thread(new Runnable() {
        @Override
        public void run() {
            while (wv.getWidth()>0 && wv.getPaint().measureText(word) > wv.getWidth()) {
                Logg.d("word too big. Shrink from " + wv.getTextSize()/density + " to " + (wv.getTextSize()/density-1.0f));
                wv.setTextSize(wv.getTextSize() / density - 1.0f);
            }
        }
    }).start();
}

Because I'm using a while loop, I am using a new thread to protect against ANR in the unlikely event of an infinite loop. Here's the weird thing: sometimes it works great. And then sometimes I get the following error:

09-26 14:25:31.389    6427-7765/com.myapp.debug E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-7789
    Process: com.myapp.debug, PID: 6427
    android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

I tried putting the setTextSize inside a runOnUiThread statement, but I can't get it to work inside the adapter. Ultimately I just want this to work. I think my options are:

  1. Keep the while loop in the UI thread and somehow safeguard it (how?)
  2. Move the setTextSize call to the UI thread (how?)
  3. Something else?

Thanks for your help!

UPDATE: based on Rustam's answer, I used wv.post to write to the UI. However the need to use the while loop meant that setTextSize had to be done in the same thread as the while condition itself. I switched from evaluating the TextView.getTextSize to the Paint.getTextSize, since I could set the Paint's text size without impacting the UI, and therefore inside the offshoot thread. Jerry-rigged, but it seems to work.

private void shrinkText(final TextView wv, final String word) {
    wv.setTextSize(defaultTextSize);
    final Paint mPaint = new Paint(wv.getPaint());
    new Thread(new Runnable() {
        @Override
        public void run() {
            while (wv.getWidth()>0 && mPaint.measureText(word) > wv.getWidth()) {
                Logg.d("word too big. Shrink from " + mPaint.getTextSize()/density + " to " + (mPaint.getTextSize()/density-1.0f));
                mPaint.setTextSize(mPaint.getTextSize() - 1.0f);
            }
            if (wv.getWidth()>0 && wv.getPaint().measureText(word) > wv.getWidth()) {
                wv.post(new Runnable() {
                    @Override
                    public void run() {
                        Logg.d(word + " final size=" + mPaint.getTextSize() / density);
                        wv.setTextSize(mPaint.getTextSize() / density);
                    }
                });
            }
        }
    }).start();
}
Rustam

try to update like this:

private void shrinkText(final TextView wv, final String word) {
    wv.setTextSize(defaultTextSize);
    new Thread(new Runnable() {
        @Override
        public void run() {
                while (wv.getWidth()>0 && wv.getPaint().measureText(word) > wv.getWidth()) {
                Logg.d("word too big. Shrink from " + wv.getTextSize()/density + " to " + (wv.getTextSize()/density-1.0f));
                wv.post(new Runnable() {
                   public void run() {
                      wv.setTextSize(wv.getTextSize() / density - 1.0f);
                   }
               });
        }
    }).start();
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Android: Updating the UI from an adapter inside a While Loop

From Dev

Updating Date inside a while loop, Java

From Dev

Android, create file from input stream hangs inside while loop

From Dev

QT: QGraphicsView not updating from inside loop

From Dev

Handler freezes UI when processing while loop inside a runnable task [android]

From Dev

Updating View while running loop inside another class?

From Dev

Python updating lists error! List will not update inside while loop

From Dev

How to break a while loop from an if condition inside the while loop?

From Dev

How to Fetch data from while loop inside a while loop

From Dev

While loop inside a while loop from PDO query

From Dev

Updating Android UI from custom view

From Dev

Android: Updating UI from thread in separate class

From Dev

Updating textView with while loop?

From Dev

Python while loop not updating

From Dev

NullPointer Exception while updating UI from AsyncTask is Finished

From Dev

UI Hangs When While Loop is Placed Inside Runnable

From Dev

Edit EditText from button click inside a List Adapter Android

From Dev

Extracting result from a for loop inside while loop - r

From Dev

updating UI thread while in asynctask

From Dev

UI freezes while updating ObservableCollection

From Dev

Python While Loop - Variable Not Updating?

From Dev

If inside while loop

From Dev

Free() inside a while loop

From Dev

Scanner inside while loop

From Dev

While loop inside SELECT

From Dev

Free() inside a while loop

From Dev

cin inside a while loop

From Dev

If statement inside a While loop

From Dev

Error inside a while loop