0% found this document useful (0 votes)
7 views3 pages

Practical No 29

The document contains code for an Android application that allows users to send SMS messages. It includes an XML layout file for the user interface with two EditText fields for inputting a phone number and a message, as well as a Button to send the message. The Java code handles the button click event to send the SMS using the SmsManager, with error handling for empty fields.

Uploaded by

Soham Pandit
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)
7 views3 pages

Practical No 29

The document contains code for an Android application that allows users to send SMS messages. It includes an XML layout file for the user interface with two EditText fields for inputting a phone number and a message, as well as a Button to send the message. The Java code handles the button click event to send the SMS using the SmsManager, with error handling for empty fields.

Uploaded by

Soham Pandit
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/ 3

Practical No 29

Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<AbsoluteLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<EditText
android:id="@+id/editText"
android:layout_width="370dp"
android:layout_height="wrap_content"
android:layout_x="11dp"
android:layout_y="291dp"
android:ems="10"
android:hint="Enter number"
android:textSize="20dp"
android:inputType="textPersonName" />

<EditText
android:id="@+id/editText2"
android:layout_width="370dp"
android:layout_height="wrap_content"
android:layout_x="18dp"
android:layout_y="386dp"
android:ems="10"
android:hint="Enter message"
android:textSize="20dp"
android:inputType="textPersonName" />

<Button
android:id="@+id/button"
android:layout_width="365dp"
android:layout_height="wrap_content"
android:layout_marginLeft="60dp"
android:layout_marginRight="60dp"
android:layout_marginTop="20dp"
android:layout_x="26dp"
android:layout_y="517dp"
android:text="SEND" />
</AbsoluteLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java
package com.example.practicalno29a;

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 {


EditText phonenumber,message;
Button send;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
send=findViewById(R.id.button);
phonenumber=findViewById(R.id.editText);
message=findViewById(R.id.editText2);
send.setOnClickListener(new View.OnClickListener() {

public void onClick(View view) {


String number=phonenumber.getText().toString();
String msg=message.getText().toString();
try {
SmsManager smsManager=SmsManager.getDefault();

smsManager.sendTextMessage(number,null,msg,null,null);
Toast.makeText(getApplicationContext(),"Message
Sent",Toast.LENGTH_LONG).show();
}catch (Exception e)
{
Toast.makeText(getApplicationContext(),"Some
fields is Empty",Toast.LENGTH_LONG).show();
}
}
});
}
}
AndroidManifest.xml
<uses-permission android:name="android.permission.SEND_SMS"/>

You might also like