android webview display the last seen page - android

I created a simple webview app and it works fine, except that every time I open the app it shows the main page and not the page that I was in last time, just like the regular browser
java file:
public class MainActivity extends Activity {
WebView webView;
public static final String PREFS_NAME = "SharedPreferences";
public static final String PREF_STRING = "http://www.google.com";
private SharedPreferences mPrefs;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webview);
// force web view to open inside application
webView.setWebViewClient(new MyWebViewClient());
openURL();
}
private void openURL() {
webView.loadUrl(PREF_STRING);
SharedPreferences settings = getSharedPreferences("StartPage", 0);
settings.getString("PREF_STRING", "http://www.google.com");
}
#Override
public void onResume() {
webView.loadUrl(mPrefs.getString(PREF_STRING, "http://www.google.com"));
}
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
xml file
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/progressbar_Horizontal"/>
</RelativeLayout>

Here's an example of how to save the last visited URL.
public class WebViewActivity extends Activity {
private WebView webView;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.webview_activity);
//The web view
webView = (WebView)findViewById(R.id.webViewEx1);
webView.setWebViewClient(new MyWebClient());
//Load the URL from saved url
webView.loadUrl(getUrl());
}
public void saveUrl(String url){
SharedPreferences sp = getSharedPreferences("SP_WEBVIEW_PREFS", MODE_PRIVATE);
Editor editor = sp.edit();
editor.putString("SAVED_URL", url);
editor.commit();
}
public String getUrl(){
SharedPreferences sp = getSharedPreferences("SP_WEBVIEW_PREFS", MODE_PRIVATE);
//If you haven't saved the url before, the default value will be google's page
return sp.getString("SAVED_URL", "http://google.com");
}
private class MyWebClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
//Save the last visited URL
saveUrl(url);
}
}
}
Basically all you have to do is save the last url inside SharedPreferences and retrieve it from there.
Hope it helps.

Related

display pdf file from google drive

now i'm trying this code
public class MainActivity extends AppCompatActivity {
private String myPdfUrl = "https://drive.google.com/open?id=1rquwQNdXeds39Rlb32isX3bjYghHitCV/view";
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webView=(WebView)findViewById(R.id.ve);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(myPdfUrl);
return true;
}
}
);
webView.loadUrl(myPdfUrl);
}
}
but it doesn't work
you try this code to make load PDF
i also implement in my application
String pdfURL = "your url";
webView.loadUrl("http://docs.google.com/gview?embedded=true&url=" + pdfURL);

How to add SwipeRefreshLayout to WebView

I need to pull from the top to refresh my web view
I found this on Android Developer site but i don't know how to use it
xml Code
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/swiperefresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Java code
mySwipeRefreshLayout.setOnRefreshListener(
new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
Log.i(LOG_TAG, "onRefresh called from SwipeRefreshLayout");
// This method performs the actual data-refresh operation.
// The method calls setRefreshing(false) when it's finished.
myUpdateOperation();
}
}
);
You gotta put your WebView inside SwipeRefreshLayout:
public class MainActivity extends AppCompatActivity {
WebView webView;
SwipeRefreshLayout swipeRefreshLayout;
String currentUrl = "https://news.ycombinator.com/";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView);
swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
webView.loadUrl(currentUrl);
webView.setWebViewClient(new MyWebViewClient());
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
webView.loadUrl(currentUrl);
}
});
}
public class MyWebViewClient extends WebViewClient{
#Override
public void onPageFinished(WebView view, String url) {
swipeRefreshLayout.setRefreshing(false);
currentUrl = url;
super.onPageFinished(view, url);
}
}
}
swipeRefreshLayout.setRefreshing(false) stops the animation.
To be able to refresh the page with the same URL running
you gotta save your link in ISharedPreferences when page first loaded
public override void OnPageFinished(WebView view, string url)
{
ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(this);
ISharedPreferencesEditor editor = prefs.Edit();
editor.PutString("MyURL", url);
editor.Commit();
}
when you load the URL for reffresh use the saved URL
string SaveURL = prefs.GetString("MyURL", "");
webView.loadUrl(SaveURL);
-- Other Solution is
Webview.Reload(); // This code Refreshes the current page loaded

facebook like on webview android shows blank screen after login

I utilize the facebook html like for my app to open a webview like this https://developers.facebook.com/docs/plugins/like-button/
The webview shows a Like and Share button, but after I login to facebook, it doesnt return to the Like and Share button, but a blank page, the share button works fine.
So how do I return to the facebook like url after logging in?
public class LikeFacebookActivity extends BaseActivity {
private WebView webView;
private final String URL = "facebookIDhere";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.like_facebook_webview);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
showLoading();
webView.setWebViewClient(new MyWebViewClient());
webView.loadUrl(URL);
ActionBar actionbar = getActionBar();
actionbar.setCustomView(R.layout.actionbar_top_like_facebook);
actionbar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
Button backButton = (Button) findViewById(R.id.buttonGeneralBack);
backButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
}
#Override
public void onBackPressed() {
finish();
overridePendingTransition(R.anim.animation_slide_from_left,
R.anim.animation_slide_to_right);
}
public class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.contains("something")) return true;
return false;
}
public void onPageFinished(WebView view, String url) {
hideLoading();
}
}
}
I have solved this, just use system.out.println to see which page does facebook load after loggin in
public class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.contains("something")) return true;
return false; //Default is to not override unless our condition is met.
}
public void onPageFinished(WebView view, String url) {
hideLoading();
//String webUrl = webView.getUrl();
//System.out.println(webUrl);
if(url.startsWith("https://www.facebook.com/plugins/close_popup.php#_=_")){
String redirectUrl = URL;
view.loadUrl(redirectUrl);
return;
}
super.onPageFinished(view, url);
}
}

Webview shouldoverrideurlloading doesn't work

I have this code in my app:
public class Home extends Activity{
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
final ProgressDialog progressBar;
if(isOnline()){
WebView webView = (WebView) findViewById(R.id.home_web);
webView.setBackgroundColor(Color.parseColor(getString(R.color.colore_bg)));
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setPluginsEnabled(true);
webView.setWebViewClient(new MyWebViewClient());
progressBar = ProgressDialog.show(this,getString(R.string.caricamento),getString(R.string.attendere));
webView.setWebViewClient(new WebViewClient(){
public void onPageFinished(WebView view, String url) {
if (progressBar.isShowing()) {
progressBar.dismiss();
}
}
});
webView.loadUrl("http://www.mysite.com/android.php");
}else{
Toast.makeText(this,getString(R.string.no_connessione),Toast.LENGTH_LONG).show();
}
}
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
System.out.println("here");
if (Uri.parse(url).getHost().equals("mysite.com")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
public boolean isOnline(){
ConnectivityManager cm=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
if(ni==null){
return false;
}
return ni.isConnected();
}
}
The shouldOverrideUrlLoading doesn't work, neither print the system.out, it seems to be never called. How can I repair this? I need to open all the link (except the main page www.mysite.com/iphone.php) in the default browser
You've set the WebViewClient twice, thus replacing the first one (shouldOverrideUrlLoading) with the second one (onPageFinished). Combine the two for it to work:
public class Home extends Activity{
private ProgressDialog progressBar;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
if(isOnline()){
progressBar = ProgressDialog.show(this,getString(R.string.caricamento),getString(R.string.attendere));
WebView webView = (WebView) findViewById(R.id.home_web);
webView.setBackgroundColor(Color.parseColor(getString(R.color.colore_bg)));
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setPluginsEnabled(true);
webView.setWebViewClient(new MyWebViewClient());
webView.loadUrl("http://www.mysite.com/android.php");
}else{
Toast.makeText(this,getString(R.string.no_connessione),Toast.LENGTH_LONG).show();
}
}
private class MyWebViewClient extends WebViewClient {
#Override
public void onPageFinished(WebView view, String url) {
if (progressBar != null && progressBar.isShowing()) {
progressBar.dismiss();
}
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
System.out.println("here");
if (Uri.parse(url).getHost().equals("mysite.com")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
}
(Please ignore the bad formatting :p)

How to set webview client?

I seen in android documentation where you use
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
To handle when items are clicked within a webview.
The only problem is with me, is that im setting the url in another method.
The HelloWebViewClient overrides that and doesnt use the url that the user can chose from. It just returns null..How could i over ride this method to use the url set by the user?
The URL is loaded when i use it in a regular method with the WebView browser; and then browser.loadUrl(String url)
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.shopping);
findIT = (Button)findViewById(R.id.findIT);
edittext = (EditText)findViewById(R.id.item);
type = (RadioGroup)findViewById(R.id.console);
site = (RadioGroup)findViewById(R.id.shopping_group);
findIT.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
item = edittext.getText().toString();
lookUp();
}
});
}
public void lookUp(){
browser = (WebView) findViewById(R.id.shoppingBrowser);
browser.getSettings().setJavaScriptEnabled(true);
Log.v(item, item);
getUserPreference();
browser.setWebViewClient(new HelloWebViewClient());
browser.loadUrl(url);
}
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String notuse) {
Log.v("shopping", url+" loaded");
return true;
}
}
public void getUserPreference(){
switch(type.getCheckedRadioButtonId()){
case R.id.item:
console = "item";
break;
case R.id.PS3:
console = "item";
break;
case R.id.item:
console = "item";
break;
}Log.v("item", console);
switch(site.getCheckedRadioButtonId()){
case R.id.store:
url = "http://www.gamestop.com/browse?nav=16k- "+ item +" " + console;
break;
case R.id.store:
url = "http://www.google.com/search?q="+item + " " + console+"&tbm=shop&hl=en&aq=0&oq=where+";
break;
case R.id.store:
url = "http://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Dvideogames&field-keywords="+item + " "+ console+"&x=0&y=0";
Log.v("shopping", url);
}
}
}
If you see what im trying to do the user gets to select what site they want to shop from. and from there i set it to the url.
If the user is choosing the URL from the same activity you can just reference the URL from the member variable instead of the URL from the parameter:
// Member variable stored to reflect user's choice
private String mUserUrl = "http://stackoverflow.com";
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// This line right here is what you're missing.
// Use the url provided in the method. It will match the member URL!
view.loadUrl(url);
return true;
}
}
This tells the WebviewClient that you've overloaded the URL loading (and in fact caused it to load the URL that you wish instead of the url supplied).
Here is a complete example of something I mocked up:
public class HelloWebViewActivity extends Activity {
private WebView mWebView = null;
private EditText mInputUrl = null;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mInputUrl = (EditText)findViewById(R.id.input_url);
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String url = mInputUrl.getText().toString();
mWebView.loadUrl(url);
}
});
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new HelloWebViewClient());
}
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
Hope this helps. If this works for you please mark the answer as accepted.
It isn't clear from your question whether this is the case, but it is possible that you did not set the WebViewClient of your WebView to the custom subclass that you created in your code. Somewhere in your code you should have something like:
browser.setWebViewClient(new HelloWebViewClient());
If you are only doing this with this one instance of WebView and your modifications to the WebViewClient are simple, then I would suggest that a more elegant way to accomplish this would be the following:
browser.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// Insert your code here
}
});
Edit:
Is it possible that the WebViewClient is actually a red herring? It appears to me that there is a problem with your switch statements in getUserPreference(). While the first switch statement seems unnecessary, the second one only ever sets the url to gamestop because all of your cases are the same.
You will set your custom WebViewClient for your webview and load the url in the webview as you we're doing before:
mWebView.setWebViewClient(new HellowWebViewClient());
mWebView.loadUrl(yourUrl);

Categories

Resources