SMS Intent - Android Studio - Compose
Open SMS Intent with Phone Number using Android Studio and Jetpack Compose
Learn how to create a simple SMS sending feature in Android using Android Studio with Kotlin Jetpack Compose. 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.
Code Snippet
val intent = Intent(Intent.ACTION_VIEW, ("sms:" + Uri.encode(phone)).toUri()) context.startActivity(intent)
Full Example
MainActivity.kt
package com.technifysoft.myapplication import android.content.Context import android.content.Intent import android.net.Uri import android.os.Bundle import android.widget.Toast import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.PaddingValues import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.padding import androidx.compose.foundation.text.KeyboardOptions import androidx.compose.material3.Button import androidx.compose.material3.MaterialTheme import androidx.compose.material3.OutlinedTextField import androidx.compose.material3.Scaffold import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.text.input.KeyboardType import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import com.technifysoft.myapplication.ui.theme.MyApplicationTheme import androidx.core.net.toUri /** * MainActivityCompose is the main activity of the application, responsible for setting up the UI. */ class MainActivityCompose : ComponentActivity() { /** * Called when the activity is first created. This is where you should do all of your normal static set up: * create views, bind data to lists, etc. This method also provides you with a Bundle containing the activity's * previously frozen state, if there was one. */ override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { MyApplicationTheme { Scaffold( modifier = Modifier.fillMaxSize(), ) { innerPadding -> MainUI(innerPadding) } } } } } @Composable fun MainUI(innerPadding: PaddingValues) { // Get the current context val context = LocalContext.current var phone by remember { mutableStateOf("") } Column( modifier = Modifier .fillMaxSize() .padding(10.dp), verticalArrangement = Arrangement.Center, horizontalAlignment = Alignment.CenterHorizontally ) { // Phone input OutlinedTextField( value = phone, onValueChange = { phone = it }, label = { Text("Enter Phone Number") }, singleLine = true, keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Phone), modifier = Modifier.fillMaxWidth() ) Spacer(modifier = Modifier.height(16.dp)) // SMS button Button( onClick = { if (phone.isNotBlank()) { openSmsIntent(context, phone) } else { Toast.makeText(context, "Please enter a phone number", Toast.LENGTH_SHORT).show() } }, modifier = Modifier .fillMaxWidth() .height(60.dp), shape = MaterialTheme.shapes.medium // gives rounded corners ) { Text("SMS") } } } private fun openSmsIntent(context: Context, phone: String) { val intent = Intent(Intent.ACTION_VIEW, ("sms:" + Uri.encode(phone)).toUri()) context.startActivity(intent) } /** * GreetingPreview is a composable function for previewing the MainUI in Android Studio. * It is annotated with @Preview to enable live preview. * */ @Preview(showBackground = true) @Composable fun GreetingPreview() { MyApplicationTheme { MainUI(PaddingValues()) } }
Comments
Post a Comment