• Add the following code to res/layout/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:orientation="vertical" android:padding="4dp" tools:context=".MainActivity"> <EditText android:hint="Enter mobile number" android:id="@+id/editTextNum" android:layout_width="match_parent" android:layout_height="wrap_content"/> <EditText android:hint="Enter your message" android:id="@+id/editTextMsg" android:layout_width="match_parent" android:layout_height="wrap_content"/> <Button android:id="@+id/btnSendMsg" android:onClick="SendSMS" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Send Message"/> </LinearLayout>
• Add the following code to src/MainActivity.java
import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; import android.Manifest; import android.content.pm.PackageManager; import android.os.Bundle; import android.telephony.SmsManager; import android.text.TextUtils; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends AppCompatActivity { Button btnSend; EditText editTextNum, editTextMsg; private static final int PERMISSION_REQUEST = 101; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editTextNum = findViewById(R.id.editTextNum); editTextMsg = findViewById(R.id.editTextMsg); btnSend = findViewById(R.id.btnSendMsg); } public void SendSMS(View view) { int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS); if (permissionCheck == PackageManager.PERMISSION_GRANTED){ MyMessage(); } else { ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.SEND_SMS}, PERMISSION_REQUEST); } } private void MyMessage() { String myNumber = editTextNum.getText().toString().trim(); String myMsg = editTextMsg.getText().toString().trim(); if (myNumber.equals("") || myMsg.equals("")){ Toast.makeText(this, "Field cannot be empty", Toast.LENGTH_SHORT).show(); } else { if (TextUtils.isDigitsOnly(myNumber)){ SmsManager smsManager = SmsManager.getDefault(); smsManager.sendTextMessage(myNumber, null, myMsg, null, null); Toast.makeText(this, "Message Sent", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "Please enter the correct number", Toast.LENGTH_SHORT).show(); } } } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == PERMISSION_REQUEST) { if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { MyMessage(); } else { Toast.makeText(this, "You don't have required permission to send a message", Toast.LENGTH_SHORT).show(); } } } }
• Add the following code to androidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="app.com.sample"> <uses-permission android:name="android.permission.SEND_SMS"/> <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> </application></manifest>
OutPut