Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

EXP 26

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".MainActivity">

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="sleep time in second"

android:textSize="30dp"

android:id="@+id/t1"

android:layout_marginTop="20dp"

/>

<EditText

android:id="@+id/et1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginLeft="280dp"

android:layout_marginTop="20dp"

android:ems="10"

android:inputType="number"

tools:ignore="TouchTargetSizeCheck,SpeakableTextPresentCheck" />

<Button

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/b1"

android:text=" run async task"

android:textSize="20dp"

android:layout_marginTop="70dp"

android:layout_marginLeft="70dp"

/>
<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/t2"

android:layout_below="@id/b1"

android:layout_marginLeft="140dp"

android:textSize="20dp"/>

</RelativeLayout>

MAIN ACTIVITY.JAVA

public class MainActivity extends AppCompatActivity {

Button b1;

TextView t1, t2;

EditText et1;

@SuppressLint("MissingInflatedId")

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

b1 = (Button) findViewById(R.id.b1);

t1 = (TextView) findViewById(R.id.t1);

t2 = (TextView) findViewById(R.id.t2);

et1 = (EditText) findViewById(R.id.et1);

b1.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

AsyncTaskRunner runner = new AsyncTaskRunner();

String sleepTime = et1.getText().toString();

runner.execute(sleepTime);

});
private class AsyncTaskRunner extends AsyncTask<String, String, String> {

String resp;

ProgressDialog progressDialog;

@Override

protected String doInBackground(String... params) {

publishProgress("Sleeping");

try {

int time = Integer.parseInt(params[0]) * 1000;

Thread.sleep(time);

resp = "Sleep for" + params[0] + "seconds";

} catch (InterruptedException e) {

e.printStackTrace();

} catch (Exception e) {

resp = e.getMessage();

return resp;

@Override

protected void onPostExecute(String result) {

progressDialog.dismiss();

t2.setText(result);

@Override

protected void onPreExecute() {

progressDialog = ProgressDialog.show(MainActivity.this, "ProgressDialog", "Wait for" +


et1.getText().toString() + "seconds");

@Override

protected void onProgressUpdate(String... text) {

t2.setText(text[0]);

You might also like