spot7.org logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories

How to show toast according to user input?


  1. Your app crashes on the line final int Me= Integer.parseInt(a);, since your EditText will be empty in the OnCreatemethod and an empty string cannot be parsed to an integer. You should catch the NumberFormatException
  2. You should get the text in the OnClickListener, because only then you have given the user the opportunity to enter a value in the EditText.

So your code will be:

sub.setOnClickListener(new
View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mEdit1 = (EditText)
findViewById(R.id.editText1);
                String a=
mEdit1.getText().toString();
                int me = 0;
                try{
                    me= Integer.parseInt(a);
                }catch(NumberFormatException e){
                    //me will be 0 if the user
does not enter a valid number
                    e.printStackTrace(); 
                }
                //setting minimum distance to be
10cm
                // the Me >= 0 check is not
needed, since if it is larger then 10
                //it is larger then 0
                if (Me > 10) {
                    LayoutInflater inflater =
getLayoutInflater();

                    View layout =
inflater.inflate(R.layout.toast, (ViewGroup)
findViewById(R.id.toast_layout_id));

                    // set a message
                    TextView text = (TextView)
layout.findViewById(R.id.text);
                    text.setText("Perfect!!");

                    // Toast configuration
                    Toast toast = new
Toast(getApplicationContext());
                   
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
                   
toast.setDuration(Toast.LENGTH_LONG);
                    toast.setView(layout);
                   
toast.getView().setBackgroundColor(Color.GREEN);
                    toast.show();
                } else {

                    LayoutInflater inflater =
getLayoutInflater();

                    View layout =
inflater.inflate(R.layout.toast, (ViewGroup)
findViewById(R.id.toast_layout_id));

                    // set a message
                    TextView text = (TextView)
layout.findViewById(R.id.text);
                    text.setText("Stop!! Stop!!");

                    // Toast configuration
                    Toast toast = new
Toast(getApplicationContext());
                   
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
                   
toast.setDuration(Toast.LENGTH_LONG);
                    toast.setView(layout);
                   
toast.getView().setBackgroundColor(Color.RED);
                    toast.show();
                    //}

                }
            }
        });

Categories : Android

Related to : How to show toast according to user input?
Toast is not applicable for the arguments
If want to use this as a pointer to an Activity in an anonymous inner class (e.g. OnClickListener), you have to use the Activity name before this: MyActivity.this, substitute with your Activity's name.

Categories : Java
Async Task and Toast - Android
In the your Task class, create a constructor and give it your context like this: private class Check_Loguin_Request { Context cx; public Check_Loguin_Request(Context cx) { this.cx=cx; } } Then in the OnPostExecute use this cx protected void onPostExecute(String result) { Toast.makeText(cx, "Task Finalized: " + result, Toast.LENGTH_SHORT).show(); } The use this class like this:

Categories : Android
Toast Notification From Background Task
Maybe you must use something like that ToastTemplateType toastTemplate = ToastTemplateType.ToastText02; XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(toastTemplate); XmlNodeList toastTextElements = toastXml.GetElementsByTagName("text"); toastTextElements[0].AppendChild(toastXml.CreateTextNode("Hello from the background task. ")); ToastNotification toast = new ToastNotificat

Categories : Windows
How can one ellicit a Toast from a Service to a Fragment
why you can't perform Toast from Service? if you really need to send data from Service to Fragment you should use service binding (example) then from Activity to Fragment

Categories : Android
Handling empty EditText with toast
You are getting an error, because your findViewById(...) isn't bound to the correct this. It should be outside of the onClick listener. Typically, I would do something like the following: public class MainActivity extends Activity { private EditText sun; private EditText mon; private EditText tue; private EditText wed; private EditText thu; private EditText fri; private EditText sat; private Ed

Categories : Android
Recently Add
I'm trying to create an onListItemClick even for a customized adapter. The array of items is found in res/values folder
No navigation/status bar touch listener exists, why?
z-index in Relative layout
java.lang.SecurityException: Permission Denial: getIntentSender() when using UiAutomation in a test
mvvmcross videoview URL binding
Android Emulator Unfortunately dev tools has stopped
Square Dagger error: class is exclusively members injected / cannot be scoped required
Installing a widget from an APP
How to stop onDraw() methods causing jerky scrolling of a ListView
How to find my Google Play Services Android BASE64_PUBLIC_KEY
Set style programatically does not work
How to get contacts from phonebook and sim but skype, facebook etc. in android
Where to put JUnit test in Android project
My Android Studio project keeps running my old project/application
how to avoid OUtOfMemory problems with create Bitmap
What tools use and how do continuous integration and test in Android with eclipse?
Converting drawable resource to Bitmap Android
Fragment Transaction show() does not show fragment on second time
Espresso - How to match ActionBar visibility?
Viewport setting ignored in Phonegap Android app, but only on certain phones?
how to open an activity with url scheme android
Android AsyncTask and Thread different behavior
How can one ellicit a Toast from a Service to a Fragment
GCMIntentService onRegistered not called
LinearLayout cropping but not expanding in Scrollview
Get a file from the application directory
How to set layout params using view's on touch listener?
How to disable Google Fit and revoke permissions from the app itself
Android Wear - receiving message from Wear without WearableListenerService?
How can I start new activity from another package?
© Copyright 2017 spot7.org Publishing Limited. All rights reserved.