user1841702
user1841702

Reputation: 2763

Android - View.post() does not modify TextView from onCreate()

Why does this not change my TextView's text ? I have a TextView who's text I try to modify from a view.post method. What am I doing wrong here ?

mTextView = (TextView)findViewById(R.id.text_view);

new Thread(new Runnable() {
    @Override
    public void run() {
        mTextView.post(new Runnable(){
            @Override
            public void run() {
                mTextView.setText("A change in text, thank you very much");
            }
        });
    }
}).start();

This runs in the Activity's onCreate() method. However, if I define a handler and pass the Runnable to the handler the TextView is modified.

Edit: The code I have here is based on the example:

public void onClick(View v) {
    new Thread(new Runnable() {
        public void run() {
            final Bitmap bitmap =
                    loadImageFromNetwork("http://example.com/image.png");
            mImageView.post(new Runnable() {
                public void run() {
                    mImageView.setImageBitmap(bitmap);
                }
            });
        }
    }).start();
}

at http://developer.android.com/guide/components/processes-and-threads.html

Edit 2: Now I am totally confused. When I run the same code from an onClickListener attached to a Button in my Activity, the TextView's text is indeed manipulated. Why did this not happen in the onCreate method ?

Upvotes: 0

Views: 2458

Answers (3)

Misha Akopov
Misha Akopov

Reputation: 13027

Just run without new Thread(new Runnable()

     mTextView.post(new Runnable(){

         @Override
         public void run() {

             // anything you want
         }
     });

Upvotes: 0

krishnan muthiah pillai
krishnan muthiah pillai

Reputation: 2741

In order to update a text view in activity you must use runOnUiThread

runOnUiThread(new Runnable() {
    @Override 
    public void run() { 
        mTextView.setText("A change in text, thank you very much"); 
    }
}); 

You can use With using AsycTask to load your Image in a Imageview

public class ImageLoadTask extends AsyncTask<Void, Void, Bitmap> {

    private String url;
    private ImageView imageView;

    public ImageLoadTask(String url, ImageView imageView) {
        this.url = url;
        this.imageView = imageView;
    } 

    @Override 
    protected Bitmap doInBackground(Void... params) {
        try { 
            URL urlConnection = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) urlConnection
                    .openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap myBitmap = BitmapFactory.decodeStream(input);
            return myBitmap;
        } catch (Exception e) {
            e.printStackTrace();
        } 
        return null; 
    } 

    @Override 
    protected void onPostExecute(Bitmap result) {
        super.onPostExecute(result);
        imageView.setImageBitmap(result);

       // you can also update your textview here 

    } 

} 

And Call this Task On Your Onclick

button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // pass your url and your imageview 
                // eg :http://example.com/image.png
                // imageView=(your ImageView)
                   new ImageLoadTask(url, imageView).execute();
            }
        });

Upvotes: 2

nvinayshetty
nvinayshetty

Reputation: 3237

Why you are running your code inside another thread? all the UI operations works only in the UI thread..try executing only the below code

mTextView = (TextView)findViewById(R.id.text_view);
mTextView.post(new Runnable() {
    @Override
    public void run() {
        mTextView.setText("A change in text, thank you very much");
    }
});

Upvotes: 0

Related Questions