Updating Android UI using threads

I am writing an android chat app, i am listening for connections and i receive data and i can see it in the Log.d, but whenever i try to update my UI the application crash .. the code is

private class chatReceiver implements Runnable {
    @Override
    public void run() {
        try {
            skt = new DatagramSocket(Integer.parseInt(Main.prefs.getString("port_number", "5432")));
            DatagramPacket rcvPkt = new DatagramPacket(rcvBuf,rcvBuf.length);
            String ack = "Hello from our SimpleUDPServer";
            byte[] sndBuf = ack.getBytes();
            while (true) {
                Log.d("Server received: " ,"entered loop");
                skt.receive(rcvPkt);
                String rcvMsg = new String(rcvBuf, 0, rcvPkt.getLength(), "UTF-8");
                Log.d("Server received: " ,"receiving" + rcvMsg);
                if (rcvMsg != null) {
                    Log.d("Server received: " ,"not equal null");
                    // I want to update my UI here
                }
                DatagramPacket k = new DatagramPacket(sndBuf, sndBuf.length,
                        rcvPkt.getAddress(), rcvPkt.getPort());
                skt.send(k);
                Log.d("Server sent" ,ack);

            }
        } catch (IOException ex) {
            Log.d("ThreadStart", "Error Starting thread" + ex.getStackTrace());
        }

    }
} 

and to update the UI i use :

  • Publish .aar file with javadocs attached on artifactory repo
  • The method openOrCreateDatabase(String, int, null) is undefined
  • Must be one of: View.VISIBLE, View.INVISIBLE, View.GONE
  • Detecting Android L Battery Saver
  • Develop android Application using Html,Css and JavaScript
  • Set visibility of progress bar gone on completion of image loading using Glide library
  • public static void updateUI(Bubble b, View itemView) {
    
        TextView txt_display_name = (TextView) itemView
                .findViewById(R.id.display_name);
        txt_display_name.setText(b.getDisplay_name());
        TextView txt_chat_body = (TextView) itemView
                .findViewById(R.id.chat_body);
        txt_chat_body.setText(b.getChat_body());
        TextView txt_creation_date = (TextView) itemView
                .findViewById(R.id.creation_date);
        txt_creation_date.setText(b.getCreation_time());
    }
    

    my app keeps on crashing !!

    Related posts:

    IndexNotReadyException - Android Studio
    AndroidHttpClient versus HttpUrlConnection for API Level 9 and above
    How to draw a smaller ShapeDrawable inside another shapeDrawable programmatically
    Taking pictures with camera on Android programmatically
    How to register my own Application subclass in Xamarin.Android?
    Google cloud messaging sample
  • Adapt layout to Android platform or API level
  • Visual Studio Cordova build fails
  • GL error from OpenGLRenderer: 0x502
  • How to solve “invalid resource directory name”, resource “crunch”
  • BackupManager Not Calling Backup Transport
  • Detecting whether a BLE device is connectable on Android
  • 4 Solutions collect form web for “Updating Android UI using threads”

    you cannot touch anything in the ui thread from a background thread, to do that use Handlers, initialize your background thread passing it a Handler object. When data arrives use the handler to send a message to the ui. In the ui when the message from the background thread comes, just update the Views.

    something like this…

    in the background thread:

    if(dataArrives){
        Message msg = handler.obtainMessage();
        msg.what = UPDATE_IMAGE;
        msg.obj = bitmap;
        msg.arg1 = index;
        handler.sendMessage(msg);
    }
    

    in the ui thread:

    final Handler handler = new Handler(){
      @Override
      public void handleMessage(Message msg) {
        if(msg.what==UPDATE_IMAGE){
          images.get(msg.arg1).setImageBitmap((Bitmap) msg.obj);
        }
        super.handleMessage(msg);
      }
    };
    

    and pass the handler to the bakground thead

    You cannot change UI elements from a non-UI thread. Try using runOnUiThread.

    runOnUiThread(new Runnable(){
        @Override
        public void run(){
            // change UI elements here
        }
    });
    

    Or just use AsyncTask, it is more useful IMHO.

    Use this method to update your UI from worker thread, here is explained very well.

    **Note – If you are using progressBar.setProgress(progress) you can update directly from worker thread, because the ProgressBar internally call setProgress() method using Handler. **

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.