To get the downloaded icon and title, implement our custom WebChromeClient, MyWebChromeClient, override onReceivedIcon() and onReceivedTitle() methods.
To update icon and title, call getSupportActionBar().setIcon(iconDrawable) and getSupportActionBar().setTitle(title). In order to call getSupportActionBar(), change our MainActivity extends ActionBarActivity, instead of Activity.
Call myBrowser.setWebChromeClient(new MyWebChromeClient()) in MainActivity to set 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;
public class MainActivity extends ActionBarActivity {
public class MyWebChromeClient extends WebChromeClient {
@Override
public void onReceivedTitle(WebView view, String title) {
Toast.makeText(getApplicationContext(),
"Title received: " + title,
Toast.LENGTH_LONG).show();
getSupportActionBar().setTitle(title);
super.onReceivedTitle(view, title);
}
@Override
public void onReceivedIcon(WebView view, Bitmap icon) {
Toast.makeText(getApplicationContext(),
"icon received",
Toast.LENGTH_LONG).show();
BitmapDrawable iconDrawable =
new BitmapDrawable(getResources(), icon);
getSupportActionBar().setIcon(iconDrawable);
super.onReceivedIcon(view, icon);
}
}
WebView myBrowser;
private final static String FIFA_HOME = "http://www.fifa.com/";
private final 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 before config/orientation changed
outState.putString(KEY_URL, myBrowser.getUrl());
super.onSaveInstanceState(outState);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_openinbrowser) {
Uri openUri = Uri.parse(myBrowser.getUrl());
Intent myIntent = new Intent(Intent.ACTION_VIEW, openUri);
startActivity(myIntent);
return true;
}
return super.onOptionsItemSelected(item);
}
}
Other files, activity_main.xml, /res/menu/main.xml, /res/values/strings.xml and AndroidManifest.xml, refer to the last post.
Download the files.
1 comment:
I had big troubles with saving the state of the url. Your code did really, really help me to solve the problem !. (I wasted 8 hours)
Post a Comment