Professional Documents
Culture Documents
BONAFIDE CERTIFICATE
Certified that this is the bonafide record of the practical work done by ………...
CONTENTS
Ex.No.1 Date:
DEVELOP AN APPLICATION THAT USES GUI COMPONENTS, FONT AND COLOURS
Aim:
To develop an android application to change the font size and color of text view.
Algorithm:
1. Create a mobile application using Android studio
2. Design the prototype of the application
3. In source code tab, modify the code as the application logic.
4. To change the Font colour and size, change the font property of the text view in runtime as
desired font colour and size.
5. Save and test the application.
Procedure:
1. Open Android Studio and then click on File -> New -> New project.
2. Then type the Application name as “Font App″ and click Next.
3. Then select the Minimum SDK as shown below and click Next.
4. Then select the Empty Activity and click Next.
5. Finally click Finish.
6. It will take some time to build and load the project.
7. Click on app -> res -> layout -> activity_main.xml.
8. Now click on Next as shown below.
9. Then delete the code which is there and type the code as given (Activity_main.xml).
10. Now click on Design and your application will look as given below.
11. So now the designing part is completed.
12. Click on app -> java -> com.example.fontapp -> MainActivity.
13. Then delete the code which is there and type the code as given below.
14. Now run the application to see the output.
Source Code:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.user.font " >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20sp"
android:gravity="center"
android:text="HELLO WORLD"
android:textSize="20sp"
android:textStyle="bold" />
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" android:text="Change font size"
android:textSize="20sp" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Change color"
android:textSize="20sp" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Change font"
android:textSize="20sp" />
</LinearLayout>
MainActivity.java
package com.example.user.font;
import android.R.layout;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
float font =24;
int i=1;
Typeface tf;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView t1=(TextView) findViewById(R.id.textView1);
Button b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() { public void onClick(View view) {
t1.setTextSize(font);
font=font+4;
if(font==40)
font=20;
}
});
Button b2 = (Button) findViewById(R.id.button2);
b2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) { switch(i)
{
case 1: t1.setTextColor(Color.parseColor("#0000FF")); break;
case 2: t1.setTextColor(Color.parseColor("#00FF00")); break;
case 3: t1.setTextColor(Color.parseColor("#FF0000")); break;
case 4: t1.setTextColor(Color.parseColor("#800000")); break;
}
i++;
if(i==5)
i=1;
} });
Button b3 = (Button) findViewById(R.id.button3);
b3.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
switch (i) {
case 1:
tf = Typeface.create("scans-serif",1);
t1.setTypeface(tf);
break;
case 2:
tf = Typeface.create("serif",2);
t1.setTypeface(tf);
break;
case 3:
tf = Typeface.create("casual",1);
t1.setTypeface(tf);
break;
case 4:
tf = Typeface.create("cursive",2);
t1.setTypeface(tf);
break;
}
i++;
if (i == 5)
i = 1;
}
});
}}
Output:
RESULT:
Ex.No.2 Date:
DEVELOP AN APPLICATION THAT USES LAYOUT MANAGERS AND EVENT
LISTENERS.
Aim:
To develop an android application that uses layout managers.
Algorithm:
1. Create a mobile application using Android studio
2. Design the prototype of the application
3. In source code tab, modify the code as the application logic.
4. Layout defines the structure for a user interface such as activity. All elements in layout are
built using a hierarchy of view and view group.
5. Types of layout: Linear Layout, Relative Layout, Table Layout, Absolute Layout, Frame
layout, List View and Grid View.
6. Select the appropriate layout types and design a simple arithmetic event.
7. Display the result as a Toast message.
8. Save and test the application.
Procedure:
1. Open Android Studio and then click on File -> New -> New project.
2. Then type the Application name as “Layout Manager″ and click Next.
3. Then select the Minimum SDK as shown below and click Next.
4. Then select the Empty Activity and click Next.
5. Finally click Finish.
6. It will take some time to build and load the project.
7. Click on app -> res -> layout -> activity_main.xml.
8. Now click on Next as shown below.
9. Then delete the code which is there and type the code as given (Activity_main.xml).
10. Now click on Design and your application will look as given below.
11. So now the designing part is completed.
12. Click on app -> java -> com.example.layout -> MainActivity.
13. Then delete the code which is there and type the code as given below.
14. Now run the application to see the output.
Source Code
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="ADDITION"
android:textSize="20dp" >
</TextView>
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/linearLayout1" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ENTER NO 1" >
</TextView>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.20"
android:id="@+id/edittext1"
android:inputType="number">
</EditText>
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/linearLayout2" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ENTER NO 2" >
</TextView>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.20"
android:id="@+id/edittext2"
android:inputType="number">
</EditText>
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/linearLayout3" >
<Button
android:layout_width="wrap_content"
android:id="@+id/button1"
android:layout_height="wrap_content"
android:text="Addition"
android:layout_weight="0.50" />
<Button
android:layout_width="wrap_content"
android:id="@+id/button3"
android:layout_height="wrap_content"
android:text="subtraction"
android:layout_weight="0.50" />
<Button
android:layout_width="wrap_content"
android:id="@+id/button2"
android:layout_height="wrap_content"
android:text="CLEAR"
android:layout_weight="0.50" />
</LinearLayout>
<View
android:layout_height="2px"
android:layout_width="fill_parent"
android:layout_below="@+id/linearLayout4"
android:background="#DDFFDD"/>
</RelativeLayout>
MainActivity.java
package com.example.ui.layout;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
EditText txtData1,txtData2;
float num1,num2,result1,result2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button add = (Button) findViewById(R.id.button1);
add.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try
{
txtData1 = (EditText) findViewById(R.id.edittext1);
txtData2 = (EditText) findViewById(R.id.edittext2);
num1 = Float.parseFloat(txtData1.getText().toString());
num2 = Float.parseFloat(txtData2.getText().toString());
result1=num1+num2;
Toast.makeText(getBaseContext(),"ANSWER:"+result1,Toast.LENGTH_SHORT).show();
}
catch(Exception e)
{
Toast.makeText(getBaseContext(), e.getMessage(),
Toast.LENGTH_SHORT).show();
}
}
});
Button sub = (Button) findViewById(R.id.button3);
sub.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try
{
txtData1 = (EditText) findViewById(R.id.edittext1);
txtData2 = (EditText) findViewById(R.id.edittext2);
num1 = Float.parseFloat(txtData1.getText().toString());
num2 = Float.parseFloat(txtData2.getText().toString());
result2=num1-num2;
Toast.makeText(getBaseContext(),"ANSWER:"+result2,Toast.LENGTH_SHORT).show();
}
catch(Exception e)
{
Toast.makeText(getBaseContext(), e.getMessage(),
Toast.LENGTH_SHORT).show();
}
}
});
Button clear = (Button) findViewById(R.id.button2);
clear.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try
{
txtData1.setText("");
txtData2.setText("");
}
catch(Exception e)
{
Toast.makeText(getBaseContext(), e.getMessage(),
Toast.LENGTH_SHORT).show();
}
}
});
}
}
Output
Result:
Ex.No.3 Date:
DEVELOP AN APPLICATION THAT DRAWS BASIC GRAPHICAL PRIMITIVES ON THE
SCREEN.
Aim:
To develop an android application to draw the basic graphical shapes on layout.
Algorithm:
1. Create a mobile application using Android studio
2. Design the prototype of the application
3. In source code tab, modify the code as the application logic.
4. In an Image View element, draw the bitmap image using Canvas and Paint objects.
5. Using Paint object we can draw rectangle, square and any drawing objects in a Canvas.
6. Save and test the application.
Procedure:
1. Open Android Studio and then click on File -> New -> New project.
2. Then type the Application name as “Graph″ and click Next.
3. Then select the Minimum SDK as shown below and click Next.
4. Then select the Empty Activity and click Next.
5. Finally click Finish.
6. It will take some time to build and load the project.
7. Click on app -> res -> layout -> activity_main.xml.
8. Now click on Next as shown below.
9. Then delete the code which is there and type the code as given (Activity_main.xml).
10. Now click on Design and your application will look as given below.
11. So now the designing part is completed.
12. Click on app -> java -> com.example.graph -> MainActivity.
13. Then delete the code which is there and type the code as given below.
14. Now run the application to see the output.
Source Code:
MainActivity.java
package com.example.user;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity
{ @Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(new myview(this));
}
public class myview extends View
{
public myview(Context context)
{
super(context);
}
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setTextSize(40);
paint.setColor(Color.GREEN);
canvas.drawText("Circle", 55, 30, paint);
paint.setColor(Color.RED);
canvas.drawCircle(100, 150, 100, paint);
paint.setColor(Color.GREEN);
canvas.drawText("Rectangle", 255, 30, paint);
paint.setColor(Color.YELLOW);
canvas.drawRect(250, 50, 400, 350, paint);
paint.setColor(Color.GREEN);
canvas.drawText("SQUARE", 55,430, paint);
paint.setColor(Color.BLUE);
canvas.drawRect(50, 450, 150, 550, paint);
paint.setColor(Color.GREEN);
canvas.drawText("LINE", 255, 430, paint);
paint.setColor(Color.CYAN);
canvas.drawLine(250, 500, 350, 500, paint);
}
}
}
activity_main.xml
<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=".MainActivity">
<TextView android:text="@string/hello_worldandroid:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
Output:
Result:
Ex.No.4 Date:
DEVELOP APPLICATION THAT MAKES USE OF DATABASE
Aim:
To develop an android application to manipulate the SQL database.
Algorithm:
1. Create a mobile application using Android studio
2. Design the prototype of the application
3. In source code tab, modify the code as the application logic.
4. Using SQLLite database, create a table and establish the connection using sqllitedatabse object.
5. Based on the operation by button click, do insert, view and delete the record from the table.
6. Save and test the application.
Procedure:
1. Open Android Studio and then click on File -> New -> New project.
2. Then type the Application name as “Database″ and click Next.
3. Then select the Minimum SDK as shown below and click Next.
4. Then select the Empty Activity and click Next.
5. Finally click Finish.
6. It will take some time to build and load the project.
7. Click on app -> res -> layout -> activity_main.xml.
8. Now click on Next as shown below.
9. Then delete the code which is there and type the code as given (Activity_main.xml).
10. Now click on Design and your application will look as given below.
11. So now the designing part is completed.
12. Click on app -> java -> com.example.database -> MainActivity.
13. Then delete the code which is there and type the code as given below.
14. Now run the application to see the output.
Source code:
MainActivity.java
package com.example.ui.db;
import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity implements OnClickListener
{
EditText editName,editNumber,editEid,editsalary;
Button btnAdd,btnDelete,btnViewAll,btnModify,btnView;
SQLiteDatabase db;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editEid =(EditText)findViewById(R.id.editEid);
editName=(EditText)findViewById(R.id.editName);
editsalary=(EditText)findViewById(R.id.editsalary);
btnAdd=(Button)findViewById(R.id.btnAdd);
btnDelete=(Button)findViewById(R.id.btnDelete);
btnModify=(Button)findViewById(R.id.btnModify);
btnView=(Button)findViewById(R.id.btnView);
btnViewAll=(Button)findViewById(R.id.btnViewAll);
btnAdd.setOnClickListener(this);
btnDelete.setOnClickListener(this);
btnModify.setOnClickListener(this);
btnView.setOnClickListener(this);
btnViewAll.setOnClickListener(this);
db=openOrCreateDatabase("EmployeeDB", Context.MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS employee(empid VARCHAR,name
VARCHAR,salary VARCHAR);");
}
public void onClick(View view)
{ if(view==btnAdd)
{
if(editEid.getText().toString().trim().length()==0||
editName.getText().toString().trim().length()==0||
editsalary.getText().toString().trim().length()==0)
{
showMessage("Error", "Please enter all values");
return;
}
db.execSQL("INSERT INTO employee VALUES('"+ editEid.getText()+"','"+editName.getText()+
"','"+editsalary.getText()+"');");
showMessage("Success", "Record added");
clearText();
}
if(view==btnDelete)
{
if(editEid.getText().toString().trim().length()==0)
{
showMessage("Error", "Please enter Employee id");
return;
}
Cursor c=db.rawQuery("SELECT * FROM employee WHERE empid='"+editEid.getText()+"'", null);
if(c.moveToFirst())
{
db.execSQL("DELETE FROM employee WHERE empid='"+editEid.getText()+"'");
showMessage("Success", "Record Deleted");
}
else
{
showMessage("Error", "Invalid Employee id");
}
clearText();
} if(view==btnModify)
{
if(editEid.getText().toString().trim().length()==0)
{
showMessage("Error", "Please enter Employee id");
return;
}
Cursor c=db.rawQuery("SELECT * FROM employee WHERE empid='"+editEid.getText()+"'", null);
if(c.moveToFirst())
{
db.execSQL("UPDATE employee SET name='"+editName.getText()+"',salary='"+editsalary.getText()+
"' WHERE empid='"+ editEid.getText()+"'");
showMessage("Success", "Record Modified");
}
else
{
showMessage("Error", "Invalid Rollno");
}
clearText();
}
if(view==btnView)
{
if(editEid.getText().toString().trim().length()==0)
{
showMessage("Error", "Please enter Employee id");
return;
}
Cursor c=db.rawQuery("SELECT * FROM employee WHERE empid='"+ editEid.getText()+"'", null);
if(c.moveToFirst())
{
editName.setText(c.getString(1));
editsalary.setText(c.getString(2));
}
else
{ showMessage("Error", "Invalid Employee id");
clearText();
}
}
if(view==btnViewAll)
{
editEid.setText("");
editName.setText("");
editsalary.setText("");
editEid.requestFocus();
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myLayout"
android:stretchColumns="0"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:text="@string/title"
android:layout_x="110dp"
android:layout_y="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:text="@string/empid"
android:layout_x="30dp"
android:layout_y="50dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText android:id="@+id/editEid"
android:inputType="number"
android:layout_x="150dp"
android:layout_y="50dp"
android:layout_width="150dp"
android:layout_height="40dp"/>
<TextView android:text="@string/name"
android:layout_x="30dp"
android:layout_y="100dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText android:id="@+id/editName"
android:inputType="text"
android:layout_x="150dp"
android:layout_y="100dp"
android:layout_width="150dp"
android:layout_height="40dp"/>
<TextView android:text="@string/salary"
android:layout_x="30dp"
android:layout_y="150dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText android:id="@+id/editsalary"
android:inputType="number"
android:layout_x="150dp"
android:layout_y="150dp"
android:layout_width="150dp"
android:layout_height="40dp"/>
<Button android:id="@+id/btnAdd"
android:text="@string/add"
android:layout_x="30dp"
android:layout_y="200dp"
android:layout_width="130dp"
android:layout_height="40dp"/>
<Button android:id="@+id/btnDelete"
android:text="@string/delete"
android:layout_x="160dp"
android:layout_y="200dp"
android:layout_width="130dp"
android:layout_height="40dp"/>n
<Button android:id="@+id/btnModify"
android:text="@string/modify"
android:layout_x="30dp"
android:layout_y="250dp"
android:layout_width="130dp"
android:layout_height="40dp"/>
<Button android:id="@+id/btnView"
android:text="@string/view"
android:layout_x="160dp"
android:layout_y="250dp"
android:layout_width="130dp"
android:layout_height="40dp"/>
<Button android:id="@+id/btnViewAll"
android:text="@string/view_all"
android:layout_x="85dp"
android:layout_y="300dp"
android:layout_width="150dp"
android:layout_height="40dp"/>
</AbsoluteLayout>
menu_main.xml
<menu 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" tools:context=".MainActivity">
<item android:id="@+id/action_settings" android:title="@string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
</menu>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Employee detail1</string>
<string name="hello">Hello World, Employee detail Activity!</string>
<string name="title">Employee Details</string>
<string name="empid">Enter Employee ID: </string>
<string name="empno">Enter Employee ID: </string>
<string name="name">Enter Name: </string>
<string name="salary">Enter salary: </string>
<string name="add">Add Employee</string>
<string name="delete">Delete Employee</string>
<string name="modify">Modify Employee</string>
<string name="view">View Employee</string>
<string name="view_all">View All Employee</string>
<string name="action_settings" />
</resources>
Output:
Result:
Ex.No.5 Date:
DEVELOP AN APPLICATION THAT MAKES USE OF RSS FEED
Aim:
To develop an android application to get the data from website using RSS Feed.
Algorithm:
1. Create a mobile application using Android studio
2. Design the prototype of the application
3. In source code tab, modify the code as the application logic.
4. Using XML parser object, fetch the data like Title, Link and Descriptions of given website.
5. Using web view activity fetch the content of the given website.
6. Save and test the application.
Procedure:
1. Open Android Studio and then click on File -> New -> New project.
2. Then type the Application name as “RSSFeed″ and click Next.
3. Then select the Minimum SDK as shown below and click Next.
4. Then select the Empty Activity and click Next.
5. Finally click Finish.
6. It will take some time to build and load the project.
7. Click on app-> AndroidManifest.xml
8. Using uses-permission tag add the Internet access permission to application
9. Click on app -> res -> layout -> activity_main.xml.
10. Now click on Next as shown below.
11. Then delete the code which is there and type the code as given (Activity_main.xml).
12. Now click on Design and your application will look as given below.
13. So now the designing part is completed.
14. Click on app -> java -> com.example.rssfeed -> MainActivity.
15. Then delete the code which is there and type the code as given below.
16. Now run the application to see the output.
Source Code:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ammu.rssfeed">
<uses-permission android:name="android.permission.INTERNET"/>
<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="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"
/>
</intent-filter>
</activity>
<activity android:name=".WebViewActivity"/>
</application>
</manifest>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
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>
activity_webview.xml
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</WebView
MainActivity.java
package com.example.ammu.rssfeed;
import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.webkit.WebView;
import android.webkit.WebViewClient;
setContentView(R.layout.activity_webview);
WebView webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl(url);
}
}
Output:
Result:
Ex.No.6 Date:
DEVELOP AN APPLICATION THAT IMPLEMENTS MULTI-THREADING
Aim:
To develop an android application that implements multi threading.
Algorithm:
1. Create a mobile application using Android studio
2. Design the prototype of the application
3. In source code tab, modify the code as the application logic.
4. Create three run able threads for multiple task using Thread object.
5. Save and test the application.
Procedure:
1. Open Android Studio and then click on File -> New -> New project.
2. Then type the Application name as “Multithread” and click Next.
3. Then select the Minimum SDK as shown below and click Next.
4. Then select the Empty Activity and click Next.
5. Finally click Finish.
6. It will take some time to build and load the project.
7. Click on app -> res -> layout -> activity_main.xml.
8. Now click on Next as shown below.
9. Then delete the code which is there and type the code as given (Activity_main.xml).
10. Now click on Design and your application will look as given below.
11. So now the designing part is completed.
12. Click on app -> java -> com.example.multithread -> MainActivity.
13. Then delete the code which is there and type the code as given below.
14. Now run the application to see the output.
Source Code:
MainActivity.java
package com.example.ui.thread;
//import your.first.R;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView tvOutput;
private static final int t1 = 1;
private static final int t2 = 2;
private static final int t3 = 3;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvOutput = (TextView) findViewById(R.id.textView1);
}
public void fetchData(View v) {
tvOutput.setText("Main thread");
thread1.start();
thread2.start();
thread3.start();
}
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
handler.sendEmptyMessage(t1);
}
}
});
Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
handler.sendEmptyMessage(t2);
}
}
});
Thread thread3 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
handler.sendEmptyMessage(t3);
}
}
});
Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
if(msg.what == t1) {
tvOutput.append("\nIn thread 1");
}
if(msg.what == t2) {
tvOutput.append("\nIn thread 2");
}
if(msg.what == t3) {
tvOutput.append("\nIn thread 3");
}
}
};
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ui.thread" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
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"
android:id="@+id/info" >
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="fetchData"
android:text="Start MULTITHREAD" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Main thread" />
</LinearLayout>
Output:
Result:
Ex.No.7 Date:
DEVELOP A NATIVE APPLICATION THAT USES GPS LOCATION INFORMATION
Aim:
To develop an android application that detects the GPS location.
Algorithm:
1. Create a mobile application using Android studio
2. Design the prototype of the application
3. In source code tab, modify the code as the application logic.
4. Using LocationListener package and Location System service create an object Location
Manager which fetches the network provider and GPS information.
5. Save and test the application.
Procedure:
1. Open Android Studio and then click on File -> New -> New project.
2. Then type the Application name as “GPStrace” and click Next.
3. Then select the Minimum SDK as shown below and click Next.
4. Then select the Empty Activity and click Next.
5. Finally click Finish.
6. It will take some time to build and load the project.
7. Click on app-> AndroidManifest.xml
8. Using uses-permission tag add the Internet access permission and Access Fine Location
permission to application.
9. Click on app -> res -> layout -> activity_main.xml.
10. Now click on Next as shown below.
11. Then delete the code which is there and type the code as given (Activity_main.xml).
12. Now click on Design and your application will look as given below.
13. So now the designing part is completed.
14. Click on app -> java -> com.example.gpstrace -> MainActivity.
15. Then delete the code which is there and type the code as given below.
16. Now run the application to see the output.
Source Code:
MainActivity.java
package com.example.ui.gps;
//import android.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
Button btnShowLocation;
GPStrace gps;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnShowLocation=(Button)findViewById(R.id.show_Location);
btnShowLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
gps=new GPStrace(MainActivity.this);
if(gps.canGetLocation()){
double latitude=gps.getLatitude();
double longitude=gps.getLongtiude();
Toast.makeText(getApplicationContext(),"Your Location is \nLat:"+latitude+"\
nLong:"+longitude, Toast.LENGTH_LONG).show();
}
else
{
gps.showSettingAlert();
}
}
});
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/show_Location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show_Location"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ui.gps" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission
android:name="android.permission.INTERNET"/>
</manifest>
GPStrace.java
package com.example.ui.gps;
import android.app.AlertDialog;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;
public class GPStrace extends Service implements LocationListener{
private final Context context;
boolean isGPSEnabled=false;
boolean canGetLocation=false;
boolean isNetworkEnabled=false;
Location location;
double latitude;
double longtitude;
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 GPStrace(Context context)
{
this.context=context;
getLocation();
}
public Location getLocation()
{
try{
locationManager=(LocationManager) context.getSystemService(LOCATION_SERVICE);
isGPSEnabled=locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled=locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if(!isGPSEnabled && !isNetworkEnabled){
}else{
this.canGetLocation=true;
if(isNetworkEnabled){
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES,this);
}
if(locationManager!=null){
location=locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if(location !=null){
latitude=location.getLatitude();
longtitude=location.getLongitude();
}
}
}
if(isGPSEnabled){
if(location==null){
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MIN_TIME_BW_UPD
ATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
if(locationManager!=null){
location=locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(location!=null){
latitude=location.getLatitude();
longtitude=location.getLongitude();
}
}
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
return location;
}
public void stopUsingGPS(){
if(locationManager!=null){
locationManager.removeUpdates(GPStrace.this);
}
}
public double getLatitude(){
if(location!=null){
latitude=location.getLatitude();
}
return latitude;
}
public double getLongtiude(){
if(location!=null){
longtitude=location.getLatitude();
}
return longtitude;
}
public boolean canGetLocation(){
return this.canGetLocation;
}
public void showSettingAlert(){
AlertDialog.Builder alertDialog=new AlertDialog.Builder(context);
alertDialog.setTitle("GPS is settings");
alertDialog.setMessage("GPS is not enabled.Do you want to go to setting menu?");
alertDialog.setPositiveButton("settings", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,int which){
Intent intent=new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
context.startActivity(intent);
}
});
alertDialog.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.cancel();
}
});
alertDialog.show();
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
Output:
Result:
Ex.No.8 Date:
DEVELOP AN APPLICATION THAT WRITES DATA TO THE SD CARD
Aim:
To develop an android application that writes and read the data to the SD card.
Algorithm:
1. Create a mobile application using Android studio
2. Design the prototype of the application
3. In source code tab, modify the code as the application logic.
4. Using File and FileInputStream, fetch the data from user and creates a file under MyDirectory
folder in SDCard.
5. Using File and FileOutputStream fetch the data from the file and display as message.
6. Save and test the application.
Procedure:
1. Open Android Studio and then click on File -> New -> New project.
2. Then type the Application name as “sdcard” and click Next.
3. Then select the Minimum SDK as shown below and click Next.
4. Then select the Empty Activity and click Next.
5. Finally click Finish.
6. It will take some time to build and load the project.
7. Click on app-> AndroidManifest.xml
8. Using uses-permission tag adds the WRITE_EXTERNAL_STORAGE permission to the
application.
9. Click on app -> res -> layout -> activity_main.xml.
10. Now click on Next as shown below.
11. Then delete the code which is there and type the code as given (Activity_main.xml).
12. Now click on Design and your application will look as given below.
13. So now the designing part is completed.
14. Click on app -> java -> com.example.multithread -> MainActivity.
15. Then delete the code which is there and type the code as given below.
16. Now run the application to see the output.
Source Code:
MainActivity.java
package com.example.ui.sdcard;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.util.jar.Manifest;
//import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity
{
EditText e1;
Button write,read,clear;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
}
write.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
String message=e1.getText().toString();
try
{
read.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
String message;
String buf = "";
try
{
File f = new File("/sdcard/myfile.txt");
FileInputStream fin = new FileInputStream(f);
BufferedReader br = new BufferedReader(new InputStreamReader(fin));
while ((message = br.readLine()) != null)
{
buf += message;
}
e1.setText(buf);
br.close();
fin.close();
Toast.makeText(getBaseContext(),"Data Recived from
SDCARD",Toast.LENGTH_LONG).show();
}
catch (Exception e)
{
Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
}
});
clear.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
e1.setText("");
}
});
}
}
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: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>
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ui.sdcard" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-
permission>
</manifest>
Output
Result:
Ex.No.9 Date:
DEVELOP AN APPLICATION THAT CREATES AN ALERT UPON RECEIVING A
MESSAGE
Aim:
To develop an android application that creates a notification alert upon receiving a message.
Algorithm:
1. Create a mobile application using Android studio
2. Design the prototype of the application
3. In source code tab, modify the code as the application logic.
4. Create Notification Manager Object.
5. Create Builder and initiate builder notification.
6. Save and test the application.
Procedure:
1. Open Android Studio and then click on File -> New -> New project.
2. Then type the Application name as “notification” and click Next.
3. Then select the Minimum SDK as shown below and click Next.
4. Then select the Empty Activity and click Next.
5. Finally click Finish.
6. It will take some time to build and load the project.
7. Click on app -> res -> layout -> activity_main.xml.
8. Now click on Next as shown below.
9. Then delete the code which is there and type the code as given (Activity_main.xml).
10. Now click on Design and your application will look as given below.
11. So now the designing part is completed.
12. Click on app -> java -> com.example.notification -> MainActivity.
13. Then delete the code which is there and type the code as given below.
14. Now run the application to see the output.
Source Code:
MainActivity.java
package com.example.user.notification;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import com.example.user.notification.R;
public class MainActivity extends ActionBarActivity {
Button btnShow, btnClear;
NotificationManager manager;
Notification myNotication;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialise();
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
btnShow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
//API level 11
Intent intent = new Intent("com.example.user.notifications.SECACTIVITY");
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 1, intent, 0);
Notification.Builder builder = new Notification.Builder(MainActivity.this);
builder.setAutoCancel(false);
builder.setTicker("this is ticker text");
builder.setContentTitle("WhatsApp Notification");
builder.setContentText("You have a new message");
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setContentIntent(pendingIntent);
builder.setOngoing(true);
builder.setSubText("This is subtext..."); //API level 16
builder.setNumber(100);
builder.build();
myNotication = builder.getNotification();
manager.notify(11, myNotication);
/* //API level 8
Notification myNotification8 = new Notification(R.drawable.ic_launcher, "this is ticker text
8", System.currentTimeMillis());
Intent intent2 = new Intent(MainActivity.this, SecActivity.class);
PendingIntent pendingIntent2 = PendingIntent.getActivity(getApplicationContext(), 2, intent2,
0);
myNotification8.setLatestEventInfo(getApplicationContext(), "API level 8", "this is api 8
msg", pendingIntent2);
manager.notify(11, myNotification8);
*/
}
});
btnClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
manager.cancel(11);
}
});
}
private void initialise() {
btnShow = (Button) findViewById(R.id.btnShowNotification);
btnClear = (Button) findViewById(R.id.btnClearNotification);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/btnShowNotification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Notification" />
<Button
android:id="@+id/btnClearNotification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clear Notification" />
</LinearLayout>
Output
Result:
Ex. No. 10 Develop a mobile application to send an
email. Date:
Aim:
To develop an Android Application to send an Email.
Procedure:
Creating a New project:
Open Android Studio and then click on File -> New -> New project.
Then type the Application name as “exno11″ 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.
<activity
android:name="com.example.exno11.MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<action
android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="message/rfc822"/>
</intent-filter>
</activity>
</application>
</manifest>
import android.content.Intent;
//import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
Output:
Result:
Thus Android Application for sending an email is developed and executed successfully.
Ex. No. 11 Develop a Mobile application for simple needs (Mini Project)
Date:
Aim:
To develop a Simple Android Application for Native Calculator.
Procedure:
Creating a New project:
Open Android Studio and then click on File -> New -> New project.
Then type the Application name as “exno12″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.
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp">
<EditText android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="numberDecimal"
android:textSize="20sp" />
<EditText android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="numberDecimal"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp">
<Button android:id="@+id/Add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="+"
android:textSize="30sp"/>
<Button android:id="@+id/Sub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="-"
android:textSize="30sp"/>
<Button android:id="@+id/Mul"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="*"
android:textSize="30sp"/>
<Button android:id="@+id/Div"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="/"
android:textSize="30sp"/>
</LinearLayout>
<TextView android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="Answer is"
android:textSize="30sp"
android:gravity="center"/>
</LinearLayout>
Now click on Design and your application will look as given below.
So now the designing part is completed.
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// set a listener
Add.setOnClickListener(this);
Sub.setOnClickListener(this);
Mul.setOnClickListener(this);
Div.setOnClickListener(this);
}
@Override
public void onClick (View v)
{
float num1 = 0;
float num2 = 0;
float result = 0;
String oper = "";
// check if the fields are empty
if (TextUtils.isEmpty(Num1.getText().toString()) ||
TextUtils.isEmpty(Num2.getText().toString()))
return;
// defines the button that has been clicked and performs the corresponding
operation
// write operation into oper, we will use it later for output
switch (v.getId())
{
case R.id.Add:
oper = "+";
result = num1 + num2;
break;
case R.id.Sub:
oper = "-";
result = num1 - num2;
break;
case R.id.Mul:
oper = "*";
result = num1 * num2;
break;
case R.id.Div:
oper = "/";
result = num1 / num2;
break;
default:
break;
}
// form the output line
Result.setText(num1 + " " + oper + " " + num2 + " = " + result);
}
}
So now the Coding part is also
completed.
Now run the application to see the output.
Output:
Result:
Thus a Simple Android Application for Native Calculator is developed and executed
successfully.