Android Simple Thread Example

Carlos Gómez
devops and cross platform development
2 min readMay 19, 2014

--

In this article we are going to create a simple thread example, in the android documentation the thread is defined as a concurrent unit of execution. It’s very important to know that every thread has its own call stack for methods , arguments and local variables. We have two ways to execute the thread, one way is use a subclass thread and overriding its run() method, and the other way is construct a new Thread and pass a Runnable to the constructor, in both case we need to call the start() method to execute the thread.

Create a new android project

In this case we are going to use Android Studio. Launch Android Studio and create a “New Project” named SimpleThread with the appropriate package name and SDK selections. Select blank activity then on the New Blank Activity screen of the Android Application wizard, set the Activity Name to SimpleThreadActivity and the Layout Name to activity_simple_thread.

Edit the main layout

Let’s add a progress bar and a button, open the activity_simple_thread.xml and edit:

<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"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.devcfgc.simplethread.app.SimpleThreadActivity">
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/progressBarThread"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="85dp"
android:max="10" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/start_thread_button"
android:id="@+id/startBtnThread"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Android ui thread simple example
Create the threadOpen the SimpleThreadActivity.java. Main point of this thread is to update the progress bar value.public class SimpleThreadActivity extends Activity {private ProgressBar mProgressBar;
private Button mStartBtn;
private Thread mThread;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_simple_thread);
mProgressBar = (ProgressBar) findViewById(R.id.progressBarThread);
mStartBtn = (Button) findViewById(R.id.startBtnThread);
mStartBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startProgress(view);
}
});
}public void startProgress(View view) {
mProgressBar.setProgress(0);
mThread = new Thread(){
@Override
public void run(){
// Perform thread commands...
for (int i = 0; i <= 10; i++) {
final int value = i;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
mProgressBar.setProgress(value);
}
// Call the stopThread() method.
stopThread(this);
}
};
// Start the thread.
mThread.start();
}private synchronized void stopThread(Thread theThread)
{
if (theThread != null)
{
theThread = null;
}
}
}
Note: we must make sure to stop the thread correctly, and a good solution is pointed out in http://stackoverflow.com/questions/8505707/android-best-and-safe-way-to-stop-thread

--

--

Cloud/Software Architect and DevOps learning about #devops, #cloud, #netcore, #microservices and #newtech