Bikal Nepal
Bikal Nepal

Reputation: 487

How to make a phone number clickable to use in sms service in android?

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.

enter image description here

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:

enter image description 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);
    }
}

Upvotes: 0

Views: 1274

Answers (2)

ravi
ravi

Reputation: 11

First add a attribute android:clickable=true for that phone number textview

TextView  textView = (TextView) findViewById(R.id.phoneNumber);

Then add

textView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(this,SendSmsActivity.class);
       // send phone number to sms activity 

        intent.putExtra("phonenumber",textView.getText().toString())
        startActivity(intent);
    }
});

In SMS activity

Bundle bundle=getIntent().getExtras();

editText=(EditText)findViewById(R.id.phoneNumber);
editText.setText(bundle.getString("phonenumber"));

Upvotes: 1

h_patel
h_patel

Reputation: 744

you can use this

@Override
public void onReceive(Context context, Intent intent) {

    final Bundle bundle = intent.getExtras();
    try {
        if (bundle != null) {
            Object[] pdusObj = (Object[]) bundle.get("pdus");
            for (Object aPdusObj : pdusObj) {
                SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) aPdusObj);
                String senderAddress = currentMessage.getDisplayOriginatingAddress();
                String message = currentMessage.getDisplayMessageBody();

                Log.e(TAG, "Received SMS: " + message + ", Sender: " + senderAddress);

                String verificationCode = getVerificationCode(message);

                Log.e(TAG, "OTP received: " + verificationCode);

                setdata(verificationCode);

                Intent hhtpIntent = new Intent(context, OTPVerificationActivity.class);
                hhtpIntent.putExtra("otp", verificationCode);
                context.startService(hhtpIntent);
            }
        }
    } catch (Exception e) {
        Log.e(TAG, "Exception: " + e.getMessage());
    }
}

/**
 * Getting the OTP from sms message body
 * ':' is the separator of OTP from the message
 *
 * @param message
 * @return
 */
private String getVerificationCode(String message) {
    String code = null;
    code = message.substring(0, 6);
    return code;
}}

Upvotes: 1

Related Questions