Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update TextView from Thread only updating after Thread

I am trying to make a book alike application which has pictures and text in it. I have used fragments for this and have added a voice over which starts when a fragment page is viewable and stops when it is not viewable.

I am now trying to highlight a word as it is said in the voice over. This is my code so far

In the fragment:

    @Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);

    // Make sure that we are currently visible
    if (this.isVisible()) {

        if(MainActivity.soundOn){
            if(!MainActivity.mediaPlayer.isPlaying()){
                try {
                    MainActivity.mediaPlayer.prepare();
                } catch (IllegalStateException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                MainActivity.mediaPlayer.start();
                MainActivity.start(getActivity(), sentence);
            }

            //MainActivity.test(sentence);
            MainActivity.mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    //MainActivity.start();
                }
            });
        }
        // If we are becoming invisible, then...
        if (!isVisibleToUser || !MainActivity.soundOn) { 
            stopPlaying(getActivity());
        }

    }
}

in the main activity:

public static void start(Activity activity, Sentence sent){
    //textControl.start();
    try{
        player = new Player(activity);
        player.set(sent, textControl);

        textControl.draw(sent, sent.words.get(0));

                    //while the mediaplayer is playing the voice over
        while(mediaPlayer.isPlaying())
        {
            player.run();
        }
    }
    catch(Exception e){
        e.printStackTrace();
    }

The Thread, I have removed most of the code for testing and readablitly:

    class Player extends Thread{

public int time;
public TextController control;
public int i = 1;
TextView tv;

private WeakReference<Activity> mActivity;
private Sentence sentence = null;

public Player(Activity activity) {
    mActivity = new WeakReference<Activity>(activity);
}

public void set(Sentence _sentence, TextController _control){
    sentence = _sentence;
    control = _control;
}


@Override
public void run()
{

    final Activity activity = mActivity.get();

    tv = (TextView) activity.findViewById(R.id.textView1);

        activity.runOnUiThread(new Runnable() {

            public void run() {

                    tv.setText("Testing");
                                //other stuff
            }
        });
    }
}

I want to know why the code "tv.setText("Testing");" won't update the textview right after its execution? Instead it updates the textview sometime after the method setUserVisibleHint(boolean isVisibleToUser) has finished.

I am also open to suggestions on better ways to do this.

If you would like more background info into the task and problem please feel free to ask as I have left it quite brief, thank you!

XML for fragment with textView:

<?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="horizontal"
  android:onClick="toggleAction">

<TextView
    android:id="@+id/textView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:gravity="center_horizontal"
    android:text="@string/fragment1"
    android:textSize="50sp" />

</LinearLayout>
like image 734
A User Avatar asked Nov 23 '25 02:11

A User


1 Answers

I could not fix this issue using this Thread and thus tried using a AsyncTask. This worked very well by updating the TextView in the onProgressUpdate

@Override
protected void onProgressUpdate(Spannable... result){
    view.setText(result[0]);
    view.invalidate();
}
like image 194
A User Avatar answered Nov 24 '25 17:11

A User



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!