Implement Webchromeclient To Recall Downloaded Favicon In Addition To Championship Inwards Webview.

Posted on

In the postal service of “Create your ain App for FIFA World Cup“, nosotros purpose our default icon in addition to title. Now I desire to purpose the downloaded favicon in addition to championship from the target page equally our icon in addition to title.

To larn the downloaded icon in addition to title, implement our custom WebChromeClient, MyWebChromeClient, override onReceivedIcon() in addition to onReceivedTitle() methods.

To update icon in addition to title, call getSupportActionBar().setIcon(iconDrawable) and getSupportActionBar().setTitle(title). In gild to call getSupportActionBar(), alter our MainActivity extends ActionBarActivity, instead of Activity.

Call myBrowser.setWebChromeClient(new MyWebChromeClient()) inwards MainActivity to railroad train our custom WebChromeClient.

MainActivity.java

package com.example.myfifa;  import android.annotation.SuppressLint; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.drawable.BitmapDrawable; import android.net.Uri; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.Menu; import android.view.MenuItem; import android.webkit.WebChromeClient; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Toast;  populace degree MainActivity extends ActionBarActivity {    populace degree MyWebChromeClient extends WebChromeClient {    @Override   populace void onReceivedTitle(WebView view, String title) {    Toast.makeText(getApplicationContext(),      "Title received: " + title,      Toast.LENGTH_LONG).show();    getSupportActionBar().setTitle(title);     super.onReceivedTitle(view, title);   }    @Override   populace void onReceivedIcon(WebView view, Bitmap icon) {    Toast.makeText(getApplicationContext(),      "icon received",      Toast.LENGTH_LONG).show();        BitmapDrawable iconDrawable =      novel BitmapDrawable(getResources(), icon);        getSupportActionBar().setIcon(iconDrawable);    super.onReceivedIcon(view, icon);   }   }   WebView myBrowser;  mortal in conclusion static String FIFA_HOME = "http://www.fifa.com/";    mortal in conclusion static String KEY_URL = "KEY_URL";   @SuppressLint("SetJavaScriptEnabled")  @Override  protected void onCreate(Bundle savedInstanceState) {   super.onCreate(savedInstanceState);   setContentView(R.layout.activity_main);      //restore url after config/orientation changed   String openUrl = FIFA_HOME;   if (savedInstanceState != null) {    openUrl = savedInstanceState.getString(KEY_URL);   }    myBrowser = (WebView)findViewById(R.id.mybrowser);   myBrowser.getSettings().setJavaScriptEnabled(true);   myBrowser.setWebViewClient(new WebViewClient());   myBrowser.setWebChromeClient(new MyWebChromeClient());   myBrowser.loadUrl(openUrl);   }   @Override  protected void onSaveInstanceState(Bundle outState) {   //save url earlier config/orientation changed   outState.putString(KEY_URL, myBrowser.getUrl());   super.onSaveInstanceState(outState);  }   @Override  populace boolean onCreateOptionsMenu(Menu menu) {    // Inflate the menu; this adds items to the activeness bar if it is present.   getMenuInflater().inflate(R.menu.main, menu);   provide true;  }   @Override  populace boolean onOptionsItemSelected(MenuItem item) {   int id = item.getItemId();   if (id == R.id.action_openinbrowser) {        Uri openUri = Uri.parse(myBrowser.getUrl());    Intent myIntent = novel Intent(Intent.ACTION_VIEW, openUri);    startActivity(myIntent);        provide true;   }   provide super.onOptionsItemSelected(item);  }  } 

Other files, activity_main.xml, /res/menu/main.xml, /res/values/strings.xml and AndroidManifest.xml, advert to the in conclusion post.

 Now I desire to purpose the downloaded favicon in addition to championship from the target page equally our icon in addition to t Implement WebChromeClient to shout out back downloaded favicon in addition to championship inwards WebView.Download the files.