0

Right now I have one thread which populates the view of my activity. But I want another thread to add some textviews and imageviews in the same activity. I am using SurfaceView inside which I created this thread and I don't know how to add another thread so that it can contribute to the view of the current activity.

Help me out..

MyView view;
protected void onCreate(Bundle savedInstanceState) {

    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    view = new MyView(this);

    setContentView(view);
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    view.pause();
}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    view.resume();
}

public class MyView extends SurfaceView implements Runnable {

    Thread threadstill = null;
    boolean isitok = false;
    SurfaceHolder holder;

    public MyView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        holder = getHolder();

    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        while (isitok == true) {
            if (!holder.getSurface().isValid()) {
                continue;
            }
            canvas = holder.lockCanvas();
            canvas.drawARGB(255, 255, 255, 255);
            canvas.drawBitmap(<bitmapimage>, x, y, null);
            holder.unlockCanvasAndPost(canvas);
        }

    }


   public void pause() {
        isitok = false;
        while (true) {
            try {
                threadstill.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            break;
        }
        threadstill = null;
    }

    public void resume() {
        isitok = true;
        threadstill = new Thread(this);
        threadstill.start();
    }
}
Prime
  • 15
  • 2

3 Answers3

1

You cannot directly manipulate the user interface from another thread. The main thread is responsible for all UI changes. However, if you want to perform some expensive work in the background (e.g. loading pictures) you can send the results to the UI thread.

Hava a look at the official documentation: https://developer.android.com/training/multiple-threads/communicate-ui.html

Or at a similiar question: How can I manipulate main thread UI elements from another thread in Android?

Community
  • 1
  • 1
0

I don't exactly know what is your issue but regarding to I don't know how to add another thread so that it can contribute to the view of the current activity. you can create a new thread easily with a nested class...simply call:

Thread myNewThread = new Thread(new Runnable() {
    public void run() {
    //do code here
    }
}).start();

But be Aware you can only change views from the UIThread so i would recommend you the AsyncTask:

public void myAsyncTask exstends Asynctask<Void,Void,Void> {

....
}

AsyncTask Comes with several methods the most important is the doInBackground where you can do the heavy things like Network Connections or other stuff which would freeze the UI or is simply not allowed on the UIThread(Network stuff).

After doInBackground is finished onPostExecute is called which runs on the UiThread and where you can process changes on your views.

To get more Information look at this link:

http://developer.android.com/reference/android/os/AsyncTask.html

Hope it helps and it is what you asked for. ;)

Mike
  • 857
  • 1
  • 7
  • 11
  • I tried to use nested threads but I realised that it will not serve the purpose, I am trying to push widgets like textview into the view using the second thread and nesting will make it visible only until second thread is running. This is because in nesting, I need to run them serially and not parallel (only possible if the things we are pushing are atomic).. – Prime Dec 30 '14 at 23:10
  • @prime yes you can run asynctask serially as well as parallel. You are welcome man – Mike Dec 30 '14 at 23:28
0

Do it with AsyncTask class. In doInBackground method download image, and in onPostExecute method apply image to ImageView.

I did it here already https://github.com/bajicdusko/AndroidJsonProvider/tree/master/app/src/main/java/com/bajicdusko/ajp/tools.

You can use my class or make your own .

bajicdusko
  • 1,630
  • 1
  • 17
  • 32
  • @Prime: If you use class from the link above (FetchLargeImage) you will have image caching implemented, and on every next call, existing image will be returned instead of downloading same image again. I am glad i could help you. Regards ;-) – bajicdusko Dec 30 '14 at 23:24