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.

new project

  • Then type the Application name as “ex.no.7″ and click Next. 

application-name-7

  • Then select the Minimum SDK as shown below and click Next.

minimum sdk

  • Then select the Empty Activity and click Next. 

empty activity

  • Finally click Finish.

finish

  • It will take some time to build and load the project.
  • After completion it will look as given below.

new

Designing layout for the Android Application:

  • Click on app -> res -> layout -> activity_main.xml

activity_main

  • Now click on Text as shown below.

text

  • 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.

design-7

  • So now the designing part is completed.

Java Coding for the Android Application:

  • Click on app -> java -> com.example.exno7 -> MainActivity.

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:

multi threading    multi threading    multi threading

Result:

              Thus Android Application that implements Multi threading is developed and executed successfully.

 

1 Response

  1. sivanandham.k says:

    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 ..

Leave a Reply

Your email address will not be published. Required fields are marked *