Android Application that implements Multi threading
Aim:
To develop a Android Application that implements Multi threading.
Procedure:
Creating a New project:
- Open Android Studio and then click on File -> New -> New project.
- Then type the Application name as “ex.no.7″ and click Next.
- Then select the Minimum SDK as shown below and click Next.
- Then select the Empty Activity and click Next.
- Finally click Finish.
- It will take some time to build and load the project.
- After completion it will look as given below.
Designing layout for the Android Application:
- Click on app -> res -> layout -> activity_main.xml
- Now click on Text as shown below.
- Then delete the code which is there and type the code as given below.
Code for Activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_margin="50dp"
android:layout_gravity="center" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_gravity="center"
android:text="Load Image 1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_gravity="center"
android:text="Load image 2" />
</LinearLayout>
- Now click on Design and your application will look as given below.
- So now the designing part is completed.
Java Coding for the Android Application:
- Click on app -> java -> com.example.exno7 -> MainActivity.
- Then delete the code which is there and type the code as given below.
Code for MainActivity.java:
package com.example.exno7;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity
{
ImageView img;
Button bt1,bt2;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt1 = (Button)findViewById(R.id.button);
bt2= (Button) findViewById(R.id.button2);
img = (ImageView)findViewById(R.id.imageView);
bt1.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
new Thread(new Runnable()
{
@Override
public void run()
{
img.post(new Runnable()
{
@Override
public void run()
{
img.setImageResource(R.drawable.india1);
}
});
}
}).start();
}
});
bt2.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
new Thread(new Runnable()
{
@Override
public void run()
{
img.post(new Runnable()
{
@Override
public void run()
{
img.setImageResource(R.drawable.india2);
}
});
}
}).start();
}
});
}
}
- So now the Coding part is also completed.
- Now run the application to see the output.
Note: Before Running the Application, Copy the Images given below and Paste it in “app -> res -> drawable” by pressing “right click mouse button on drawable” and selecting the “Paste” option.
TO DOWNLOAD THE IMAGES : CLICK HERE
Output:
Result:
Thus Android Application that implements Multi threading is developed and executed successfully.














very useful for me..thanks its working.. i am working as assistant professor in engineering college. . your coding is useful for my students to get output in lab. thanks ..
I am a student in engineering college. your coding is useful for me in lab. thank you.
Thankyou for this code , its very useful for my lab program . i am an engineering student .
i am a engineering student i like that concepts of program and this coding is used to get the output in laboratory is very simple.
nice..it was verymuch helpful…keep on doing this for upcomming labs too.