0% found this document useful (0 votes)
18 views2 pages

Practical No 29

The document contains code for an Android application that allows users to send SMS messages. It includes the layout file 'activity_main.xml' with input fields for mobile number and message, as well as a button to send the SMS. The 'MainActivity.java' handles the SMS sending functionality and displays success or failure messages to the user.

Uploaded by

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

Practical No 29

The document contains code for an Android application that allows users to send SMS messages. It includes the layout file 'activity_main.xml' with input fields for mobile number and message, as well as a button to send the SMS. The 'MainActivity.java' handles the SMS sending functionality and displays success or failure messages to the user.

Uploaded by

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

Practical no.

29

Activity_main.xml

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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Mob. Number: "
android:inputType="phone"
android:id="@+id/et1"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Write Messege Here"
android:id="@+id/et2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SEND"
android:id="@+id/bt1"/>
</LinearLayout>

AndroidMenifest.xml

<uses-permission android:name="android.permission.SEND_SMS"/>

MainActivity.java

package com.example.sms;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText e1=(EditText) findViewById(R.id.et1);
EditText e2=(EditText) findViewById(R.id.et2);
Button b1=(Button) findViewById(R.id.bt1);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
SmsManager sm=SmsManager.getDefault();
sm.sendTextMessage(e1.getText().toString(),null,e2.getText().toString(),null,null);
Toast.makeText(MainActivity.this, "SMS sent Successfully.", Toast.LENGTH_SHORT).show();
}
catch(Exception e)
{
Toast.makeText(MainActivity.this, "SMS fail to send, Please try again", Toast.LENGTH_SHORT).show();
}
}
});

}
}

output:

You might also like