asdff asdff - 4 months ago 9
Android Question

Using Threads to change UI properties in fragment?

I'm using handlers to change some UI properties but i cannot see any change when the app runs. why isn't the code compiling? i used handlers in my thread to change UI buttons and images. i understand for images i could easily use Bitmaps to be more efficient but im using this to learn coding with threads to improve app performance.

Fragments code;

import android.media.Image;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.view.View;
import android.widget.TextView;
import android.widget.ImageView;
import android.widget.TextView;
import org.w3c.dom.Text;
import android.view.View;
import android.os.AsyncTask;
import java.net.URL;
import android.os.Handler;
import android.os.Message;


public class headercode extends Fragment {

ImageView image;
TextView text;
View myView;
Thread thread;

Handler handler = new Handler(){
@Override
public void handleMessage(Message msg)
{
image = (ImageView)myView.findViewById(R.id.image);
image.setBackgroundResource(R.drawable.ahmed);
}
};


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {


myView = inflater.inflate(R.layout.frag, container, false);
text = (TextView)myView.findViewById(R.id.text);
Runnable r = new Runnable()
{
@Override
public void run(){
handler.sendEmptyMessage(0);
}
};
return myView;
}
}


Main Activity Code where i call OnCreate() of course and display the initial fragments that is created with the Activity;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.FragmentManager;
import android.widget.FrameLayout;
import android.widget.Button;



public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {




Button button;
headercode header;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);



DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();



NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);

if (findViewById(R.id.fragment) != null){

header = new headercode();
getSupportFragmentManager().beginTransaction().add(R.id.fragment,header).commit();
}

}

Answer
Runnable r = new Runnable()
{
    @Override
    public void run(){
        handler.sendEmptyMessage(0);
    }
};

here you just define a Runnable, but you didn't post it to handler. so you should add one line after the Runnable defination:

handler.post(r);

And, in fact, you made many mistakes in your code:

if you use Runnable, you didn't need ovrride handleMessage, they are doing same work, so you just use Runnable or just use handleMessage, like following:

// Just use Runnable
@Override
public View onCreateView(...) {
    //...
    new Handler().post(new Runnable() {
        @Override
        public void run() {
            image = (ImageView)myView.findViewById(R.id.image);
            image.setBackgroundResource(R.drawable.ahmed);
        }
    })
    //...
}


//-------------------------------------------------
// Or just use handleMessage
Handler handler = new Handler(){
    @Override
    public void handleMessage(Message msg)
    {
        image = (ImageView)myView.findViewById(R.id.image);
        image.setBackgroundResource(R.drawable.ahmed);
    }
};

@Override 
public View onCreateView(...) {
    //...
    handler.sendEmptyMessage(0);
    //...
}

And, here in fact, you didn't use any sub-threads, all the invokes are in main thread. Use a Runnable or Handler doesn't mean that runs in sub-thread.

Comments