Open webpage inside my app after button is clicked - android

I have 5 buttons and i need to open different webpages after buttons is clicked. How to make it?
my java code for webview activity:
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebView extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView v, String url)
{
v.loadUrl(url);
return true;
}
}
And xml:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context="lv.shit.test.Sakums" >
<webview android:id="#+id/manswebview"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</webview>
</linearlayout>
What should I write on OnClick to open webpages in my webview?

You can load different pages on your webView object on the click.
as
webView.loadUrl("http://googlecom");
You can pas the url to the method loadUrl() under the webView object to preform the loading of different urls.

Call this method inside the onClick listener:
loadWebView(url);
public void loadWebView(String url){
/*
* Setting the options of my webView
*/
mWebView = (WebView)findViewById(R.id.manswebview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.getSettings().setDisplayZoomControls(false);
mWebView.getSettings().setUseWideViewPort(true);
mWebView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
mWebView.loadUrl(url);
mWebView.setWebChromeClient(new WebChromeClient() {
#Override
public void onReceivedTitle(WebView view, String title) {
super.onReceivedTitle(view, title);
}
#Override
public void onCloseWindow(WebView window) {
super.onCloseWindow(window);
}
});
mWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
//when finish loading page
public void onPageFinished(WebView view, String url) {
}
});
//done loading now render the content
this.setContentView(mWebView);
}

Related

WebView not working for Capture Image from Camera Or Select Image from gallery

I'm loading one URL in WebView, the URL page contains functionality of capturing image from device camera OR select image from gallery. If I open URL in Browser, everything works fine but, if I open the same URL in WebView then Camera and Gallery functionality do not works. I think I'm missing some WebView setting. Please check my below code and help me to solve my problem.
Below is my WebViewActivity--
public class WebViewActivity extends AppCompatActivity
{
WebView myWebView;
ProgressBar myProgress;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_webview);
findViews();
}
private void findViews()
{
myWebView= (WebView) findViewById(R.id.myWebView);
myProgress= (ProgressBar) findViewById(R.id.myProgress);
myWebView.setWebViewClient(new myWebClient());
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.getSettings().setLoadWithOverviewMode(true);
//Other webview settings
myWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
myWebView.setScrollbarFadingEnabled(false);
myWebView.getSettings().setBuiltInZoomControls(true);
myWebView.getSettings().setPluginState(WebSettings.PluginState.ON);
myWebView.getSettings().setAllowFileAccess(true);
myWebView.getSettings().setSupportZoom(true);
myWebView.loadUrl(SafeNestConstants.MEDLIFE_URL);
}
public class myWebClient extends WebViewClient
{
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
#RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request)
{
myProgress.setVisibility(View.VISIBLE);
view.loadUrl(request.getUrl().toString());
return true;
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
// TODO Auto-generated method stub
myProgress.setVisibility(View.VISIBLE);
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url)
{
// TODO Auto-generated method stub
super.onPageFinished(view, url);
myProgress.setVisibility(View.GONE);
}
}
}
Below is my activity_my_webview.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_terms_and_conditions"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.mypackage.WebViewActivity">
<WebView
android:id="#+id/myWebView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</WebView>
<ProgressBar
android:id="#+id/myProgress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:indeterminateDrawable="#drawable/progress_medium"
android:visibility="visible" />
</RelativeLayout>
Also let me know if I can provide more information for the same. Thank you.

Android webView always show url in tilte

I use this code to load WebView
web.setWebChromeClient(new WebChromeClient(){
#Override
public void onReceivedTitle(WebView view, String title) {
getWindow().setTitle(title); //Set Activity tile to page title.
}
});
web.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
This code works but when the title is empty , the webview will show the url of the page in the title. How to solve it?
Do like this...
web.setWebChromeClient(new WebChromeClient(){
#Override
public void onReceivedTitle(WebView view, String title) {
if(title!=null && title!="")
getWindow().setTitle(title); //Set Activity tile to page title.
else
getWindow().setTitle("");
}
});
May it help !

Android WebView Links Does not Open

How can I click on a link that is provided in the WebView data that I load?
The WebView data that is load is in HTML format as:
<P CLASS="western" ALIGN=JUSTIFY STYLE="margin-bottom: 0.14in">Further
here</P>
The WebView is placed inside a scroll view and its webClient is as:
webViewClient = new WebViewClient()
{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
#Override
public void onLoadResource(WebView view, String url)
{
}
};
webView = (WebView) findViewById(R.id.tos); // Defining the WebView for the terms and conditions and loading the required data
webView.setWebViewClient(webViewClient);
webView.loadData(getResources().getString(R.string.terms_of_service), "text/html", "UTF-8");
The problem is that when I click on the Link no action takes place. How can this be resolved?
use this code to open
WebView webView;//make sure to initialize
webView.setWebViewClient(webViewClient);
WebViewClient webViewClient= new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
return true;
}
#Override
public void onLoadResource(WebView view, String url){
if( url.equals("http://yoururl.com") ){
// do something
}
}
}

How Can I Continue Internet Link Action With My App?

I have a webview. My question is, when user clicks to a link in webview, how to continue with my app?
When user clicks to a link, this dialog box appearing:
How can I avoid this dialogbox and continue with my app?
WebView Code:
WebView webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl(url);
Edit (MYActivity İn Test project)
package com.mycompany.myapp5;
import android.app.*;
import android.os.*;
import android.webkit.*;
public class MainActivity extends Activity
{
WebView view;
String url="http://google.com";
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
view.setWebViewClient(new WebViewClient()
{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
}
}
This SO answer is exactly what you need.
The idea is to set a WebViewClient to your WebView and override shouldOverrideUrlLoading method like below:
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
I hope this helps you!
Here is the complete code from a test project that is running.
The xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.test.myapplication.MainActivity">
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"></WebView>
</RelativeLayout>
Code in the Activity:
WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webview);
String url="http://www.google.com";
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
webView.loadUrl(url);
}
This works fine in a 4.4 device. Please try this and let me know if you still have the issue.

How To Open Web Page Within My App?

This is my first question here. I know that this question has been asked before, but I didn't find an answer/solution that really explains the answer for a totally newbie like me.
I am creating an app with a linear layout that has a lot of buttons, each button should drive the user to a different web page. The buttons works well and every buttons goes to its specific web page, but in the default browser, not within the app.
This is my webview.xml file:
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
This is the WebViewActivity.java file:
public class WebViewActivity extends Activity {
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(
"http://egy-tech-droid.blogspot.com.eg/search/label/%D8%AA%D8%B7%D8%A8%D9%8A%D9%82%D8%A7%D8%AA%20%D8%AD%D8%B5%D8%B1%D9%8A%D8%A9");
}
I added the internet permission in the Manifest file:
<uses-permission android:name="android.permission.INTERNET" />
This opens the web page but in the default browser of the device and I want it to open inside my app. Any help? (please give me a detailed answer/explanation)
Add this to your code
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
view.loadUrl(url);
return true;
}
});
You need to set up a WebViewClient in order to override that behavior (opening links using the web browser).
Use this;
webview.setWebViewClient(new WebViewClient());
Android documentation says:
public void setWebViewClient (WebViewClient client)
Sets the WebViewClient that will receive various notifications and
requests. This will replace the current handler.
Enjoy full code :
Oncreate () :
webView = (WebView) findViewById(R.id.webView1);
if(Constants.isNetworkAvailable(mContext)){
webView.setWebViewClient(new MyWebViewClient());
webView.setWebChromeClient(new WebChromeClient() );
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setPluginState(PluginState.ON);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setSupportZoom(true);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.setScrollbarFadingEnabled(false);
webView.setInitialScale(30);
webView.loadUrl(url);
}else{
Toast.makeText(mContext, Constants.msgNoInternet, Toast.LENGTH_LONG).show();
}
MyWebViewClient :
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
if (!pDialog.isShowing()) {
pDialog.show();
}
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
//view.loadUrl(url);
System.out.println("on finish");
if (pDialog.isShowing()) {
pDialog.dismiss();
}
}
}
Kotlin version of Sunny's answer
webView.webViewClient = object : WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
view?.loadUrl(request?.url.toString())
return true
}
}
You can't talk about the Web nowadays without considering Javascript. By default, its use in a WebView is not active. To enable Javascript just insert these lines of code:
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
I hope this will help you.
In your web view layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/relay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#c7bbac">
<ImageView
android:id="#+id/txtmain"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="#drawable/topbar50" />
<ImageView
android:id="#+id/backbutn"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:adjustViewBounds="true"
android:paddingTop="2dp"
android:src="#drawable/backbtn" />
</RelativeLayout>
<WebView
android:id="#+id/webView1"
android:layout_below="#+id/relay"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
Webview Button Onclick:
webbutton = (ImageView) findViewById(R.id.web);
webbutton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(getApplicationContext(), WebViewActivity.class);
startActivity(intent);
}
});
Webview Activity:
public class WebViewActivity extends Activity {
private WebView webViewurl;
ImageView back;
AndroidInterface AMW = AndroidInterface.GetInstance();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
back = (ImageView) findViewById(R.id.backbutn);
webViewurl = (WebView) findViewById(R.id.webView1);
webViewurl.getSettings().setJavaScriptEnabled(true);
webViewurl.getSettings().setBuiltInZoomControls(true);
final Activity activity = this;
webViewurl.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
});
webViewurl.loadUrl("http://example.com");
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
}
webView = (WebView) findViewById(R.id.youtubelink);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("your url");
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("http://www.google.co.in");
this code works fine...
Above code opens the link in your App.

Categories

Resources