What Are Threads in Android With Example
What Are Threads in Android With Example
GlobalScope.launch {
Java
});
thread.start();
<LinearLayout
xmlns:android="http://schemas.android.com/ap
k/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/result_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp" />
<Button
android:id="@+id/start_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start" />
</LinearLayout>
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
resultTextView =
findViewById(R.id.result_text_view);
startButton.setOnClickListener(view ->
new BackgroundTask().execute());
@Override
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "Task Completed";
@Override
resultTextView.setText(result);
Output: