shaon007
shaon007

Reputation: 163

can not understand Toast.maketext()

I have the following code.

public class MyActivity extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        WebView webview = new WebView(this);
        webview.getSettings().setJavaScriptEnabled(true);

        //Add a client to the view
        webview.setWebViewClient(mClient);
        webview.loadUrl("http://www.google.com");
        setContentView(webview);
    }

    private WebViewClient mClient = new WebViewClient()
    {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url)
        {
            Uri request = Uri.parse(url);
            if(TextUtils.equals(request.getAuthority(), "www.google.com"))
            { //Allow the load
                return false;
            }
            Toast.makeText(MyActivity.this, "Sorry, buddy", Toast.LENGTH SHORT).show();
            return true;
        }
    };
}

Understood till the if condition but what does Toast.makeText() do? what happens by returning true?

Upvotes: 1

Views: 227

Answers (5)

Sino
Sino

Reputation: 886

getAuthority()-Gets the decoded authority part of this URI. if(TextUtils.equals(request.getAuthority(), "www.google.com"))- It will compare the athority part of request uri with String "www.google.com", If true its return false else it return true and it also shows the toast message(Toast is a object used to display short messages in android);

If WebViewClient is provided, return true means the host application handles the url, while return false means the current WebView handles the url. This method is not called for requests using the POST "method".

Upvotes: 0

paulsm4
paulsm4

Reputation: 121799

From the Android documentation:

http://developer.android.com/reference/android/widget/Toast.html

A toast is a view containing a quick little message for the user.

See also: http://developer.android.com/guide/topics/ui/notifiers/toasts.html

You said you understood the "if" condition: it determines whether to load the content back to this WebView, based on the URL passed in, keeping the user from leaving the Google site.

Upvotes: 1

Ssam
Ssam

Reputation: 277

If your webview couldn't load the webpage you can see the toast message. and then after returning true, your webview is gonna be finished.

Upvotes: 0

NitroG42
NitroG42

Reputation: 5356

You can find all the answer in a little place called The Documentation.

Here's the one for maketext : http://developer.android.com/reference/android/widget/Toast.html#makeText%28android.content.Context,%20java.lang.CharSequence,%20int%29

And the other for shoudOverrideUrl : http://developer.android.com/reference/android/webkit/WebViewClient.html#shouldOverrideUrlLoading%28android.webkit.WebView,%20java.lang.String%29

You can see that makeText is used to create a Toast, but you still need to call the method show() in order to display it, so your code is wrong.

As for shouldOverrideUrlLoading, returning True if the host application wants to leave the current WebView and handle the url itself, otherwise return false.

What your code do is "if the url requested is not www.google.com", don't load it and display an error toast.

Upvotes: 2

It shows a short message on your device that says "Sorry, buddy" appears if you couldnt load the page

Upvotes: 0

Related Questions