0% found this document useful (0 votes)
15 views32 pages

Mad Lab

The document outlines a series of exercises for developing Android applications, each focusing on different user interface components such as reversing a string, implementing buttons, lists, progress dialogs, date pickers, and intents. Each exercise includes a clear aim, step-by-step procedures, and the necessary XML and Java code to create the applications in Android Studio. The document serves as a practical guide for beginners to learn Android app development through hands-on projects.

Uploaded by

Vimal Cheyyar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views32 pages

Mad Lab

The document outlines a series of exercises for developing Android applications, each focusing on different user interface components such as reversing a string, implementing buttons, lists, progress dialogs, date pickers, and intents. Each exercise includes a clear aim, step-by-step procedures, and the necessary XML and Java code to create the applications in Android Studio. The document serves as a practical guide for beginners to learn Android app development through hands-on projects.

Uploaded by

Vimal Cheyyar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 32

Ex. No.

01
Reverse String
Date :

AIM:
To develop a Android Application to Reverse the Given String

Procedure:

Step 1: Create a new project in Android Studio and name it Edit text.
Select File -> New -> New Project ->. Fill the forms and click "Finish" button.

Step 2: Open res -> layout -> activity_main.xml (or) main.xml and add following code.

activity_main.xml

<?xml version = "1.0" encoding = "utf-8"?>


<LinearLayout 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"
tools:context = ".MainActivity"
android:orientation = "vertical">
<EditText
android:id = "@+id/name"
android:layout_width = "match_parent"
android:hint = "Enter Name"
android:layout_height = "wrap_content" />
<LinearLayout
android:layout_width = "wrap_content"
android:layout_height = "wrap_content">
<Button
android:id = "@+id/save"
android:text = "Save"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content" />
</LinearLayout>
<TextView
android:id = "@+id/textview"
android:layout_width = "match_parent"
android:layout_height = "match_parent" />
</LinearLayout>
Step 3: Now open app-> java -> package -> MainActivity.java and add the following code.

MainActivity.java

package com.example.acer.reverse;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity {
EditText name;
HashMap<Character, Integer> charCountMap;
TextView textview;
@Override
protected void onCreate(Bundle readdInstanceState) {
super.onCreate(readdInstanceState);
setContentView(R.layout.activity_main);
name =(EditText) findViewById(R.id.name);
final TextView textview =(TextView) findViewById(R.id.textview);
charCountMap = new HashMap<>();
findViewById(R.id.save).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!name.getText().toString().isEmpty()) {
StringBuffer c = new StringBuffer(name.getText().toString());
textview.setText(c.reverse());
Toast.makeText(MainActivity.this, "Inserted", Toast.LENGTH_LONG).show();
} else {
name.setError("Enter NAME");
}
}
});
}
Step 4: Now run the application to see the output.
Ex. No. 02
USER INTERFACE COMPONENTS (TEXT, EDIT TEXT,
BUTTON)
Date :

AIM:
To develop a Android Application to implement ANDROID USER INTERFACE
COMPONENTS (TEXT, EDIT TEXT, BUTTON)

Procedure:

Step 1: Create a new project in Android Studio and name it Edit text.
Select File -> New -> New Project ->. Fill the forms and click "Finish" button.

Step 2: Open res -> layout -> activity_main.xml (or) main.xml and add following code.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>


<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.user.edittext.MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter the first Number"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/number1"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="40dp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter the Second number"
android:id="@+id/textView2"
android:layout_below="@+id/number1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="44dp" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/number2"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="sum"
android:id="@+id/button"
android:layout_below="@+id/number2"
android:layout_centerHorizontal="true"
android:layout_marginTop="53dp" />
</RelativeLayout>

Step 3: Now open app-> java -> package -> MainActivity.java and add the following
code.

mainactivity.java

package com.example.user.edittext;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity
{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText firstNum = (EditText)findViewById(R.id.number1);
final EditText secNum = (EditText)findViewById(R.id.number2);
Button btnAdd = (Button)findViewById(R.id.button);
btnAdd.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
if (firstNum.getText().toString().isEmpty() ||
secNum.getText().toString().isEmpty())
{
Toast.makeText(getApplicationContext(), "Please fill
all the fields", Toast.LENGTH_SHORT).show();
}
else
{
int num1 =
Integer.parseInt(firstNum.getText().toString());
int num2 =
Integer.parseInt(secNum.getText().toString());
Toast.makeText(getApplicationContext(), "SUM = " +
(num1 + num2), Toast.LENGTH_SHORT).show();
} }
});
}
}
Step 4: Now run the application to see the output.
Ex. No. 03
List View
Date :

AIM:
To develop a Android Application to implement User Interface Components (List
View)

Procedure:

Step 1: Create a new project in Android Studio and name it Edit text.
Select File -> New -> New Project ->. Fill the forms and click "Finish" button.

Step 2: Open res -> layout -> activity_main.xml (or) main.xml and add following code.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.user.listexample.MainActivity">

<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:divider="#e66e3f"
android:dividerHeight="1dp"
android:listSelector="#0f0"/>
</LinearLayout>
Step 3: Now open app-> java -> package -> MainActivity.java and add the following
code.

MainActivity.java
package com.example.acer.listview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView);
final ArrayList<String> arrayList=new ArrayList<>();
arrayList.add("India");
arrayList.add("China");
arrayList.add("Australia");
arrayList.add("America");
arrayList.add("Newzeland");
arrayList.add("portugle");

ArrayAdapter arrayAdapter = new ArrayAdapter(this,


android.R.layout.simple_list_item_1,arrayList);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int i, long l) {
Toast.makeText(MainActivity.this,"clicked
item:"+i+""+arrayList .get(i).toString() ,Toast.LENGTH_SHORT ).show();
}

});

}
}
Step 4: Now run the application to see the output.
Ex. No. 04
Progress Dialog
Date :

AIM :
To develop a Android Application to implement User Interface Components (List
View)

Procedure:

Step 1: Create a new project in Android Studio and name it Edit text.
Select File -> New -> New Project ->. Fill the forms and click "Finish" button.

Step 2: Open res -> layout -> activity_main.xml (or) main.xml and add following code.

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.progessdialogexample.MainActivity"
android:elevation="1dp">

<Button
android:text=" Click To View Progress Dialog..."
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="171dp"
android:id="@+id/button"
style="@android:style/Widget.DeviceDefault.Button.Inset"
android:textStyle="normal|bold"
android:textSize="15sp"
android:textColor="@android:color/background_dark"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />

</RelativeLayout>
Step 3: Now open app-> java -> package -> MainActivity.java and add the following
code.
MainActivity.java

package com.example.acer.progressdialogue;

import android.app.ProgressDialog;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {


Button b1;
ProgressDialog progressDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.button);

b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("Loading..."); // Setting Message
progressDialog.setTitle("ProgressDialog"); // Setting Title
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); // Progress Dialog Style
Spinner
progressDialog.show(); // Display Progress Dialog
progressDialog.setCancelable(false);
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(10000);
} catch (Exception e) {
e.printStackTrace();
}
progressDialog.dismiss();
}
}).start();
}
});

}
}

Step 4: Now run the application to see the output.


Ex. No. 05
Date Picker
Date :
AIM :
To develop a Android Application to implement User Interface Components (List
View)

Procedure:

Step 1: Create a new project in Android Studio and name it Edit text.
Select File -> New -> New Project ->. Fill the forms and click "Finish" button.

Step 2: Open res -> layout -> activity_main.xml (or) main.xml and add following code.
activity_main.xml
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity
{
TextView tView;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final DatePicker picker;
Button btnGet;
tView=(TextView)findViewById(R.id.textView);
picker=(DatePicker)findViewById(R.id.datePicker);
btnGet=(Button)findViewById(R.id.button);
btnGet.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
tView.setText("selected Date:" + picker.getDayOfMonth() + "/" +
(picker.getMonth() + 1) + "/" + picker.getYear());

}
});
}
}

Step 3: Now open app-> java -> package -> MainActivity.java and add the following
code.
MainActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity
{
TextView tView;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final DatePicker picker;
Button btnGet;
tView=(TextView)findViewById(R.id.textView);
picker=(DatePicker)findViewById(R.id.datePicker);
btnGet=(Button)findViewById(R.id.button);
btnGet.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
tView.setText("selected Date:" + picker.getDayOfMonth() + "/" +
(picker.getMonth() + 1) + "/" + picker.getYear());

}
});
}
}

Step 4: Now run the application to see the output.


Ex. No. 06
Intent
Date :
AIM:
To develop a Android Application to implement working with activity
Procedure:
Step 1: Create a new project and name it ex2
Select File -> New -> New Project… then Fill the forms and click "Finish" button.
Step 2: Open res -> layout -> activity_main.xml (or) main.xml and add following code:
In this step we open an xml file and add the code for displaying the personal details.

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="vertical"
android:padding="10dp"
android:gravity="center">

<EditText
android:id="@+id/ED1"
android:hint="Enter name"
android:inputType="text"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

<EditText
android:id="@+id/ED2"
android:hint="Enter EMail"
android:inputType="textEmailAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

<EditText
android:id="@+id/ED3"
android:hint="Enter Phone"
android:inputType="phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btn"
android:text="Save"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>

activity_main2.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:padding="10dp"
android:gravity="center" >

<TextView
android:id="@+id/TV1"
android:textSize="30sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

</LinearLayout>

Step 3: Now Open src -> package -> MainActivity.java and add the following code.

MainActivity.java
package com.example.user.ex2;

import android.os.Bundle;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

final EditText ET1 = (EditText) findViewById(R.id.ED1);


final EditText ET2 = (EditText) findViewById(R.id.ED2);
final EditText ET3 = (EditText) findViewById(R.id.ED3);
Button bt = (Button) findViewById(R.id.btn);
bt.setOnClickListener(new View.OnClickListener() {

public void onClick(View arg0) {


// TODO Auto-generated method stub
String name = ET1.getText().toString();
String email = ET2.getText().toString();
String phone = ET3.getText().toString();

Intent intent = new Intent(getBaseContext(), Main2Activity.class);


intent.putExtra("Name", name);
intent.putExtra("EMAIL", email);
intent.putExtra("Phone", phone);
startActivity(intent);
}
});

Main2Activity.java

package com.example.user.ex2;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class Main2Activity extends Activity {

protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);

Intent intent = getIntent();


String name = intent.getStringExtra("Name");
String email = intent.getStringExtra("EMAIL");
String phone = intent.getStringExtra("Phone");

TextView txt = (TextView) findViewById(R.id.TV1);


txt.setText("Name :" + name + "\n Email :" + email + "\nPhone :" +phone);
}
}

Step 4: Now run the application to see the output.


Ex. No. 07
Date : Option Menu
AIM:
To develop a Android Application to implement working with activity
Procedure:
Step 1: Create a new project and name it Option Menu
Select File -> New -> New Project… then Fill the forms and click "Finish" button.
Step 2: Open res -> layout -> activity_main.xml (or) main.xml and add following code:
In this step we open an xml file and add the code for displaying the personal details.

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.user.optionmenudemo.MainActivity">

</RelativeLayout>

optionmenu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/search_item"
android:title="search" />
<item android:id="@+id/upload_item"
android:title="Upload" />
<item android:id="@+id/copy_item"
android:title="Copy" />

</menu>

Step 3: Now Open src -> package -> MainActivity.java and add the following code.

MainActivity.java
package com.example.acer.optionmenu;

import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;

public class MainActivity extends AppCompatActivity {


private GoogleApiClient client;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.optionmenu, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
Toast.makeText(this, "Selected Item: " + item.getTitle(), Toast.LENGTH_SHORT).show();
switch (item.getItemId()) {
case R.id.search_item:
// do your code
return true;
case R.id.upload_item:
// do your code
return true;
case R.id.copy_item:
// do your code
return true;

default:
return super.onOptionsItemSelected(item);
}
}

@Override
public void onStart() {
super.onStart();
client.connect();
Action viewAction = Action.newAction(
Action.TYPE_VIEW, // TODO: choose an action type.
"Main Page", // TODO: Define a title for the content shown.
Uri.parse("http://host/path"),
Uri.parse("android-app://com.example.acer.optionmenu/http/host/path")
);
AppIndex.AppIndexApi.start(client, viewAction);
}

@Override
public void onStop() {
super.onStop();

Action viewAction = Action.newAction(


Action.TYPE_VIEW, // TODO: choose an action type.
"Main Page", // TODO: Define a title for the content shown.
Uri.parse("http://host/path")
Uri.parse("android-app://com.example.acer.optionmenu/http/host/path")
);
AppIndex.AppIndexApi.end(client, viewAction);
client.disconnect();
}
}
Step 4: Now run the application to see the output.

Ex. No. 08
Date : Audio Menu
AIM:
To develop a Android Application to implement working with activity
Procedure:
Step 1: Create a new project and name it Option Menu
Select File -> New -> New Project… then Fill the forms and click "Finish" button.
Step 2: Open res -> layout -> activity_main.xml (or) main.xml and add following code:
In this step we open an xml file and add the code for displaying the personal details.

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.acer.audio.MainActivity">

</RelativeLayout>

Step 3: Now Open src -> package -> MainActivity.java and add the following code.

MainActivity.java
package com.example.acer.audio;

import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MediaPlayer ring= MediaPlayer.create(MainActivity.this, R.raw.rahmen);
ring.start();
}
}

Step 4: Now run the application to see the output.


Ex. No. 9
Date : E - MAIL

AIM:
To develop a Android Application to send E-Mail via intent

Procedure:
Step 1: Create a new project in Android Studio and name it SendMailExample.
Select File -> New -> New Project ->. Fill the forms and click "Finish" button.

Step 2: Open res -> layout -> activity_main.xml (or) main.xml and add following code.

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:paddingLeft="20dp"
android:paddingRight="20dp"
android:orientation="vertical" >
<EditText
android:id="@+id/txtTo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="To"/>
<EditText
android:id="@+id/txtSub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Subject"/>
<EditText
android:id="@+id/txtMsg"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="top"
android:hint="Message"/>
<Button
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="Send"
android:id="@+id/btnSend"/>
</LinearLayout>

Step 3: Open app -> manifests ->AndroidManifest.xml and add following code:

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.user.sendmail">

<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="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>

Step 4: Now open app-> java -> package -> MainActivity.java and add the following
code.

MainActivity.java
package com.example.user.sendmail;

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;

public class MainActivity extends AppCompatActivity {

private EditText eTo;


private EditText eSubject;
private EditText eMsg;
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
eTo = (EditText)findViewById(R.id.txtTo);
eSubject = (EditText)findViewById(R.id.txtSub);
eMsg = (EditText)findViewById(R.id.txtMsg);
btn = (Button)findViewById(R.id.btnSend);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent it = new Intent(Intent.ACTION_SEND);
it.putExtra(Intent.EXTRA_EMAIL, new String[]{eTo.getText().toString()});
it.putExtra(Intent.EXTRA_SUBJECT,eSubject.getText().toString());
it.putExtra(Intent.EXTRA_TEXT,eMsg.getText());
it.setType("message/rfc822");
startActivity(Intent.createChooser(it,"Choose Mail App"));
}
});
}
}

Step 5: Now run the application to see the output.

Ex. No. 10
SMS
Date :

AIM:
To develop a Android Application to send SMS via intent

Procedure:

Step 1: Create a new project in Android Studio and name it Phone sms.
Select File -> New -> New Project ->. Fill the forms and click "Finish" button.

Step 2: Open res -> layout -> activity_main.xml (or) main.xml and add following code.

activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="vertical" >

<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="40dp"
android:ems="10" />

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mobile No:"
android:layout_below="@+id/editText1"
android:layout_alignRight="@+id/textView2"
android:layout_alignEnd="@+id/textView2" />

<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="40dp"
android:ems="10"
android:inputType="textMultiLine" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Message:"
android:layout_below="@+id/editText2"
android:layout_centerHorizontal="true" />

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send SMS"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>

Step 3: Open app -> manifests ->AndroidManifest.xml and add following code:

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.user.phonesms" >
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name="com.example.user.phonesms.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

Step 4: Now open app-> java -> package -> MainActivity.java and add the following
code.

MainActivity.java
package com.example.user.phonesms;
import android.os.Bundle;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.telephony.SmsManager;
import android.view.Menu;
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 mobileno, message;
Button sendsms;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mobileno = (EditText) findViewById(R.id.editText1);
message = (EditText) findViewById(R.id.editText2);
sendsms = (Button) findViewById(R.id.button1);
sendsms.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
String no = mobileno.getText().toString();
String msg = message.getText().toString();
Intent intent = new Intent(getApplicationContext(),
MainActivity.class);
PendingIntent pi =
PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(no, null, msg, pi, null);
Toast.makeText(getApplicationContext(), "Message Sent
successfully!",
Toast.LENGTH_LONG).show();
}
});
}
}

Step 5: Now run the application to see the output.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy