0% found this document useful (0 votes)
20 views4 pages

Lecture (Send SMS)

The document discusses how to send SMS messages from an Android application using an intent. It provides code snippets for the layout XML, manifest file, and Java code to send an SMS when a button is clicked, including validating the phone number and displaying a success message.

Uploaded by

Back Bencher
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)
20 views4 pages

Lecture (Send SMS)

The document discusses how to send SMS messages from an Android application using an intent. It provides code snippets for the layout XML, manifest file, and Java code to send an SMS when a button is clicked, including validating the phone number and displaying a success message.

Uploaded by

Back Bencher
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/ 4

Lecture (Sending Message in Android Studio)

How to send sms in android


We can send sms in android via intent. You need to write only 4 lines of code the send
sms in android.

Example of sending sms in android


activity_main.xml

Drag the 2 edittexts, 2 textviews and 1 button from the pallete, now the
activity_main.xml file will like this:

<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"
tools:context=".MainActivity">

<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="20dp"
android:ems="10" />

<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="26dp"
android:ems="10"
android:inputType="textMultiLine" />

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText1"
android:layout_alignBottom="@+id/editText1"
android:layout_toLeftOf="@+id/editText1"
android:text="Mobile No:" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText2"
android:layout_alignBottom="@+id/editText2"
android:layout_alignLeft="@+id/textView1"
android:text="Message:" />

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText2"
android:layout_below="@+id/editText2"
android:layout_marginLeft="34dp"
android:layout_marginTop="48dp"
android:text="Send SMS" />
</RelativeLayout>

Add Permission in Menifest File

Now add code in main java file


package com.example.actionbar;

import androidx.appcompat.app.AppCompatActivity;

import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


EditText mobileno, message;
Button sendsms;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mobileno = findViewById(R.id.editText1);
message = findViewById(R.id.editText2);
sendsms = findViewById(R.id.button1);

sendsms.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
String no = mobileno.getText().toString().trim();
String msg = message.getText().toString();

// Validate phone number


if (no.isEmpty() ||
!android.util.Patterns.PHONE.matcher(no).matches()) {
Toast.makeText(getApplicationContext(), "Invalid phone
number", Toast.LENGTH_SHORT).show();
return;
}

Intent intent = new Intent(getApplicationContext(),


MainActivity.class);
PendingIntent pi =
PendingIntent.getActivity(getApplicationContext(), 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);

SmsManager sms = SmsManager.getDefault();


sms.sendTextMessage(no, null, msg, pi, null);

Toast.makeText(getApplicationContext(), "Message Sent


successfully!", Toast.LENGTH_LONG).show();
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Now run and check Output

You might also like