SMS Intent - Android Studio - Kotlin
Open SMS Intent with Phone Number using Android Studio and Kotlin
Learn how to create a simple SMS sending feature in Android using Android Studio with Kotlin. This tutorial shows how to build a clean UI with an input field for phone numbers and a button that opens the SMS app with the entered number. A step-by-step guide with Kotlin code for beginners and developers moving from XML layouts to Compose.
val intent = Intent(Intent.ACTION_VIEW, Uri.parse("sms:" + Uri.encode(phone))) startActivity(intent)
Full Example:
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:gravity="center" android:orientation="vertical" android:padding="10dp" tools:context=".MainActivity1"> <!--EditText: Input Phone Number--> <EditText android:id="@+id/phoneEt" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Enter Phone Number" android:inputType="phone" /> <!--MaterialButton: Click to open sms intent--> <com.google.android.material.button.MaterialButton android:id="@+id/smsBtn" android:layout_width="match_parent" android:layout_height="wrap_content" android:minHeight="60dp" android:text="SMS" app:cornerRadius="8dp" /> </LinearLayout>
MainActivity.java
package com.technifysoft.myapplication import android.content.Intent import android.net.Uri import android.os.Bundle import android.widget.EditText import androidx.appcompat.app.AppCompatActivity import com.google.android.material.button.MaterialButton class MainActivity1 : AppCompatActivity() { private lateinit var phoneEt: EditText private lateinit var smsBtn: MaterialButton public override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main1) //init UI Views phoneEt = findViewById(R.id.phoneEt) smsBtn = findViewById(R.id.smsBtn) //handle smsBtn click smsBtn.setOnClickListener { openSmsIntent() } } private fun openSmsIntent() { //input phone number val phone = phoneEt.text.toString().trim { it <= ' ' } //open call/dialer intent val intent = Intent(Intent.ACTION_VIEW, Uri.parse("sms:" + Uri.encode(phone))) startActivity(intent) } }


Comments
Post a Comment