Everywhere I’ve seen, they have used Threading with onClick. Now i am kind of forced to use Threading as i can not perform a networking operation on its main thread as documentation says. So how would i put The threading on a button that i have coded something like this?
public void firstbutton(View view)
{
//some code
}
Thanks for the help!
EDIT:
public void firstbutton(View view)
{
InputMethodManager inputMgr = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
EditText editText = (EditText)findViewById(R.id.editText1);
inputMgr.hideSoftInputFromWindow(editText.getWindowToken(), 0);
EditText idnumber=(EditText)findViewById(R.id.editText1);
String idnumber2= idnumber.getText().toString();
int i = Integer.parseInt(editText.getText().toString());
idnum=i;
setContentView(R.layout.viewer);
Context context = view.getContext();
Drawable image = ImageOperations(context, WEB ADDRESS HIDDEN FOR PRIVACY"+idnumber2);
ImageView icon = new ImageView(context);
icon = (ImageView)findViewById(R.id.imageView1);
icon.setImageDrawable(image);
};
public Drawable ImageOperations(Context ctx, String url) {
try {
InputStream is = (InputStream) this.fetch(url);
Drawable d = Drawable.createFromStream(is, "src");
return d;
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public Object fetch(String address) throws MalformedURLException,IOException
{
URL url = new URL(address);
Object content = url.getContent();
return content;
}
It depends on what you want to achieve.
If you just want to start a new thread for your networking stuff, you can use the
Threadmethod like this:If you need to update the UI once the network operation is done,
AsyncTaskis probably a better choice: