Java Class
----------------
package com.example.handleexample;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button sample = (Button) findViewById(R.id.button1);
sample.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
fetchData();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
private void fetchData() {
// TODO Auto-generated method stub
progressDialog = ProgressDialog.show(this, "", "Doing...");
new Thread() {
public void run() {
String text = "hello world";
Message msg = Message.obtain();
msg.what = 2;
try {
Thread.sleep(800);
} catch (InterruptedException e) {
}
Bundle b = new Bundle();
b.putString("text", text);
msg.setData(b);
messageHandler.sendMessage(msg);
}
}.start();
}
private Handler messageHandler = new Handler() {
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 1:
// ImageView img = (ImageView) findViewById(R.id.imageview01);
// img.setImageBitmap((Bitmap)(msg.getData().getParcelable("bitmap")));
break;
case 2:
TextView text = (TextView) findViewById(R.id.textView2);
System.out.println("Test here "
+ msg.getData().getString("text"));
text.setText(msg.getData().getString("text"));
break;
}
progressDialog.dismiss();
}
};
}
activity_main.xml
----------------------
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="50dp"
android:layout_toLeftOf="@+id/textView1"
android:text="Button" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="22dp"
android:text="TextView" />
</RelativeLayout>
SampleOutPut
------------------
----------------
package com.example.handleexample;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button sample = (Button) findViewById(R.id.button1);
sample.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
fetchData();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
private void fetchData() {
// TODO Auto-generated method stub
progressDialog = ProgressDialog.show(this, "", "Doing...");
new Thread() {
public void run() {
String text = "hello world";
Message msg = Message.obtain();
msg.what = 2;
try {
Thread.sleep(800);
} catch (InterruptedException e) {
}
Bundle b = new Bundle();
b.putString("text", text);
msg.setData(b);
messageHandler.sendMessage(msg);
}
}.start();
}
private Handler messageHandler = new Handler() {
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 1:
// ImageView img = (ImageView) findViewById(R.id.imageview01);
// img.setImageBitmap((Bitmap)(msg.getData().getParcelable("bitmap")));
break;
case 2:
TextView text = (TextView) findViewById(R.id.textView2);
System.out.println("Test here "
+ msg.getData().getString("text"));
text.setText(msg.getData().getString("text"));
break;
}
progressDialog.dismiss();
}
};
}
activity_main.xml
----------------------
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="50dp"
android:layout_toLeftOf="@+id/textView1"
android:text="Button" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="22dp"
android:text="TextView" />
</RelativeLayout>
SampleOutPut
------------------
No comments:
Post a Comment