I have a phone number in my xml layout and I want to make it clickable such that it would automatically get entered in sms service and I can send sms to the number.
Alinas.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:layout_marginLeft="10dp"
android:background="#ffffff">
<ImageView
android:layout_width="160dp"
android:layout_height="140dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:background="@drawable/alina_sbakerycafe"
android:id="@+id/imageView" />
<TextView
android:layout_width="2500dp"
android:layout_height="50dp"
android:text="Alina's Bakery Cafe"
android:textStyle="bold"
android:textSize="25dp"
android:gravity="end"
android:textAlignment="textEnd"
android:layout_alignTop="@+id/imageView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="42dp" />
<TextView
android:layout_width="300dp"
android:layout_height="50dp"
android:text="Address: New Baneswor, Kathmandu Phone No.:\t 9841123456"
android:textAlignment="center"
android:textSize="15dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="@android:dimen/thumbnail_width"
android:layout_height="@android:dimen/app_icon_size"
android:onClick="moveToActivityViewMenu"
android:background="@drawable/button"
android:text="View Menu"
android:textColor="#ffffff"
android:layout_marginBottom="83dp"
android:id="@+id/button"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
After I click in the phone number it should automatically get entered here:
SendSMSActivity.java
package com.golo.acer.mrestro4;
import android.app.Activity;
import android.content.Intent;
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 SendSmsActivity extends Activity {
Button sendSmsBtn;
EditText toPhoneNumber;
EditText smsMessageET;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_send_sms);
sendSmsBtn = (Button) findViewById(R.id.btnSendSMS);
toPhoneNumber = (EditText) findViewById(R.id.editTextPhoneNo);
smsMessageET = (EditText) findViewById(R.id.editTextSMS);
sendSmsBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendSms();
}
});
}
private void sendSms() {
String toPhone = toPhoneNumber.getText().toString();
String smsMessage = smsMessageET.getText().toString();
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(toPhone, null, smsMessage, null, null);
Toast.makeText(this, "SMS sent", Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
}
}
public void goToInbox(View v) {
Intent intent = new Intent(SendSmsActivity.this, ReceiveSMSActivity.class);
startActivity(intent);
}
public void moveToSmsActivity(View view) {
Intent intent = new Intent(this, SmsActivity.class);
startActivity(intent);
}
}