WebView Changes the Url Automatically - android

I am very new in App Development with Eclipse for android. I am able to make the buttons and links but am stuck at few places, and need help here.
The App has 3 button and a webview .
I am loading my Gmail account in webview by clicking the button . these buttonos are shortcuts like Inbox and Junk Box.So i should be able to check the mail by just clicking the inbox button.
The problems I face are:
When i click the inbox button for first time its ask for username and password but when i click login- It changes the webview Url to something else and ends with not found!! or error .I want to stop the Url from changing.
Webview Opens a new default webbrowser of my phone with the address bar on the top.
I want to hide that address bar and dont want to use the default browser to open the Page.
It has to be a smoth transaction without jumoing to URl page.
Hi Thank you for the response but i have few errors here,1) Since i am using three button , to activate the webview i have used the switch view ,therefore i am not able to define the above below is the sample code:
cancelbutton.setOnClickListener(new View.OnClickListener() {
private Activity result;
#SuppressWarnings("deprecation")
public void onClick(View v)
{
{ switch (v.getId())
{
case R.id.can_button3:
Intent myintent1 = new Intent
(Launcher.this,cancelbutton.class); Toast.makeText(Launcher.this, "Cancellation ",
Toast.LENGTH_SHORT).show();
setContentView(R.layout.activity_main);
WebView = (WebView) findViewById(R.id.webView1);
WebView.getSettings().setJavaScriptEnabled(true);
WebView.getSettings().setSavePassword(true);
WebView.loadUrl("https://gmail.com/inbox");break;
}
} }
}
);
junkBox.setOnClickListener(new View.OnClickListener() //Next Button{private Activity
result;

Create a CustomWebViewClient for your WebView. Override the following two functions:
shouldOverrideUrlLoading - To display the page within WebView
onReceivedSslError - To ignore SSL errors.
The code would look like this:
public class CustomWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed(); // Ignore SSL certificate errors
}
}
Add this WebViewClient to your WebView:
WebView webView = (WebView) result.findViewById(R.id.web_view);
webView.setWebViewClient(new CustomWebViewClient());

Related

My webview is opening the webpage in the default browser

So I just implemented a simple webview application in which i was loading the stackoverflow main page. Earlier it was working just fine but now as I click on some link it opens that link in the default browser. I have implemented and override the shouldoverrideUrlLoading method by creating my custom webViewClient class.
I know that there are various question ask like these but I am writing this question only because they don't work for me.
public class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(Uri.parse(url).getHost().endsWith(".com"))
return false;
Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new MyWebViewClient());
final customEditText editText = findViewById(R.id.urlEditText);
Button button = findViewById(R.id.enterButtonId);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
webView.loadUrl("https://"+editText.getText().toString().trim().toLowerCase());
}
});
}
You can use this:
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("url");
Just implement the web client and set it before loadUrl. The simplest way is:
WebView.setWebViewClient(new WebViewClient());
So When I was reading a tutorial https://www.journaldev.com/9333/android-webview-example-tutorial in this it is given that when shouldOverrideUrlLoading( ) method provides false then it url opens in our webview and if it returns true, then it will not load the page at all. So I think that earlier my code was working was because I was opening .com extensioned websites but when i open other extension website then it redirect to the default browser.

Android WebView Facebook Login (popup/redirection issues)

how is everyone?
I am working on a simple mobile application utilizing WebView. My website utilizes a layout giving it the appearance of a native app. I also allow users to login with their facebook account into my website. Here are my scenarios
** WORKS ** From PC/Chrome: Can access the mobile site URL, login with facebook which opens the pop-up dialog to either A.) Login, or B.) if user is FB logged in, prompt user to accept
** WORKS ** From Phone/Chrome: Can access the mobile site URL, login with facebook which opens a new tab with A.) Login, or B.) if user is FB logged in, prompt user to accept
** PROBLEM! ** From Phone/APK: Loads website in WebView, click login with facebook, opens facebook login page in the webview, but once I enter details and attempt to login, nothing happens. If I go to my home screen and reopen the app, it logs me in using the details I previously submitted
So basically what I am saying is that in the APP, once I leave my website URL and go to facebook's website to enter facebook details to login with it and press login -- it doesn't redirect back to my website for some reason. I've been plucking my hairs trying to find a solution to this, I've also searched extensively on here and other places but have not been able to find a workable solution, I'm sure many other people may be experiencing similar issues with facebook or other services
Here is my code (was originally much more simple, however I am now utilizing what I found in another topic covering this issue, however it still does not work -- I'd also like to mention that with this portion of code that I found, when TARGET_URL is loaded from a phone, the phone will redirect it to the mobile version of the website, which, since it's how the code functions, causes it to open up in a browser instead of my WebView)
I'm totally confused =/
public class MainActivity extends Activity {
/* URL saved to be loaded after fb login */
private static final String target_url="http://www.moneygirlsmusic.tv/app/index.php?do=/mobile/";
private static final String target_url_prefix="www.moneygirlsmusic.tv/app/index.php?do=/mobile/";
private Context mContext;
private WebView mWebview;
private WebView mWebviewPop;
private FrameLayout mContainer;
private long mLastBackPressTime = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// final View controlsView =
// findViewById(R.id.fullscreen_content_controls);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
mWebview = (WebView) findViewById(R.id.webview);
//mWebviewPop = (WebView) findViewById(R.id.webviewPop);
mContainer = (FrameLayout) findViewById(R.id.webview_frame);
WebSettings webSettings = mWebview.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setAppCacheEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setSupportMultipleWindows(true);
mWebview.setWebViewClient(new UriWebViewClient());
mWebview.setWebChromeClient(new UriChromeClient());
mWebview.loadUrl(target_url);
mContext=this.getApplicationContext();
}
private class UriWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
String host = Uri.parse(url).getHost();
Log.d("shouldOverrideUrlLoading", url);
if (host.equals(target_url_prefix))
{
// This is my web site, so do not override; let my WebView load
// the page
if(mWebviewPop!=null)
{
mWebviewPop.setVisibility(View.GONE);
mContainer.removeView(mWebviewPop);
mWebviewPop=null;
}
return false;
}
if(host.equals("m.facebook.com") || host.equals("www.facebook.com"))
{
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;
}
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler,
SslError error) {
Log.d("onReceivedSslError", "onReceivedSslError");
//super.onReceivedSslError(view, handler, error);
}
}
class UriChromeClient extends WebChromeClient {
#Override
public boolean onCreateWindow(WebView view, boolean isDialog,
boolean isUserGesture, Message resultMsg) {
mWebviewPop = new WebView(mContext);
mWebviewPop.setVerticalScrollBarEnabled(false);
mWebviewPop.setHorizontalScrollBarEnabled(false);
mWebviewPop.setWebViewClient(new UriWebViewClient());
mWebviewPop.getSettings().setJavaScriptEnabled(true);
mWebviewPop.getSettings().setSavePassword(false);
mWebviewPop.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
mContainer.addView(mWebviewPop);
WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
transport.setWebView(mWebviewPop);
resultMsg.sendToTarget();
return true;
}
#Override
public void onCloseWindow(WebView window) {
Log.d("onCloseWindow", "called");
}
}
I forgot to include my previous resources used to try to figure this out
Making facebook login work with an Android Webview
Making facebook login work with an Android Webview
Android WebView for Facebook Like Button
Android WebView for Facebook Like Button
In android 6 running in some mobiles you should add this to your java code to redirect . otherwise it doesn't redirect and nothing happens .
webview.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
});

Android Issue with WebView and overriding the url

Below is the code which we are using to load the WebView. Capturing the url and redirecting to a new activity page is done using the “shouldOverrideUrlLoading”, once the user click on log in button in html page in the webview. But when loading the page, the page is redirecting after the security check(redirecting to https page) and control is coming to the “shouldOverrideUrlLoading” function and its making the activity blank. If we remove the “shouldOverrideUrlLoading” function we can see the log in screen on the WebView. But we are not able to go to the new activity. I tried to catch the redirection url and load it on the
“shouldOverrideUrlLoading” function, but its not allowing to load the content. And I try to return true and false for different conditions form “shouldOverrideUrlLoading” function that also not working.
Can anyone suggest what I need to do to load the log in page in the WebView after the redirecting from the security check and override the url after log in and redirect to a new activity?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_page);
WebView webview = (WebView) findViewById(R.id.wvLogin);
setContentView(webview);
webview.setWebViewClient(new WebViewClient()
{
// Override URL
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
if(url.equals("http://Url which needs to override after login"))
{
Intent i = new Intent(getApplicationContext(), APImages.class);
startActivity(i);
}
return true;
}
});
webview.loadUrl("http://Login Page Url");
}

Url open in full screen instead of Webview

I am working on an Android project, and my task is to open a url in an embedded webview. Here is the code. When a button is clicked I open the url as follows:
yookosBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
linearLayout.setVisibility(View.GONE);
webview.setVisibility(View.VISIBLE);
webview.loadUrl("https://www.google.com.pk/");
}
});
1: When i open the google.com it is perfectly opened in embedded webview:
But when I replace the link with "http://videoshare.loveworldapis.com/commentredirect.php" url, the link is opened in full screen instead of embedded portion of webview as shown below:
Can you tell me what modification should I do to open the second website into embedded webview instead of full screen.
The WebView, by default, will open successive URLs by firing an intent, and opening the browser. To disable it so all URLs load in the WebView do this:
webView.setWebViewClient(new WebViewClient()
{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
return false;
}
});
I suspect your web site load involves an HTTP redirect, and that redirect is causing the browser to open.

Android Web View has trouble loading page when buttons are linked to a specific url

I have an app with a native header and a Webview below it. In this header I have a few links to different pages on the same site. For some reason when I click these links it takes almost twice as long as a link inside of the Webview takes to navigate to the same page.
The code that I am using to navigate to the specific page is below
btnCategory.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mWebView.loadUrl("http://mywebsiteurl.com/categories");
}
}
If I create the link to this page in HTML and put in on one of the pages it goes much faster. Is there a better way to set up this native button to navigate to the page in my Webview?
Thanks..
Try this:
btnCategory.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new MyWebViewClint());
mWebView.loadUrl("http://mywebsiteurl.com/categories");
});
}
public class MyWebViewClint extends WebViewClient
{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
}

Categories

Resources