Mad 55
Mad 55
IT Department
MOBILE APPLICATION & DEVELOPMENT
(3161612)
Laboratory Manual
NAME
ENROLLMENT NUMBER
BATCH
YEAR
VISION
“To impart quality education and research in Information Technology to produce a competent, committed
and goal oriented workforce to fulfill the needs of the local and global IT Industry”
It gives us immense pleasure to present the first edition of Mobile Application Development Practical Book for
the B.E. 3rd year students of Ahmedabad Institute of Technology.
The lab exercises in this manual are designed to be both educational and practical. Each lab session provides
clear instructions, code snippets, and real-world examples to help you understand and apply the concepts.
Additionally, each exercise is accompanied by challenges to test your knowledge and encourage problem-
solving skills.To successfully complete the labs, you will need access to certain tools and resources,
including integrated development environments (IDEs) like Xcode and Android Studio, as well as emulators
and physical devices for testing. Detailed instructions on setting up these tools are provided in the initial
chapters..
The student is required to keep a laboratory manual in which the raw data will be recorded as well as the
questions will be kept. The lab write-ups form a permanent record of your work.
Do’s:
Don’ts:
CERTIFICATE
This is to certify that Mr. / Ms.
Of Enrolment No has Satisfactorily completed
the course in as by the Gujarat
Technological University for Year (B.E.) semester of Computer
Engineering in the Academic year .
Date of Submission:-
Faculty Name :
Signature: Head Of Department (IT)
INDEX
Sr Experiment Title Page DATE SIGN
No No.
3 Create an application that will get the Text Entered in Edit Text
and display that Text using toast (Message).
9 Create an application that uses the image button and clicking this
button change the image of it and also handle onLongClick event.
Theory:
Prerequisites for this android project
Java: First of all you need to have the knowledge of Java Programming. Java programming plays a very
important role as we will develop the app code in Java
XML: XML is another important part of our android application. It will be used for the development of the user
interface for the application
Android Studio: Android Studio is the backbone of our application, as we will develop our app using android
studio. Android virtual device is also shipped with android studio that will be helpful in testing whether the
applications are working or not
Code:
● activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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="Hello World!"
android:textColor="#FF0000"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
● MainActivity.java
package com.example.prac1;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
23002311605
5
}
}
Output
PRACTICAL – 2
AIM: Create an android application that demonstrates the android activity life cycle.
Theory:
Android Activity Lifecycle is controlled by 7 methods of android.app.Activity class. The android Activity
is the subclass of ContextThemeWrapper class.An activity is the single screen in android. It is like a
window or frame of Java. By the help of activity, you can place all your UI components or widgets in a
single screen. The 7 lifecycle method of Activity describes how activity will behave at different states.
onResume() called when activity will start interacting with the user.
Code:
● activity_main.xml
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</RelativeLayout>
● MainActivity.java
package com.example.prac2;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("Lifecycle","OnCreateInvoke");
}
@Override
protected void onStart(){
super.onStart();
Log.d("Lifecycle","OnStartInvoke");
}
@Override
protected void onResume(){
super.onResume();
Log.d("Lifecycle","OnResumeInvoke");
}
@Override
protected void onPause(){
super.onPause();
Log.d("Lifecycle","OnPauseInvoke");
}
@Override
protected void onStop(){
super.onStop();
Log.d("Lifecycle","OnStopInvoke");
}
@Override
protected void onRestart(){
23002311605
5
super.onRestart();
Log.d("Lifecycle","OnRestartInvoke");
}
@Override
protected void onDestroy(){
super.onDestroy();
Log.d("Lifecycle","OnDestroyInvoke");
}
}
Output
23002311605
5
Practical:-3
AIM:-Create an application that will get the Text Entered in Edit Text and display
that Text using toast (Message).
Theory:
What is Toast Massage?
A toast provides simple feedback about an operation in a small popup. It only fills the amount of
space required for the message and the current activity remains visible and interactive. Toasts
automatically disappear after a timeout.
For example, clicking Send on an email triggers a "Sending message..." toast, as shown in the
following screen capture:
If your app targets Android 12 (API level 31) or higher, its toast is limited to two lines of text and
shows the application icon next to the text. Be aware that the line length of this text varies by
screen size, so it's good to make the text as short as possible.
Code:
● activity_main.xml
<?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">
<EditText
android:id="@+id/edit_text"
android:hint="Name!!!"
android:layout_centerInParent="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/edit_text"
android:layout_centerInParent="true"
/>
</RelativeLayout>
● MainActivity.java
package com.example.prac2;
import android.appcompat.app.AppCompatActivity;
import android.view.View;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity{
23002311605
5
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click_me (View view) {
EditText e = findViewById(R.id.edit_text);
Toast.makeText(getApplicationContext(),e.getText().toString(),Toast.LENGTH_SHORT).show();
}
}
Output
23002311605
5
PRACTICAL – 4
AIM:-Create an application in which when a user clicks a button, the user will
navigate to the next screen which displays a welcome message (using Explicit
Intent).
Theory :
Android Intent is the message that is passed between components such as activities, content
providers, broadcast receivers, services etc.It is generally used with startActivity() method to
invoke activity, broadcast receivers etc.
The dictionary meaning of intent is intention or purpose. So, it can be described as the intention
to do action.
Android intents are mainly used to:
● Start the service
● Launch an activity
● Display a web page
● Display a list of contacts
● Broadcast a message
● Dial a phone call etc.
Types of Android Intents :
1) Implicit Intent:
Implicit Intent doesn't specify the component. In such case, intent provides information of
available components provided by the system that is to be invoked
For example, you may write the following code to view the webpage:
Intent intent=new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("web page url"));
startActivity(intent);
2) Explicit Intent:
Explicit Intent specifies the component. In such a case, intent provides the external class to be
invoked.
Android Explicit intent specifies the component to be invoked from activity. In other words, we
can call another activity in android by explicit intent.
We can also pass the information from one activity to another using explicit intent.Here, we are
going to see an example to call one activity from another and vice-versa.
Code:
● activity_main.xml
<?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">
<Button
android:id="@+id/butt_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Welcome to next page"
android:textColor="@color/white"
android:onClick="OnClick" />
</RelativeLayout>
● MainActivity.java
package com.example.prac3;
import android.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
● activity_main2.xml
23002311605
5
<?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=".MainActivity2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome!!!!"/>
</RelativeLayout>
● MainActivity2.java
package com.example.prac3;
import android.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity2 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
}
Output
23002311605
5
Post Practical Questions:
1. How can we stop the services in android?
A. Stopself() and stopservice()
B. Finish()
C. System.exit()
D. None of the above
2. What is the use of content provider in android?
A. Storing data in database
B. Sharing data between application
C. Sending data from one app to another app
D. None
3. Which of the following method is used to handle what happens after clicking
a button?
A. onClick
B. onCreate
C. onSelect
D. None
4. An Android is an abstract description of an operation to be performed. It can be used
with startActivity to launch an Activity.
A. Filters
B. Intent
C. Service
D. Broadcast receiver
5. Which of the following action will display the phone dialer with the given number
filled in?
A. ACTION_VIEW tel:123
B. ACTION_SET
C. ACTION_DIAL tel:123
D. None
23002311605
5
Practical:- 5
AIM:-Create an application that will pass two numbers using EditText to the next
screen , and on the next screen display the sum of that number.
Theory:-
Text view to display any text. In this example we are going to create an app to add two numbers.
Create an android project and use the following Text Fields control to take input from the user.
I have taken two textview to display messages and one textview to display output. Two plan text fields i.e
EditText to take input and one button.
In XML:
STEP-1: Declare a few variables and the values entered in the Text Views can be read by
using an id which we have set in the XML code above.
STEP-2: Add the click listener to the Add button.
STEP-3: When the Add button has been clicked, add the values and store it into the sum
variable.
STEP-4: To show the output in the result text view, set the sum in the textview.
Example:
public void sum(View v)
{
Code:
● activity_main.xml
23002311605
5
<?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">
<EditText
android:id="@+id/edt1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:hint="Enter no."
/>
<EditText
android:id="@+id/edt2"
android:layout_below="@id/edt1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter no."
android:layout_centerVertical="true"
/>
<Button
android:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/edt2"
android:text="Submit"/>
</RelativeLayout>
● MainActivity.java
package com.example.prac5;
import android.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Button;
● activity_2.xml
<?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=".Activity2">
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
</RelativeLayout>
● Activity2.java
package com.example.prac5;
import android.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
23002311605
5
import android.widget.TextView;
Textview textview;
@Override
Output
23002311605
5
Practical :-6
AIM:-Create an application with three different Radio Button and by clicking
them change the background of activity.
Theory:-
Radio buttons allow the user to select one option from a set. You should use radio buttons for
optional sets that are mutually exclusive if you think that the user needs to see all available
options side-by-side. If it's not necessary to show all options side-by-side, use a spinner instead.
To create each radio button option, create a RadioButton in your layout. However, because radio
buttons are mutually exclusive, you must group them together inside a RadioGroup. By grouping
them together, the system ensures that only one radio button can be selected at a time.
The method you declare in the android:onClick attribute must have a signature exactly as shown
above. Specifically, the method must:
● Be public
● Return void
● Define a View as its only parameter (this will be the View that was clicked)
Code:
● activity_main.xml
● MainActivity.java
package com.example.prac6;
import android.annotation.Nullable;
import android.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.graphics.Color;
public class MainActivity extends AppCompatActivity {
23002311605
5
RadioButton r,b,g;
RelativeLayout l1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
l1= findViewById(R.id.layout);
r= findViewById(R.id.rdr);
b= findViewById(R.id.rdb);
g= findViewById(R.id.rdg);
}
public void onClick (View v)
{
boolean checked = ((RadioButton) v).isChecked();
switch(v.getId()) {
case R.id.rdr:
if (checked)
l1.setBackgroundColor(Color.RED);
break;
case R.id.rdb:
if (checked)
l1.setBackgroundColor(Color.BLUE);
break;
case R.id.rdg:
if (checked)
l1.setBackgroundColor(Color.GREEN);
break;
}
}
}
Output
23002311605
5
Practical -7
Code:
● activity_main.xml
<EditText
android:id="@+id/et1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter number"
android:layout_marginTop="5dp"/>
<EditText
android:id="@+id/et2"
android:layout_width="match_parent"
23002311605
5
android:layout_height="wrap_content"
android:hint="Enter number"
android:layout_below="@id/et1"
android:layout_marginTop="5dp"/>
<Button
android:id="@+id/add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/et2"
android:text="Addition"
android:onClick="OnClickAdd"
android:layout_marginTop="5dp"
android:layout_marginHorizontal="115dp"/>
<Button
android:id="@+id/sub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/add"
android:text="Subtraction"
android:onClick="OnClickSub"
android:layout_marginTop="5dp"
android:layout_marginHorizontal="115dp"/>
<Button
android:id="@+id/mul"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/sub"
android:text="Multiplication"
android:onClick="OnClickMul"
android:layout_marginTop="5dp"
android:layout_marginHorizontal="115dp"/>
<Button
android:id="@+id/div"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/mul"
android:text="Division"
android:onClick="OnClickDiv"
android:layout_marginTop="5dp"
android:layout_marginHorizontal="115dp"/>
<Button
android:id="@+id/max"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/div"
android:text="Maximum No."
android:onClick="OnClickMax"
android:layout_marginTop="5dp"
23002311605
5
android:layout_marginHorizontal="115dp"/>
</RelativeLayout>
● MainActivity.java
package com.example.prac8;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity
{ EditText ed1,ed2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed1= findViewById(R.id.et1);
ed2= findViewById(R.id.et2);
}
public void OnClickAdd(View v)
{
Double no1= Double.parseDouble(ed1.getText().toString());
Double no2= Double.parseDouble(ed2.getText().toString());
Double ans= no1+no2;
Toast.makeText(MainActivity.this,"Addition is
"+ans.toString(),Toast.LENGTH_LONG).show();
}
public void OnClickSub(View v)
{
Double no1= Double.parseDouble(ed1.getText().toString());
Double no2= Double.parseDouble(ed2.getText().toString());
Double ans= no1-no2;
Toast.makeText(MainActivity.this,"Subtraction is
"+ans.toString(),Toast.LENGTH_LONG).show();
}
public void OnClickMul(View v)
{
Double no1= Double.parseDouble(ed1.getText().toString());
Double no2= Double.parseDouble(ed2.getText().toString());
Double ans= no1*no2;
Toast.makeText(MainActivity.this,"Multiplication is
"+ans.toString(),Toast.LENGTH_LONG).show();
}
public void OnClickDiv(View v)
{
Double no1= Double.parseDouble(ed1.getText().toString());
Double no2= Double.parseDouble(ed2.getText().toString());
Double ans= no1/no2;
Toast.makeText(MainActivity.this,"Division is
23002311605
5
"+ans.toString(),Toast.LENGTH_LONG).show();
}
public void OnClickMax(View v) {
Double no1 = Double.parseDouble(ed1.getText().toString());
Double no2 = Double.parseDouble(ed2.getText().toString());
if (no1 > no2)
{
Toast.makeText(MainActivity.this, "Max no. is " + no1.toString(),
Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(MainActivity.this, "Max no. is " + no2.toString(),
Toast.LENGTH_LONG).show();
}
}
}
Output
23002311605
5
Practical -8
AIM:- Create sample application with login module. (Check username and
password) On successful login, display welcome message in next activity. And on
failing login, alert the user.
Theory:
A login application is the screen asking your credentials to login to some particular application.
You might have seen it when logging into facebook,twitter e.t.c
This chapter explains, how to create a login screen and how to manage security when false attempts
are made.
First you have to define two TextView asking username and password of the user. The password
TextView must have inputType set to password
In the java file, inside the method of onClick get the username and passwords text using getText()
and toString() method and match it with the text using equals() function.
Steps Description
1 You will use Android studio to create an Android application under a package
com.example.sairamkrishna.myapplication.
5 Run the application and choose a running android device and install the application on it and
verify the results
Code:
● activity_main.xml
<?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">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
23002311605
5
android:hint="Username"
android:id="@+id/uname"
android:layout_centerInParent="true"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:id="@+id/pass"
android:layout_below="@+id/uname"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/lgn"
android:layout_below="@+id/pass"
android:text="LOGIN"
android:onClick="Login"
android:layout_centerInParent="true"/>
</RelativeLayout>
● MainActivity.java
package com.example.login;
import android.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
● activity_main2.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login Successful"
android:textAlignment="center"
android:layout_centerInParent="true"
/>
</RelativeLayout>
● MainActivity2.java
package com.example.login;
import android.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity2 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
}
23002311605
5
Output
23002311605
5
Practical -9
AIM:- Create an application that uses the image button and clicking this button
changes the image of it and also handles the onLongClick event.
Theory:-
Events are a useful way to collect data about a user's interaction with interactive components of
Applications. Like button presses or screen touch etc. The Android framework maintains an event
queue as a first-in, first-out (FIFO) basis. You can capture these events in your program and take
appropriate action as per requirements.
OnClickListener()
This is called when the user either clicks or touches or focuses
onClick() upon any widget like button, text, image etc. You will use
onClick() event handler to handle such event.
OnLongClickListener()
This is called when the user either clicks or touches or focuses
upon any widget like button, text, image etc. for one or more
onLongClick() seconds. You will use onLongClick() event handler to handle
such event.
OnFocusChangeListener()
This is called when the widget looses its focus ie. user goes away
onFocusChange() from the view item. You will use onFocusChange() event handler
to handle such event.
23002311605
5
OnFocusChangeListener()
This is called when the user is focused on the item and presses or
onKey() releases a hardware key on the device. You will use onKey()
event handler to handle such event.
OnTouchListener()
This is called when the user presses the key, releases the key, or
onTouch() any movement gesture on the screen. You will use onTouch()
event handler to handle such event.
OnMenuItemClickListener()
This is called when the user selects a menu item. You will use
onMenuItemClick() onMenuItemClick() event handler to handle such event.
onCreateContextMenuItemListener()
This is called when the context menu is being built(as the result
onCreateContextMenu() of a sustained "long click)
Code:
● activity_main.xml
<?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">
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerHorizontal="true"
android:id="@+id/img1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/img1"
android:layout_centerHorizontal="true"
android:text="Change Image"
android:id="@+id/button"/>
</RelativeLayout>
● MainActivity.java
package com.example.image;
23002311605
5
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity
{ Button b1;
ImageView iv;
boolean flag;
int images[]={R.drawable.img1,R.drawable.img2,R.drawable.img3};
int i=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv=(ImageView) findViewById(R.id.img1);
b1=(Button) findViewById(R.id.button);
flag=true;
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
iv.setImageResource(images[i]);
i++;
if(i==3)
i=0;
}
});
}}
Output