Ex:No: 1
Date : DEVELOP AN APPLICATION THAT USES GUI COMPONENTS,
FONT AND COLOURS
AIM:
To develop an android application that uses GUI components ,font ,colours.
PROCEDURE:
Creating a New project:
Open Android Stdio and then click on File -> New -> New project.
Then type the Application name as “Exno1” 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 design the layout.
Designing layout for the Android Application:
Click on app -> res -> layout -> activity_main.xml.
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="[Link]
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:id="@+id/textView" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="30dp"
android:gravity="center"
android:text="Hello World!"
android:textSize="25sp"
android:textStyle="bold" />
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:gravity="center"
android:text="Change font size"
android:textSize="25sp" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:gravity="center"
android:text="Change color"
android:textSize="25sp" />
</LinearLayout>
Now click on Design and you can see your design.
So now the designing part is completed.
Java Coding for the Android Application:
Click on app -> java -> [Link].exno1 -> MainActivity.
Then delete the code which is there and type the code as given below
Code for [Link]:
package [Link].exno1;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
int ch=1;
float font=30;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
final TextView t=(TextView) findViewById([Link]);
Button b1=(Button) findViewById([Link].button1);
[Link](new [Link]() {
@Override
public void onClick(View v) {
[Link](font);
font = font+5;
if(font == 50)
font =30;
}
});
Button b2= (Button) findViewById([Link].button2);
[Link](new [Link]() {
@Override
public void onClick(View v) {
switch (ch) {
case 1:
[Link]([Link]);
break;
case 2:
[Link]([Link]);
break;
case 3:
[Link]([Link]);
break;
case 4:
[Link]([Link]);
break;
case 5:
[Link]([Link]);
break;
case 6:
[Link]([Link]);
break;
}
ch++;
if (ch == 7)
ch = 1;
}
});
}
}
OUTPUT:
RESULT:
Thus a Simple Android Application that uses GUI components, Font and Colors is
developed and executed successfully.
Ex:No: 2
Date : DEVELOP AN APPLICATION THAT USES LAYOUT MANAGERS
AND EVENT LISTENERS.
AIM:
To develop a android application that uses layout managers and event listeners.
PROCEDURE:
Creating a New project:
Open Android Stdio and then click on File -> New -> New project.
Then type the Application name as “Ex2” 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 design the layout.
Creating Second Activity for the Android Application:
Click on File -> New -> Activity -> Empty Activity.
Type the Activity Name as SecondActivity and click Finish button.
Thus Second Activity For the application is created.
Designing layout for the Android Application:
Click on app -> res -> layout -> activity_main.xml.
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"?>
<RelativeLayout xmlns:android="[Link]
xmlns:tools="[Link] android:layout_width="match_parent"
android:layout_height="match_parent" tools:context=".MainActivity">
<LinearLayout android:layout_width="match_parent" android:layout_height="100dp">
<TextView android:id="@+id/textView" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_margin="30dp"
android:text="Details
Form" android:textSize="25sp" android:gravity="center"/>
</LinearLayout>
<GridLayout android:id="@+id/gridLayout" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_marginTop="100dp"
android:layout_marginBottom="200dp" android:columnCount="2"
android:rowCount="3">
<TextView android:id="@+id/textView1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_margin="10dp"
android:layout_row="0" android:layout_column="0" android:text="Name"
android:textSize="20sp" android:gravity="center"/>
<EditText android:id="@+id/editText" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_margin="10dp"
android:layout_row="0" android:layout_column="1" android:ems="10"/>
<TextView android:id="@+id/textView2" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp" android:layout_row="1" android:layout_column="0"
android:text="[Link]" android:textSize="20sp" android:gravity="center"/>
<EditText android:id="@+id/editText2" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_margin="10dp"
android:layout_row="1" android:layout_column="1" android:inputType="number"
android:ems="10"/>
<TextView android:id="@+id/textView3" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_margin="10dp"
android:layout_row="2" android:layout_column="0" android:text="Dept"
android:textSize="20sp" android:gravity="center"/>
<Spinner android:id="@+id/spinner" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_margin="10dp"
android:layout_row="2"
android:layout_column="1" android:spinnerMode="dropdown"/>
</GridLayout>
<Button android:id="@+id/button" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_alignParentBottom="true"
android:layout_centerInParent="true" android:layout_marginBottom="150dp"
android:text="Submit"/>
</RelativeLayout>
Now click on Design and your activity will look as given below.
So now the designing part of Main Activity is completed.
Designing Layout for Second Activity:
Click on app -> res -> layout -> activity_main2.xml.
Now click on Text and then delete the code which is there and type the code as
given below.
Code for Activity_main2.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="[Link].ex2.Main2Activity"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="New Text"
android:textSize="30sp"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="New Text"
android:textSize="30sp"/>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="New Text"
android:textSize="30sp"/>
</LinearLayout>
Now click on Design and your activity will look as given below.
So now the designing part of Second Activity is also completed.
Java Coding for the Android Application:
Java Coding for MainActivity:
Click on app -> java -> [Link].ex2 -> MainActivity.
Then delete the code which is there and type the code as given below.
Code for [Link]:
package [Link].ex2;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
//Defining the Views
EditText e1,e2;
Button bt;
Spinner s;
//Data for populating in Spinner
String [] dept_array={"CSE","ECE","IT","Mech","Civil"};
String name,reg,dept;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
//Referring the Views
e1= (EditText) findViewById([Link]);
e2= (EditText) findViewById([Link].editText2);
bt= (Button) findViewById([Link]);
s= (Spinner) findViewById([Link]);
//Creating Adapter for Spinner for adapting the data from array to Spinner
ArrayAdapter adapter= new
ArrayAdapter([Link],[Link].simple_spinner_item,dept_array);
[Link](adapter);
//Creating Listener for Button
[Link](new [Link]() {
@Override
public void onClick(View v) {
//Getting the Values from Views(Edittext & Spinner)
name=[Link]().toString();
reg=[Link]().toString();
dept=[Link]().toString();
//Intent For Navigating to Second Activity
Intent i = new Intent([Link],[Link]);
//For Passing the Values to Second Activity
[Link]("name_key", name);
[Link]("reg_key",reg);
[Link]("dept_key", dept);
startActivity(i);
}
});
}
}
Java Coding for Main2Activity:
Click on app -> java -> [Link].ex2 -> Main2Activity.
Then delete the code which is there and type the code as given below.
Code for [Link]:
package [Link].ex2;
import [Link];
import [Link];
import [Link];
import [Link];
public class Main2Activity extends AppCompatActivity {
TextView t1,t2,t3;
String name,reg,dept;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main2);
t1= (TextView) findViewById([Link].textView1);
t2= (TextView) findViewById([Link].textView2);
t3= (TextView) findViewById([Link].textView3);
//Getting the Intent
Intent i = getIntent();
//Getting the Values from First Activity using the Intent received
name=[Link]("name_key");
reg=[Link]("reg_key");
dept=[Link]("dept_key");
//Setting the Values to Intent
[Link](name);
[Link](reg);
[Link](dept);
}
}
So now the Coding part of Second Activity is also completed.
Now run the application to see the output.
OUTPUT:
RESULT:
Thus a Simple Android Application that uses Layout Managers and Event
Listeners is developed and executed successfully.
Ex:No: 3
Date : WRITE AN APPLICATION THAT DRAWS BASIC GRAPHICAL
PRIMITIVES ON THE SCREEN.
AIM:
To develop a android application that draws basic graphical primitives on the screen.
PROCEDURE:
Creating a New project:
Open Android Stdio and then click on File -> New -> New project.
Then type the Application name as “Ex4” 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 design the layout.
Designing layout for the Android Application:
Click on app -> res -> layout -> activity_main.xml.
Then delete the code which is there and type the code as given below.
Code for [Link]:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView" />
</RelativeLayout>
Now click on Design and you can see your design.
So now the designing part is completed.
Java Coding for the Android Application:
Click on app -> java -> [Link].ex5 -> MainActivity.
Then delete the code which is there and type the code as given below
Code for [Link]:
package [Link].ex4;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
//Creating a Bitmap
Bitmap bg = [Link](720, 1280, [Link].ARGB_8888);
//Setting the Bitmap as background for the ImageView
ImageView i = (ImageView) findViewById([Link]);
[Link](new BitmapDrawable(bg));
//Creating the Canvas Object
Canvas canvas = new Canvas(bg);
//Creating the Paint Object and set its color & TextSize
Paint paint = new Paint();
[Link]([Link]);
[Link](50);
//To draw a Rectangle
[Link]("Rectangle", 420, 150, paint);
[Link](400, 200, 650, 700, paint);
//To draw a Circle
[Link]("Circle", 120, 150, paint);
[Link](200, 350, 150, paint);
//To draw a Square
[Link]("Square", 120, 800, paint);
[Link](50, 850, 350, 1150, paint);
//To draw a Line
[Link]("Line", 480, 800, paint);
[Link](520, 850, 520, 1150, paint);
}
}
So now the Coding part is also completed.
Now run the application to see the output.
OUTPUT:
RESULT:
Thus a Simple Android Application that draws basic Graphical Primitives on the
screen is developed and executed successfully.
Ex:No: 4
Date : DEVELOP AN APPLICATION THAT MAKES USE OF
DATABASES.
AIM:
To develop a android application that make use of databases.
PROCEDURE:
Creating a New project:
Open Android Stdio and then click on File -> New -> New project.
Then type the Application name as “Ex5” 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 design the layout.
Designing layout for the Android Application:
Click on app -> res -> layout -> activity_main.xml.
Then delete the code which is there and type the code as given below.
Code for [Link]:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="50dp"
android:layout_y="20dp"
android:text="Student Details"
android:textSize="30sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="20dp"
android:layout_y="110dp"
android:text="Enter Rollno:"
android:textSize="20sp" />
<EditText
android:id="@+id/Rollno"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_x="175dp"
android:layout_y="100dp"
android:inputType="number"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="20dp"
android:layout_y="160dp"
android:text="Enter Name:"
android:textSize="20sp" />
<EditText
android:id="@+id/Name"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_x="175dp"
android:layout_y="150dp"
android:inputType="text"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="20dp"
android:layout_y="210dp"
android:text="Enter Marks:"
android:textSize="20sp" />
<EditText
android:id="@+id/Marks"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_x="175dp"
android:layout_y="200dp"
android:inputType="number"
android:textSize="20sp" />
<Button
android:id="@+id/Insert"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_x="25dp"
android:layout_y="300dp"
android:text="Insert"
android:textSize="30dp" />
<Button
android:id="@+id/Delete"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_x="200dp"
android:layout_y="300dp"
android:text="Delete"
android:textSize="30dp" />
<Button
android:id="@+id/Update"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_x="25dp"
android:layout_y="400dp"
android:text="Update"
android:textSize="30dp" />
<Button
android:id="@+id/View"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_x="200dp"
android:layout_y="400dp"
android:text="View"
android:textSize="30dp" />
<Button
android:id="@+id/ViewAll"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_x="100dp"
android:layout_y="500dp"
android:text="View All"
android:textSize="30dp" />
</AbsoluteLayout>
Now click on Design and you can see your design.
So now the designing part is completed.
Java Coding for the Android Application:
Click on app -> java -> [Link].ex5 -> MainActivity.
Then delete the code which is there and type the code as given below
Code for [Link]:
package [Link].ex5;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends Activity implements OnClickListener
{
EditText Rollno,Name,Marks;
Button Insert,Delete,Update,View,ViewAll;
SQLiteDatabase db;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
[Link](savedInstanceState);
setContentView([Link].activity_main);
Rollno=(EditText)findViewById([Link]);
Name=(EditText)findViewById([Link]);
Marks=(EditText)findViewById([Link]);
Insert=(Button)findViewById([Link]);
Delete=(Button)findViewById([Link]);
Update=(Button)findViewById([Link]);
View=(Button)findViewById([Link]);
ViewAll=(Button)findViewById([Link]);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
// Creating database and table
db=openOrCreateDatabase("StudentDB", Context.MODE_PRIVATE, null);
[Link]("CREATE TABLE IF NOT EXISTS student(rollno VARCHAR,name
VARCHAR,marks VARCHAR);");
}
public void onClick(View view)
{
// Inserting a record to the Student table
if(view==Insert)
{
// Checking for empty fields
if([Link]().toString().trim().length()==0||
[Link]().toString().trim().length()==0||
[Link]().toString().trim().length()==0)
{
showMessage("Error", "Please enter all values");
return;
}
[Link]("INSERT INTO student VALUES('"+[Link]()
+"','"+[Link]()+
"','"+[Link]()+"');");
showMessage("Success", "Record added");
clearText();
}
// Deleting a record from the Student table
if(view==Delete)
{
// Checking for empty roll number
if([Link]().toString().trim().length()==0)
{
showMessage("Error", "Please enter Rollno");
return;
}
Cursor c=[Link]("SELECT * FROM student WHERE
rollno='"+[Link]()+"'", null);
if([Link]())
{
[Link]("DELETE FROM student WHERE rollno='"+[Link]()+"'");
showMessage("Success", "Record Deleted");
}
else
{
showMessage("Error", "Invalid Rollno");
}
clearText();
}
// Updating a record in the Student table
if(view==Update)
{
// Checking for empty roll number
if([Link]().toString().trim().length()==0)
{
showMessage("Error", "Please enter Rollno");
return;
}
Cursor c=[Link]("SELECT * FROM student WHERE
rollno='"+[Link]()+"'", null);
if([Link]()) {
[Link]("UPDATE student SET name='" + [Link]() +
"',marks='" + [Link]() +
"' WHERE rollno='"+[Link]()+"'");
showMessage("Success", "Record Modified");
}
else {
showMessage("Error", "Invalid Rollno");
}
clearText();
}
// Display a record from the Student table
if(view==View)
{
// Checking for empty roll number
if([Link]().toString().trim().length()==0)
{
showMessage("Error", "Please enter Rollno");
return;
}
Cursor c=[Link]("SELECT * FROM student WHERE
rollno='"+[Link]()+"'", null);
if([Link]())
{
[Link]([Link](1));
[Link]([Link](2));
}
else
{
showMessage("Error", "Invalid Rollno");
clearText();
}
}
// Displaying all the records
if(view==ViewAll)
{
Cursor c=[Link]("SELECT * FROM student", null);
if([Link]()==0)
{
showMessage("Error", "No records found");
return;
}
StringBuffer buffer=new StringBuffer();
while([Link]())
{
[Link]("Rollno: "+[Link](0)+"\n");
[Link]("Name: "+[Link](1)+"\n");
[Link]("Marks: "+[Link](2)+"\n\n");
}
showMessage("Student Details", [Link]());
}
}
public void showMessage(String title,String message)
{
Builder builder=new Builder(this);
[Link](true);
[Link](title);
[Link](message);
[Link]();
}
public void clearText()
{
[Link]("");
[Link]("");
[Link]("");
[Link]();
}
}
So now the Coding part is also completed.
Now run the application to see the output.
OUTPUT:
RESULT:
Thus a Simple Android Application that makes use of Database is developed and
executed successfully.
Ex:No: 5
Date : DEVELOP AN APPLICATION THAT MAKES USE OF
NOTIFICATION MANAGER
AIM:
To develop a android application that make use of notification manager.
PROCEDURE:
Creating a New project:
Open Android Stdio and then click on File -> New -> New project.
Then type the Application name as “Ex10” 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 design the layout.
Creating Second Activity for the Android Application:
Click on File -> New -> Activity -> Empty Activity.
Type the Activity Name as SecondActivity and click Finish button.
Thus Second Activity For the application is created.
Designing layout for the Android Application:
Click on app -> res -> layout -> activity_main.xml.
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="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Message"
android:textSize="30sp" />
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:textSize="30sp" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="30dp"
android:layout_gravity="center"
android:text="Notify"
android:textSize="30sp"/>
</LinearLayout>
Now click on Design and you can see your design.
So now the designing part is completed.
Java Coding for the Android Application:
Click on app -> java -> [Link].ex10 -> MainActivity.
Then delete the code which is there and type the code as given below.
Code for [Link]:
package [Link].ex10;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity
{
Button notify;
EditText e;
@Override
protected void onCreate(Bundle savedInstanceState)
{
[Link](savedInstanceState);
setContentView([Link].activity_main);
notify= (Button) findViewById([Link]);
e= (EditText) findViewById([Link]);
[Link](new [Link]()
{
@Override
public void onClick(View v)
{
Intent intent = new Intent([Link],[Link]);
PendingIntent pending = [Link]([Link], 0, intent, 0);
Notification noti = new
[Link]([Link]).setContentTitle("New
Message").setContentText([Link]().toString()).setSmallIcon([Link].ic_launcher).setC
ontentIntent(pending).build();
NotificationManager manager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
[Link] |= Notification.FLAG_AUTO_CANCEL;
[Link](0, noti);
}
});
}
}
So now the Coding part is also completed.
Now run the application to see the output.
OUTPUT:
RESULT:
Thus Android Application that creates an alert upon receiving a message is developed
and executed successfully.
Ex:No: 6
Date : IMPLEMENT AN APPLICATION THAT USES MULTI-THREADING
AIM:
To develop a android application that uses multithreading.
PROCEDURE:
Creating a New project:
Open Android Stdio and then click on File -> New -> New project.
Then type the Application name as “Multithreading_app” 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 design the layout.
Designing layout for the Android Application:
Click on app -> res -> layout -> activity_main.xml.
Then delete the code which is there and type the code as given below.
Code for [Link]:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
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 you can see your design.
So now the designing part is completed.
Java Coding for the Android Application:
Click on app -> java -> [Link].multithreading_app -> MainActivity.
Then delete the code which is there and type the code as given below
Code for [Link]:
package [Link].multithreading_app;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity
{
ImageView img;
Button bt1,bt2;
@Override
protected void onCreate(Bundle savedInstanceState)
{
[Link](savedInstanceState);
setContentView([Link].activity_main);
bt1 = (Button)findViewById([Link]);
bt2= (Button) findViewById([Link].button2);
img = (ImageView)findViewById([Link]);
[Link](new [Link]()
{
@Override
public void onClick(View v)
{
new Thread(new Runnable()
{
@Override
public void run()
{
[Link](new Runnable()
{
@Override
public void run()
{
[Link]([Link].india1);
}
});
}
}).start();
}
});
[Link](new [Link]()
{
@Override
public void onClick(View v)
{
new Thread(new Runnable()
{
@Override
public void run()
{
[Link](new Runnable()
{
@Override
public void run()
{
[Link]([Link].india2);
}
});
}
}).start();
}
});
}
}
So now the Coding part is also completed.
Before Running the Application, Paste the Images used for this program in
this path “app -> res -> drawable”.
Now run the application to see the output.
OUTPUT:
RESULT:
Thus Android Application that implements Multi threading is developed and
executed successfully.
Ex:No: 7
Date : DEVELOP A NATIVE APPLICATION THAT USES GPS LOCATION
INFORMATION
AIM:
To develop a native application that uses GPS location information.
PROCEDURE:
Creating a New project:
Open Android Stdio and then click on File -> New -> New project.
Then type the Application name as “GPS_app” 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 design the layout.
Designing layout for the Android Application:
Click on app -> res -> layout -> activity_main.xml.
Then delete the code which is there and type the code as given below.
Code for [Link]:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[Link]
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Location"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
Now click on Design and you can see your design.
So now the designing part is completed.
Adding permissions in Manifest for the Android Application:
Click on app -> manifests -> [Link].
Now include the ACCESS_COARSE_LOCATION and
ACCESS_FINE_LOCATION permissions in the [Link] file.
Code for [Link]:
<?xml version = "1.0" encoding = "utf-8"?>
<manifest xmlns:android = "[Link]
package = "[Link].gps_app">
<uses-permission
android:name="[Link].ACCESS_COARSE_LOCATION" />
<uses-permission android:name="[Link].ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="[Link]" />
<category android:name="[Link]" />
</intent-filter>
</activity>
</application>
</manifest>
So now the Permissions are added in the Manifest.
Java Coding for the Android Application:
Click on app -> java -> [Link].gps_app -> MainActivity.
Then delete the code which is there and type the code as given below
Code for [Link]:
package [Link].gps_app;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import static [Link].ACCESS_COARSE_LOCATION;
import static [Link].ACCESS_FINE_LOCATION;
public class MainActivity extends AppCompatActivity {
private ArrayList<String> permissionsToRequest;
private ArrayList<String> permissionsRejected = new ArrayList<>();
private ArrayList<String> permissions = new ArrayList<>();
private final static int ALL_PERMISSIONS_RESULT = 101;
LocationTrack locationTrack;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
[Link](ACCESS_FINE_LOCATION);
[Link](ACCESS_COARSE_LOCATION);
permissionsToRequest = findUnAskedPermissions(permissions);
//get the permissions we have asked for before but are not granted..
//we will store this in a global list to access later.
if ([Link].SDK_INT >= Build.VERSION_CODES.M) {
if ([Link]() > 0)
requestPermissions([Link](new
String[[Link]()]), ALL_PERMISSIONS_RESULT);
}
Button btn = (Button) findViewById([Link]);
[Link](new [Link]() {
@Override
public void onClick(View view) {
locationTrack = new LocationTrack([Link]);
if ([Link]()) {
double longitude = [Link]();
double latitude = [Link]();
[Link](getApplicationContext(), "Longitude:" +
[Link](longitude) + "\nLatitude:" + [Link](latitude),
Toast.LENGTH_SHORT).show();
} else {
[Link]();
}
}
});
private ArrayList<String> findUnAskedPermissions(ArrayList<String> wanted) {
ArrayList<String> result = new ArrayList<String>();
for (String perm : wanted) {
if (!hasPermission(perm)) {
[Link](perm);
}
}
return result;
}
private boolean hasPermission(String permission) {
if (canMakeSmores()) {
if ([Link].SDK_INT >= Build.VERSION_CODES.M) {
return (checkSelfPermission(permission) ==
PackageManager.PERMISSION_GRANTED);
}
}
return true;
}
private boolean canMakeSmores() {
return ([Link].SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1);
}
@TargetApi(Build.VERSION_CODES.M)
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[]
grantResults) {
switch (requestCode) {
case ALL_PERMISSIONS_RESULT:
for (String perms : permissionsToRequest) {
if (!hasPermission(perms)) {
[Link](perms);
}
}
if ([Link]() > 0) {
if ([Link].SDK_INT >= Build.VERSION_CODES.M) {
if (shouldShowRequestPermissionRationale([Link](0))) {
showMessageOKCancel("These permissions are mandatory for the
application. Please allow access.",
new [Link]() {
@Override
public void onClick(DialogInterface dialog, int which) {
if ([Link].SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions([Link](new
String[[Link]()]), ALL_PERMISSIONS_RESULT);
}
}
});
return;
}
}
break;
}
private void showMessageOKCancel(String message, [Link]
okListener) {
new [Link]([Link])
.setMessage(message)
.setPositiveButton("OK", okListener)
.setNegativeButton("Cancel", null)
.create()
.show();
}
@Override
protected void onDestroy() {
[Link]();
[Link]();
}
}
Now create a new java class file named “[Link]”
Code for [Link]:
package [Link].gps_app;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class LocationTrack extends Service implements LocationListener {
private final Context mContext;
boolean checkGPS = false;
boolean checkNetwork = false;
boolean canGetLocation = false;
Location loc;
double latitude;
double longitude;
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1;
protected LocationManager locationManager;
public LocationTrack(Context mContext) {
[Link] = mContext;
getLocation();
}
private Location getLocation() {
try {
locationManager = (LocationManager) mContext
.getSystemService(LOCATION_SERVICE);
// get GPS status
checkGPS = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// get network provider status
checkNetwork = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!checkGPS && !checkNetwork) {
[Link](mContext, "No Service Provider is available",
Toast.LENGTH_SHORT).show();
} else {
[Link] = true;
// if GPS Enabled get lat/long using GPS Services
if (checkGPS) {
if ([Link](mContext,
[Link].ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED &&
[Link](mContext,
[Link].ACCESS_COARSE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[]
permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the
documentation
// for ActivityCompat#requestPermissions for more details.
}
[Link](
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
if (locationManager != null) {
loc = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (loc != null) {
latitude = [Link]();
longitude = [Link]();
}
}
/*if (checkNetwork) {
if ([Link](mContext,
[Link].ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED &&
[Link](mContext,
[Link].ACCESS_COARSE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[]
permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the
documentation
// for ActivityCompat#requestPermissions for more details.
}
[Link](
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
if (locationManager != null) {
loc = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (loc != null) {
latitude = [Link]();
longitude = [Link]();
}
}*/
} catch (Exception e) {
[Link]();
}
return loc;
}
public double getLongitude() {
if (loc != null) {
longitude = [Link]();
}
return longitude;
}
public double getLatitude() {
if (loc != null) {
latitude = [Link]();
}
return latitude;
}
public boolean canGetLocation() {
return [Link];
}
public void showSettingsAlert() {
[Link] alertDialog = new [Link](mContext);
[Link]("GPS is not Enabled!");
[Link]("Do you want to turn on GPS?");
[Link]("Yes", new [Link]() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
[Link](intent);
}
});
[Link]("No", new [Link]() {
public void onClick(DialogInterface dialog, int which) {
[Link]();
}
});
[Link]();
}
public void stopListener() {
if (locationManager != null) {
if ([Link](mContext,
[Link].ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED &&
[Link](mContext,
[Link].ACCESS_COARSE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
[Link]([Link]);
}
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onLocationChanged(Location location) {
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
@Override
public void onProviderEnabled(String s) {
@Override
public void onProviderDisabled(String s) {
}
}
So now the Coding part is also completed.
Now run the application to see the output.
OUTPUT:
RESULT:
Thus a native application that uses GPS location information is developed and
executed successfully.
Ex:No: 8
Date : IMPLEMENT AN APPLICATION THAT WRITES DATA TO THE SD
CARD
AIM:
To develop an simple android mobile application that writes data to the sd card.
PROCEDURE:
Creating a New project:
Open Android Stdio and then click on File -> New -> New project.
Then type the Application name as “Ex9” 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 design the layout.
Designing layout for the Android Application:
Click on app -> res -> layout -> activity_main.xml.
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="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:orientation="vertical">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:textSize="30dp" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Write Data"
android:textSize="30dp" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Read Data"
android:textSize="30dp" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Clear"
android:textSize="30dp" />
</LinearLayout>
Now click on Design and you can see your design.
So now the designing part is completed.
Adding permissions in Manifest for the Android Application:
Click on app -> manifests -> [Link]
Now include the WRITE_EXTERNAL_STORAGE AND
READ_EXTERNAL_STORAGE permissions in the [Link] file.
Code for [Link]:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="[Link]
package="[Link].ex9">
<uses-permission
android:name="[Link].WRITE_EXTERNAL_STORAGE"/>
<uses-permission
android:name="[Link].READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="[Link]" />
<category android:name="[Link]" />
</intent-filter>
</activity>
</application>
</manifest>
So now the Permissions are added in the Manifest.
Java Coding for the Android Application:
Click on app -> java -> [Link].ex9 -> MainActivity.
Then delete the code which is there and type the code as given below
Code for [Link]:
package [Link].ex9;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity
{
EditText e1;
Button write,read,clear;
@Override
protected void onCreate(Bundle savedInstanceState)
{
[Link](savedInstanceState);
setContentView([Link].activity_main);
e1= (EditText) findViewById([Link]);
write= (Button) findViewById([Link]);
read= (Button) findViewById([Link].button2);
clear= (Button) findViewById([Link].button3);
[Link](new [Link]()
{
@Override
public void onClick(View v)
{
String message=[Link]().toString();
try
{
File f=new File("/sdcard/[Link]");
[Link]();
FileOutputStream fout=new FileOutputStream(f);
[Link]([Link]());
[Link]();
[Link](getBaseContext(),"Data Written in
SDCARD",Toast.LENGTH_LONG).show();
}
catch (Exception e)
{
[Link](getBaseContext(),[Link](),Toast.LENGTH_LONG).show();
}
}
});
[Link](new [Link]()
{
@Override
public void onClick(View v)
{
String message;
String buf = "";
try
{
File f = new File("/sdcard/[Link]");
FileInputStream fin = new FileInputStream(f);
BufferedReader br = new BufferedReader(new InputStreamReader(fin));
while ((message = [Link]()) != null)
{
buf += message;
}
[Link](buf);
[Link]();
[Link]();
[Link](getBaseContext(),"Data Received from
SDCARD",Toast.LENGTH_LONG).show();
}
catch (Exception e)
{
[Link](getBaseContext(), [Link](),
Toast.LENGTH_LONG).show();
}
}
});
[Link](new [Link]()
{
@Override
public void onClick(View v)
{
[Link]("");
}
});
}
}
So now the Coding part is also completed.
Now run the application to see the output.
OUTPUT:
RESULT:
Thus Android Application that writes data to the SD Card is developed and executed
successfully.
Ex:No: 9
Date : IMPLEMENT AN APPLICATION THAT CREATES AN ALERT
UPON RECEIVING A MESSAGE
AIM:
To implement an application that creates an alert upon receiving a message.
PROCEDURE:
Creating a New project:
Open Android Stdio and then click on File -> New -> New project.
Then type the Application name as “Ex10” 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 create a second activity and design the layout.
Creating Second Activity for the Android Application:
Click on File -> New -> Activity -> Empty Activity.
Type the Activity Name as SecondActivity and click Finish button.
Thus Second Activity For the application is created.
Designing layout for the Android Application:
Click on app -> res -> layout -> activity_main.xml.
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="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Message"
android:textSize="30sp" />
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:textSize="30sp" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="30dp"
android:layout_gravity="center"
android:text="Notify"
android:textSize="30sp"/>
</LinearLayout>
Now click on Design and you can see your design.
So now the designing part is completed.
Java Coding for the Android Application:
Click on app -> java -> [Link].ex10 -> MainActivity.
Then delete the code which is there and type the code as given below.
Code for [Link]:
package [Link].ex10;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity
{
Button notify;
EditText e;
@Override
protected void onCreate(Bundle savedInstanceState)
{
[Link](savedInstanceState);
setContentView([Link].activity_main);
notify= (Button) findViewById([Link]);
e= (EditText) findViewById([Link]);
[Link](new [Link]()
{
@Override
public void onClick(View v)
{
Intent intent = new Intent([Link],[Link]);
PendingIntent pending = [Link]([Link], 0, intent, 0);
Notification noti = new
[Link]([Link]).setContentTitle("New
Message").setContentText([Link]().toString()).setSmallIcon([Link].ic_launcher).setC
ontentIntent(pending).build();
NotificationManager manager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
[Link] |= Notification.FLAG_AUTO_CANCEL;
[Link](0, noti);
}
});
}
}
So now the Coding part is also completed.
Now run the application to see the output.
OUTPUT:
RESULT:
Thus Android Application that creates an alert upon receiving a message is developed
and executed successfully.
Ex:No: 10
Date : ANDROID APPLICATION THAT MAKES USE OF RSS FEED
AIM:
To develop a mobile application that makes use of RSS feed
PROCEDURE:
Creating a New project:
Open Android Stdio and then click on File -> New -> New project.
Then type the Application name as “ex6” 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 design the layout.
Designing layout for the Android Application:
Click on app -> res -> layout -> activity_main.xml.
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="[Link]
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Now click on Design and you can see your design.
So now the designing part is completed.
Adding permissions in Manifest for the Android Application:
Click on app -> manifests -> [Link]
Now include the INTERNET permissions in the [Link] file.
Code for [Link]:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="[Link]
package="[Link].ex6" >
<uses-permission android:name="[Link]"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="[Link]" />
<category android:name="[Link]" />
</intent-filter>
</activity>
</application>
</manifest>
So now the Permissions are added in the Manifest.
Java Coding for the Android Application:
Click on app -> java -> [Link].ex6 -> MainActivity.
Then delete the code which is there and type the code as given below.
Code for [Link]:
package [Link].ex6;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends ListActivity
{
List headlines;
List links;
@Override
protected void onCreate(Bundle savedInstanceState)
{
[Link](savedInstanceState);
new MyAsyncTask().execute();
}
class MyAsyncTask extends AsyncTask<Object,Void,ArrayAdapter>
{
@Override
protected ArrayAdapter doInBackground(Object[] params)
{
headlines = new ArrayList();
links = new ArrayList();
try
{
URL url = new URL("[Link]
XmlPullParserFactory factory = [Link]();
[Link](false);
XmlPullParser xpp = [Link]();
// We will get the XML from an input stream
[Link](getInputStream(url), "UTF_8");
boolean insideItem = false;
// Returns the type of current event: START_TAG, END_TAG, etc..
int eventType = [Link]();
while (eventType != XmlPullParser.END_DOCUMENT)
{
if (eventType == XmlPullParser.START_TAG)
{
if ([Link]().equalsIgnoreCase("item"))
{
insideItem = true;
}
else if ([Link]().equalsIgnoreCase("title"))
{
if (insideItem)
[Link]([Link]()); //extract the headline
}
else if ([Link]().equalsIgnoreCase("link"))
{
if (insideItem)
[Link]([Link]()); //extract the link of article
}
}
else if(eventType==XmlPullParser.END_TAG &&
[Link]().equalsIgnoreCase("item"))
{
insideItem=false;
}
eventType = [Link](); //move to next element
}
}
catch (MalformedURLException e)
{
[Link]();
}
catch (XmlPullParserException e)
{
[Link]();
}
catch (IOException e)
{
[Link]();
}
return null;
}
protected void onPostExecute(ArrayAdapter adapter)
{
adapter = new ArrayAdapter([Link], [Link].simple_list_item_1,
headlines);
setListAdapter(adapter);
}
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
Uri uri = [Link](([Link](position)).toString());
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
public InputStream getInputStream(URL url)
{
try
{
return [Link]().getInputStream();
}
catch (IOException e)
{
return null;
}
}
}
So now the Coding part is also completed.
Now run the application to see the output.
OUTPUT:
RESULT:
Thus Android Application that makes use of RSS Feed is developed and executed
successfully.
Ex:No: 11
Date : ANDROID APPLICATION TO SEND AN EMAIL
AIM:
To develop an simple android mobile application to send an email.
PROCEDURE:
Creating a New project:
Open Android Stdio and then click on File -> New -> New project.
Then type the Application name as “Email” 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 design the layout.
Designing layout for the Android Application:
Click on app -> res -> layout -> activity_main.xml.
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"?>
<!--Relative Layout-->
<RelativeLayout
xmlns:android="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!--Edit text for email id-->
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_marginTop="18dp"
android:layout_marginRight="22dp" />
<!--Edit text for email subject-->
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_alignLeft="@+id/editText1"
android:layout_marginTop="20dp" />
<!--Edit text for email body-->
<!--text Views for label-->
<EditText
android:id="@+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText2"
android:layout_alignLeft="@+id/editText2"
android:layout_marginLeft="-10dp"
android:layout_marginTop="83dp" />
<TextView
android:id="@+id/textView1"
android:textColor="#0F9D58"
android:text="Send To:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText1"
android:layout_alignBottom="@+id/editText1"
android:layout_alignParentLeft="true"
/>
<TextView
android:id="@+id/textView2"
android:textColor="#0F9D58"
android:text="Email Subject:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText2"
android:layout_alignBottom="@+id/editText2"
android:layout_alignParentLeft="true"
/>
<TextView
android:id="@+id/textView3"
android:textColor="#0F9D58"
android:text="Email Body:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText3"
android:layout_alignBottom="@+id/editText3"
/>
<!--Button to send email-->
<Button
android:id="@+id/button"
android:text="Send email!!"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText3"
android:layout_below="@+id/editText3"
android:layout_marginLeft="76dp"
android:layout_marginTop="20dp"
/>
</RelativeLayout>
Now click on Design and you can see your design.
So now the designing part is completed.
Java Coding for the Android Application:
Click on app -> java -> [Link] -> MainActivity.
Then delete the code which is there and type the code as given below.
Code for [Link]:
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
// define objects for edit text and button
Button button;
EditText sendto, subject, body;
@Override
protected void onCreate(Bundle savedInstanceState)
{
[Link](savedInstanceState);
setContentView([Link].activity_main);
// Getting instance of edittext and button
sendto = findViewById([Link].editText1);
subject = findViewById([Link].editText2);
body = findViewById([Link].editText3);
button = findViewById([Link]);
// attach setOnClickListener to button
// with Intent object define in it
[Link](new OnClickListener() {
@Override
public void onClick(View view)
{
String emailsend = [Link]().toString();
String emailsubject = [Link]().toString();
String emailbody = [Link]().toString();
// define Intent object
// with action attribute as ACTION_SEND
Intent intent = new Intent(Intent.ACTION_SEND);
// add three fiels to intent using putExtra function
[Link](Intent.EXTRA_EMAIL,
new String[] { emailsend });
[Link](Intent.EXTRA_SUBJECT, emailsubject);
[Link](Intent.EXTRA_TEXT, emailbody);
// set type of intent
[Link]("message/rfc822");
// startActivity with intent with chooser
// as Email client using createChooser function
startActivity(
Intent
.createChooser(intent,
"Choose an Email client :"));
}
});
}
}
So now the Coding part is also completed.
Now run the application to see the output.
OUTPUT:
RESULT:
Thus a Simple Android Application to send an email is developed and executed
successfully.