Click here to Skip to main content
15,884,879 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am new to android studio.I am doing toast message for my app,but i am unable to do this one.I have tried a lot but the toast message is not showing at all. Below is the code.Please suggest me.Thank you in advance.

HTML
Index.html

<!DOCTYPE html>
   <html>
    <head>
        <meta charset="utf-8" />
        <meta name="format-detection" content="telephone=no" />
        <meta name="msapplication-tap-highlight" content="no" />
         <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
        <link rel="stylesheet" type="text/css" href="css/index.css" />
        <title>Hello World</title>
        <script type="text/javascript" src="cordova.js"></script>
        <script type="text/javascript" src="toastPlugin.js"></script>
        <script>
          function toast()
            {
               // alert('hii');
               // shortToast("Short Toast Message Here...");
               longToast("Long Toast Message Here...");
            }
    </script>

    </head>
    <body>
        <div class="app">
            <h1>Apache Cordova</h1>
            <div id="deviceready" class="blink">
                <div onclick="toast();">click</div>

            </div>
        </div>
</body>
</html>



Java
ToastPlugin.java

package com.raddyx.myapplication;

        import org.apache.cordova.api.CallbackContext;
        import org.apache.cordova.api.CordovaPlugin;
        import org.json.JSONArray;
        import org.json.JSONException;

        import android.util.Log;
        import android.widget.Toast;

public class ToastPlugin extends CordovaPlugin {
    @Override
    public boolean execute(String action, JSONArray args,
                           CallbackContext callbackContext) throws JSONException {

        String message = args.getString(0);

        // used to log the text and can be seen in LogCat
        Log.d("Toast Plugin", "Calling the Toast...");
        Log.d("Toast Plugin", message);

        if (action.equals("shortToast")) {
            this.shortToast(message, callbackContext);
            return true;
        } else if (action.equals("longToast")) {
            this.longToast(message, callbackContext);
            return true;
        }
        return false;
    }

    private void shortToast(String message, CallbackContext callbackContext) {
        if (message != null && message.length() > 0) {
            Toast.makeText(cordova.getActivity().getApplicationContext(),
                    message, Toast.LENGTH_SHORT).show();
            callbackContext.success(message);


C#
In config.xml

 <feature name="ToastPlugin"> <param name="android-package" value="com.raddyx.plugins.ToastPlugin" /> </feature>


toastPlugin.js

JavaScript
          window.shortToast = function(str, callback) {   
    cordova.exec(callback, function(err) {
        callback('Nothing to echo.');
    }, "ToastPlugin", "shortToast", [ str ]);
};

window.longToast = function(str, callback) {
   alert(str);//Long Toast Message Here...
    cordova.exec(callback, function(err) { alert("Nothing to echo");
        callback('Nothing to echo.');
    }, "ToastPlugin", "longToast", [ str ]);
};
//Toast.makeText(getBaseContext(), "Reason can not be blank", Toast.LENGTH_SHORT).show();
Posted
Updated 19-Jan-16 20:52pm
v3

1 solution

You can find your answer Here:
android - How to create toast in phonegap? - Stack Overflow[^]
Or,
can use this awesome library from Github:
EddyVerbruggen/Toast-PhoneGap-Plugin - C++ - GitHub[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900