how to display sent sms at the bottom

JunCainJunCain Member ✭✭✭

I want to display my inputted data at the bottom after I clicked the send button..

MainAcitvity.cs

using Android.App;
using Android.OS;
using Android.Support.V7.App;
using Android.Runtime;
using Android.Widget;
using Android.Content.PM;
using Android.Telephony;
using System;
using Android;
using Android.Content;
using Android.Support.V4.Content;
using Android.Support.V4.App;
using Android.Util;
using Android.Support.Design.Widget;
using Android.Views;
using Android.Text;
using System.Threading;

namespace MhylesOrderingApp
{
    [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
    public class MainActivity : AppCompatActivity, ActivityCompat.IOnRequestPermissionsResultCallback
    {      
        static readonly int REQUEST_SENDSMS = 0;
        private SmsManager _smsManager;
        private BroadcastReceiver _smsSentBroadcastReceiver, _smsDeliveredBroadcastReceiver;
        View layout;
        View hiddenLayout;

#pragma warning disable CS0618 // Type or member is obsolete
        static Android.App.ProgressDialog progress;
#pragma warning restore CS0618 // Type or member is obsolete

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState); 

            SetContentView(Resource.Layout.activity_main);

            SupportActionBar.Title = "Mhyles Order Messenger App";

            layout = FindViewById(Resource.Id.sample_main_layout);
            hiddenLayout = FindViewById(Resource.Id.hidden_layout);

            var smsBtn = FindViewById<Button>(Resource.Id.btnSend);
            var phoneNum = FindViewById<EditText>(Resource.Id.phoneNum);
            var smsDate = "";
            var smsCustomer = FindViewById<EditText>(Resource.Id.txtCustomer);
            var smsAddress = FindViewById<EditText>(Resource.Id.txtAddress);
            var smsCustNo = FindViewById<EditText>(Resource.Id.txtCustomerNo);
            var smsOrderProd = FindViewById<EditText>(Resource.Id.txtOrderedProduct);
            var smsProdQty = FindViewById<EditText>(Resource.Id.txtProductQty);
            var smsUnit = FindViewById<EditText>(Resource.Id.txtUnit);
            var smsAgentName = FindViewById<EditText>(Resource.Id.txtAgentName);
            var scrollView = FindViewById<ScrollView>(Resource.Id.scrollViewing);
            var btnNewOders = FindViewById<Button>(Resource.Id.btnNewOrders);

            DateTime now = DateTime.Now.ToLocalTime();
            smsDate = now.ToString();
            smsBtn.SetBackgroundResource(Resource.Drawable.buttonBackground);
            btnNewOders.SetBackgroundResource(Resource.Drawable.buttonDesign);
            phoneNum.SetBackgroundResource(Resource.Drawable.EditTextStyle);
            smsCustomer.SetBackgroundResource(Resource.Drawable.EditTextStyle);
            smsAddress.SetBackgroundResource(Resource.Drawable.EditTextStyle);
            smsCustNo.SetBackgroundResource(Resource.Drawable.EditTextStyle);
            smsOrderProd.SetBackgroundResource(Resource.Drawable.EditTextStyle);
            smsProdQty.SetBackgroundResource(Resource.Drawable.EditTextStyle);
            smsUnit.SetBackgroundResource(Resource.Drawable.EditTextStyle);
            smsAgentName.SetBackgroundResource(Resource.Drawable.EditTextStyle);

#pragma warning disable CS0618 // Type or member is obsolete
            progress = new Android.App.ProgressDialog(this);
#pragma warning restore CS0618 // Type or member is obsolete
            progress.Indeterminate = true;
            progress.SetProgressStyle(Android.App.ProgressDialogStyle.Spinner);
            progress.SetMessage("Sending.. Please wait!");
            progress.SetCancelable(false);
            _smsManager = SmsManager.Default;

            btnNewOders.Click += (s, e) =>
            {
                if (hiddenLayout.Visibility == ViewStates.Visible)
                {
                    hiddenLayout.Visibility = ViewStates.Gone;
                    scrollView.SetBackgroundResource(0);
                }
                else
                {
                    hiddenLayout.Visibility = ViewStates.Visible;
                    scrollView.SetBackgroundResource(Resource.Drawable.TextViewStyle);
                }
            };
            smsBtn.Click += (s, e) =>
            {
                progress.Show();
                if (TextUtils.IsEmpty(smsDate) || TextUtils.IsEmpty(phoneNum.Text) || TextUtils.IsEmpty(smsCustomer.Text)
                    || TextUtils.IsEmpty(smsAddress.Text) || TextUtils.IsEmpty(smsCustNo.Text) || TextUtils.IsEmpty(smsOrderProd.Text)
                    || TextUtils.IsEmpty(smsProdQty.Text) || TextUtils.IsEmpty(smsUnit.Text) || TextUtils.IsEmpty(smsAgentName.Text))
                {
                    new Thread(new ThreadStart(delegate
                    {
                        RunOnUiThread(() => Toast.MakeText(this, "Please fill out all the fields", ToastLength.Long).Show());
                        RunOnUiThread(() => progress.Hide());
                    })).Start();                  
                    return;
                }
                else
                {
                    var phone = phoneNum.Text;
                    var message = smsDate + "|" + smsCustomer.Text + "|" + smsAddress.Text + "|" + smsCustNo.Text
                                  + "|" + smsOrderProd.Text + "|" + smsProdQty.Text + "|" + smsUnit.Text + "|" + smsAgentName.Text;
                    var piSent = PendingIntent.GetBroadcast(this, 0, new Intent("SMS_SENT"), 0);
                    var piDelivered = PendingIntent.GetBroadcast(this, 0, new Intent("SMS_DELIVERED"), 0);
                    if ((int)Build.VERSION.SdkInt < 23)
                    {                        
                        _smsManager.SendTextMessage(phone, null, message, piSent, piDelivered);
                        new Thread(new ThreadStart(delegate
                        {
                            RunOnUiThread(() => Toast.MakeText(this, "Sending Orders error! please restart the app", ToastLength.Long).Show());
                            RunOnUiThread(() => progress.Hide());
                        })).Start();                     
                        return;
                    }
                    else
                    {
                        if (ActivityCompat.CheckSelfPermission(this, Manifest.Permission.SendSms) != (int)Permission.Granted)
                        {                          
                            RequestSendSMSPermission();            
                        }
                        else
                        {
                            _smsManager.SendTextMessage(phone, null, message, piSent, piDelivered);
                        }
                    }
                }
            };           
        }
        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
        {
            Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
            base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        }
        public void RequestSendSMSPermission()
        {
            Log.Info("MainActivity", "Message permission has NOT been granted. Requesting permission.");
            if (ActivityCompat.ShouldShowRequestPermissionRationale(this, Manifest.Permission.SendSms))
            {
                Log.Info("MainActivity", "Displaying message permission rationale to provide additional context.");
                Snackbar.Make(layout, "Message permission is needed to send SMS.",
                    Snackbar.LengthIndefinite).SetAction("OK", new Action<View>(delegate (View obj) {
                        ActivityCompat.RequestPermissions(this, new String[] { Manifest.Permission.SendSms }, REQUEST_SENDSMS);
                    })).Show();
            }
            else
            {
                ActivityCompat.RequestPermissions(this, new String[] { Manifest.Permission.SendSms }, REQUEST_SENDSMS);
            }
        }      
        protected override void OnResume()
        {                      
            base.OnResume();
            _smsSentBroadcastReceiver = new SMSSentReceiver();
            _smsDeliveredBroadcastReceiver = new SMSDeliveredReceiver();
            RegisterReceiver(_smsSentBroadcastReceiver, new IntentFilter("SMS_SENT"));
            RegisterReceiver(_smsDeliveredBroadcastReceiver, new IntentFilter("SMS_DELIVERED"));         
        }
> 
        protected override void OnPause()
        {
            base.OnPause();       
            UnregisterReceiver(_smsSentBroadcastReceiver);
            UnregisterReceiver(_smsDeliveredBroadcastReceiver);
        }
        //[BroadcastReceiver(Exported = true, Permission = "//receiver/@android:android.permission.SEND_SMS")]
        [BroadcastReceiver]
        public class SMSSentReceiver : BroadcastReceiver
        {
            public override void OnReceive(Context context, Intent intent)
            {
                switch ((int)ResultCode)
                {
                    case (int)Result.Ok:
                        Toast.MakeText(Application.Context, "SMS has been sent", ToastLength.Short).Show();
                        progress.Hide();
                        break;
                    case (int)SmsResultError.GenericFailure:
                        Toast.MakeText(Application.Context, "Generic Failure", ToastLength.Short).Show();
                        progress.Hide();
                        break;
                    case (int)SmsResultError.NoService:
                        Toast.MakeText(Application.Context, "No Service", ToastLength.Short).Show();
                        progress.Hide();
                        break;
                    case (int)SmsResultError.NullPdu:
                        Toast.MakeText(Application.Context, "Null PDU", ToastLength.Short).Show();
                        progress.Hide();
                        break;
                    case (int)SmsResultError.RadioOff:
                        Toast.MakeText(Application.Context, "Radio Off", ToastLength.Short).Show();
                        progress.Hide();
                        break;
                    default:
                        break;
                }
            }
        }
        //[BroadcastReceiver(Exported = true, Permission = "//receiver/@android:android.permission.SEND_SMS")]
        [BroadcastReceiver]
        public class SMSDeliveredReceiver : BroadcastReceiver
        {
            public override void OnReceive(Context context, Intent intent)
            {
                switch ((int)ResultCode)
                {
                    case (int)Result.Ok:
                        Toast.MakeText(Application.Context, "SMS Delivered", ToastLength.Short).Show();
                        progress.Hide();
                        break;
                    case (int)Result.Canceled:
                        Toast.MakeText(Application.Context, "SMS not delivered", ToastLength.Short).Show();
                        progress.Hide();
                        break;
                }
            }
        }
    }
}

activity_main.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:id="@+id/sample_main_layout">
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btnNewOrders"
        android:textColor="#249e3b"
        android:text="Send New Orders"/>
    <ScrollView
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:layout_margin="2dp"
           android:padding="2dp"
           android:id="@+id/scrollViewing">
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/hidden_layout"
            android:visibility="gone"
            android:orientation="vertical">
            <EditText
                android:id="@+id/phoneNum"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="phone"
                style="@style/edit_margin"
                android:hint="Recipient Number"/>
            <EditText
                android:id="@+id/txtCustomer"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                style="@style/edit_margin"
                android:hint="Customer Name"/>
            <EditText
                android:id="@+id/txtAddress"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                style="@style/edit_margin"
                android:lines="5"
                android:maxLines="10"
                android:gravity="left|top"
                android:inputType="text|textMultiLine"
                android:hint="Customer Address"/>
            <EditText
                android:id="@+id/txtCustomerNo"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="phone"
                style="@style/edit_margin"
                android:hint="Customer No."/>
            <EditText
                android:id="@+id/txtOrderedProduct"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                style="@style/edit_margin"
                android:hint="Ordered Product Name"/>
            <EditText
                android:id="@+id/txtProductQty"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                style="@style/edit_margin"
                android:hint="Quantity"/>
            <EditText
                android:id="@+id/txtUnit"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                style="@style/edit_margin"
                android:hint="Unit"/>
            <EditText
                android:id="@+id/txtAgentName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                style="@style/edit_margin"
                android:hint="Agent Name"/>
            <Button
                android:id="@+id/btnSend"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="#d9dae0"
                android:text="Send Orders" />
        </LinearLayout>
    </ScrollView>
</LinearLayout>

Answers

  • jezhjezh Member, Xamarin Team Xamurai
    edited March 7

    @JunCain
    Where do you want to put this message and could you please post more details about this,eg. effect image?

Sign In or Register to comment.