Last updated on July 31st, 2016 at 11:14 am
In this post i explain how we can send in app SMS and Email from our application. I just create a simple method to do that, and create a custom popup with custom view.
Method to show Custom POP
Step 1. Create a custom view for your pop up as shown bellow.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="5dp" android:padding="10dp" android:textSize="18dp" android:layout_marginBottom="20dp" android:text="Send notification via" android:textColor="#000" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:weightSum="3"> <Button android:id="@+id/sms" android:background="#eee" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:textSize="14dp" android:text="SMS" /> <Button android:id="@+id/both" android:background="#eee" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:textSize="14dp" android:text="Both" /> <Button android:id="@+id/email" android:background="#eee" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:textSize="14dp" android:text="Email" /> </LinearLayout> </LinearLayout>
Step 2. Method to show this view in custom popup. I am using AlertDialog Box for this
AlertDialog pop; // Invitation popup and email public void invitationBox(final String mobile, final String date, final String emailId) { // show popup and select Today/Tomorrow/Future Date AlertDialog.Builder invitation = new AlertDialog.Builder(Guest_list.this); // Get the layout inflater LayoutInflater inflater = Guest_list.this.getLayoutInflater(); // Check the value of sppiner if it set to future date View layoutDefault = inflater.inflate(R.layout.via_pop, null); Button semail = (Button) layoutDefault.findViewById(R.id.email); Button sboth = (Button) layoutDefault.findViewById(R.id.both); Button ssms = (Button) layoutDefault.findViewById(R.id.sms); // Send email semail.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { pop.dismiss(); senEmail(emailId, date); } }); // Send sms and email sboth.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { popMessage(mobile, iam, date); pop.dismiss(); senEmail(emailId, date); } }); // Send SMS and close POPUP ssms.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { popMessage(mobile, iam, date); pop.dismiss(); } }); invitation.setView(layoutDefault) .setPositiveButton("Cancel", null); // Set AlertDialog pop = invitation.create(); pop.setCanceledOnTouchOutside(false); pop.setOnShowListener(new DialogInterface.OnShowListener() { @Override public void onShow(DialogInterface dialog) { Button a = pop.getButton(AlertDialog.BUTTON_POSITIVE); // Send email a.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { pop.dismiss(); } }); } }); pop.show(); }
Step 3. Method i create to do this
public void senEmail(String emailId, String forD) { String message = "Helo budy!"; Intent email = new Intent(); email.putExtra(Intent.EXTRA_EMAIL, new String[]{emailId}); email.putExtra(Intent.EXTRA_SUBJECT, "Guest invitation for you."); email.putExtra(Intent.EXTRA_TEXT, message); email.setType("sms/rfc822"); //email.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT); startActivityForResult(Intent.createChooser(email, "Choose an Email client :"), 10); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 10) { // Email send } }
Send SMS
public void popMessage(String MobileNo, String name, String forDate) { try { String message = "Hello budy"; if (MobileNo.trim().length() != 0) { SmsManager smsManager = SmsManager.getDefault(); smsManager.sendTextMessage(MobileNo, null, message, null, null); } } catch (Exception e) { e.printStackTrace(); } }
That’s all. But dont forget to add permission in your manifest
<uses-permission android:name="android.permission.SEND_SMS" />
Thank you 🙂