Search code examples
androidandroid-studiokotlinsmssend

Button to send contents of edit text to email or text using Kotlin


I am a complete newbie to Kotlin and Android Studio but I have decided that I want to write an app for use at my work. I can write what I need in Excel and VB but unfortunately Macros are not supported on Android mobile devices, hence why I would like to write an app instead. I have worked out how to create pages (activities) and how to navigate back and forth between them (roughly), However, my ultimate goal is to capture the data that the user would enter into the text fields and then once they clicked the submit button it would then send the data they had entered via SMS to a predefined phone number (S) or alternatively as a text file to an email address. Any help would be greatly appreciated. Please let me know what you need me to post to enable any help Thanks in advance Code screenshot

package com.test.warehousecontrol
import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.*
import androidx.core.app.ActivityCompat
import android.telephony.SmsManager
import kotlinx.android.synthetic.main.activity_second.*

    class SecondActivity : AppCompatActivity()


    {
    override fun onCreate(savedInstanceState: Bundle?)


    {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_second)
       //  if(ActivityCompat.checkSelfPermission(this,Manifest.permission.SEND_SMS)!=PackageManager.PERMISSION_GRANTED)
        val backbut = findViewById<Button>(R.id.backbut)
        backbut.setOnClickListener {
            val intent = Intent(this, MainActivity::class.java)
            startActivity(intent)
        }


        }
        private fun getSendSmsIntent(phoneNumber: String, content: String?): Intent? {
            val uri = Uri.parse("smsto:$phoneNumber")
            val intent: Intent = Intent(Intent.ACTION_SENDTO, uri)
            intent.putExtra("sms_body", content)
            return getIntent(Intent, true)
            //  return getIntent(intent, true)

            val sub1: Button = findViewById<Button>(R.id.sub1)
            sub1.setOnClickListener {

                val cust: String = cust.text.toString()
                val reg: String = reg.text.toString()
                val pal: String = pal.text.toString()
                val data: String =
                    "CUST : ".plus(cust).plus("\n").plus("REG : ").plus(reg).plus("\n").plus("PAL : ")
                        .plus(pal)
                startActivity(getSendSmsIntent("1234567", data))

            }
        }

        }

Solution

  • To send an SMS :

    val submit: Button = findViewById<Button>(R.id.sub1)
        submit.setOnClickListener {
            val cust: String = custTextField.text.toString()
            val reg: String = regTextField.text.toString()
            val pal: String = palTextField.text.toString()
            val data:String = "CUST : ".plus(cust).plus("\n").plus("REG : ").plus(reg).plus("\n").plus("PAL : ").plus(pal)
            startActivity(getSendSmsIntent("YOUR PHONE NUMBER HERE", data))
        }
    

    To send an email :

    val submit: Button = findViewById<Button>(R.id.sub1)
            submit.setOnClickListener {
                val cust: String = custTextField.text.toString()
                val reg: String = regTextField.text.toString()
                val pal: String = palTextField.text.toString()
                val data:String = "CUST : ".plus(cust).plus("\n").plus("REG : ").plus(reg).plus("\n").plus("PAL : ").plus(pal)
                startActivity(Intent.createChooser(getEmailIntent("[email protected]", "SUBJECT", data), "Send mail"))
            }
    

    private fun getSendSmsIntent(phoneNumber: String, content: String?): Intent? {
            val uri = Uri.parse("smsto:$phoneNumber")
            val intent = Intent(Intent.ACTION_SENDTO, uri)
            intent.putExtra("sms_body", content)
            return getIntent(intent, true)
        }
    
        private fun getEmailIntent(email: String, subject: String?, content: String?): Intent? {
            val intent = Intent(Intent.ACTION_SEND)
            intent.type = "message/rfc822"
            intent.putExtra(Intent.EXTRA_EMAIL, arrayOf(email))
            intent.putExtra(Intent.EXTRA_SUBJECT, subject)
            intent.putExtra(Intent.EXTRA_TEXT, content)
            return getIntent(intent, true)
        }
    
    
        private fun getIntent(intent: Intent, isNewTask: Boolean): Intent? {
            return if (isNewTask) intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) else intent
        }
    

    Your final Activity would be like this :

    import android.content.Intent
    import android.net.Uri
    import android.os.Bundle
    import android.widget.Button
    import androidx.appcompat.app.AppCompatActivity
    
    
    class SecondActivity : AppCompatActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_second)
            //  if(ActivityCompat.checkSelfPermission(this,Manifest.permission.SEND_SMS)!=PackageManager.PERMISSION_GRANTED)
            val backbut = findViewById<Button>(R.id.backbut)
            backbut.setOnClickListener {
                val intent = Intent(this, MainActivity::class.java)
                startActivity(intent)
            }
            val sub1: Button = findViewById<Button>(R.id.sub1)
            sub1.setOnClickListener {
                val cust: String = cust.text.toString()
                val reg: String = reg.text.toString()
                val pal: String = pal.text.toString()
                val data: String =
                    "CUST : ".plus(cust).plus("\n").plus("REG : ").plus(reg).plus("\n").plus("PAL : ")
                        .plus(pal)
                startActivity(getSendSmsIntent("1234567", data))
            }
        }
    
        fun getSendSmsIntent(phoneNumber: String, content: String?): Intent? {
            val uri = Uri.parse("smsto:$phoneNumber")
            val intent = Intent(Intent.ACTION_SENDTO, uri)
            intent.putExtra("sms_body", content)
            return getIntent(intent, true)
        }
        private fun getIntent(intent: Intent, isNewTask: Boolean): Intent? {
            return if (isNewTask) intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) else intent
        }
    }