• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps How to add at a main layout an imageView controlled from another thread?

lugopaco

Newbie
Mar 31, 2011
30
0
Mexico
Hi!! I'm making an app for Android. I have reached my goal with the single main thread pushing a button!!! (show an image saved on the SD in an ImageView) But I need to do it with threading to save some time and because I will make other threads.

The problem when I do this on a new thread a warning appears that tells me:

"Only the original thread that created a view hierachy can touch its
views."
And the Image is not opened.

Here is this code:

Code:
  public class intsocketclient extends Activity implements  OnClickListener{
        public ImageView imagen;
            private Button connectPhones;               
            private Handler conectarhandler = null;
        private Runnable conectarunner = null;
    public boolean condicion = true;
            
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);      
                imagen = (ImageView) findViewById(R.id.imagen);
                connectPhones = (Button) findViewById(R.id.connect_phones);
                connectPhones.setOnClickListener(this);
                
                conectarhandler = new Handler();         
                conectarunner = new Runnable() { 
                    public void run() {
                    conectayenvia(); 
                conectarhandler.post(this);          
                               
                    }
              };                    
            }
    
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub.
         if(v==connectPhones) {                  
             new Thread (conectarunner).start();                    
         }
    }
    
    public void conectayenvia () {
        if (condicion){                 
                      condicion = false;
                      Bitmap bMap = BitmapFactory.decodeFile("/sdcard/recibido.jpg");
                      imagen.setImageBitmap(bMap);                                           
         }    
        }    
    }
But I really need it to be this way.

Is it possible to take the main layout (the original "main.xml") and some kind of "add" over it another main file (a "threadmain.xml" which only contains the imageView)but also with the capability of pushing buttons and other kind of things of the first original "main.xml" layout????????
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones