So I have a WebView inside of a LinearLayout with an EditText Box and a Button to make the application search for the web. When I click go on my button it takes me to the stock android browser. how do I make It so that it stays within my WebView when I click go?
here is my layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText
android:id="#+id/web_address"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="http://"/>
<Button
android:id="#+id/go"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/go"
android:gravity="right"/>
<WebView
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
here is my source:
package straightapp.com.Browser;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
public class Browser extends Activity {
WebView mWebView;
Button button;
EditText text;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://www.straightapp.com");
text=(EditText)findViewById(R.id.web_address);
button=(Button) findViewById(R.id.go);
button.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
openBrowser();
}
});
text.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode==KeyEvent.KEYCODE_ENTER){
openBrowser();
return true;
}
return false;
}
});
}
public void openBrowser(){
Uri uri=Uri.parse(text.getText().toString());
Intent intent=new Intent(Intent.ACTION_VIEW,uri);
startActivity(intent);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
Thanks
When I click go on my button it takes me to the stock android browser.
That's because that is what you told Android to do. startActivity() starts an activity.
how do I make It so that it stays within my WebView when I click go?
Call loadUrl() on your WebView instead of calling startActivity().
To be precise, you can try following change:
public void openBrowser(){
//Ensure that URL entered is a valid one
mWebView.loadUrl(text.getText().toString());
}
Change your xml file as below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".Browser"> <!--Just add this line -->
<EditText
android:id="#+id/web_address"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="http://"/>
<Button
android:id="#+id/go"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/go"
android:gravity="right"/>
<WebView
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
First declare the webview and progressbar
progressBar = (ProgressBar)findViewById(R.id.progressBar);
webView = (WebView) findViewById(R.id.webView);
webView.setWebViewClient(new myWebClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://www.google.com");
Copy and paste this code below onCreate method
public class myWebClient extends WebViewClient {
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
progressBar.setVisibility(View.VISIBLE);
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
}
Related
I make a android browser .But it doesn't support youtube video and other video also .Only sound play when i play the video and the video is black. so i need the solution of this problem .Thanks in advance .
This is my code
package com.example.ashraful.userinterface;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
public class Main2Activity extends Activity {
WebView web1;
EditText ed1;
Button bt1;
String Address;
String add;
ProgressBar pbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main2);
web1 = (WebView)findViewById(R.id.webView1);
ed1 = (EditText)findViewById(R.id.editText1);
bt1 = (Button)findViewById(R.id.button1);
pbar = (ProgressBar)findViewById(R.id.progressBar1);
pbar.setVisibility(View.GONE);
bt1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Address = "http://" + ed1.getText().toString();
WebSettings webSetting = web1.getSettings();
webSetting.setBuiltInZoomControls(true);
webSetting.setJavaScriptEnabled(true);
webSetting.setLoadsImagesAutomatically(true);
web1.setWebViewClient(new WebViewClient());
web1.loadUrl(Address);
}
});
}
public class WebViewClient extends android.webkit.WebViewClient
{
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
pbar.setVisibility(View.VISIBLE);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
ed1.setText(""); // clear
ed1.setText(view.getUrl());
super.onPageFinished(view, url);
pbar.setVisibility(View.GONE);
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK) && web1.canGoBack()) {
web1.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
public void onBackPressed() {
Intent myIntent = new Intent(Main2Activity.this, MainActivity.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);// clear back stack
startActivity(myIntent);
finish();
return;
}
}
And This is XML code
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main2"
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.example.ashraful.userinterface.Main2Activity">
<Button
android:id="#+id/button1"
android:layout_height="35dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="GO"
android:layout_width="45dp" />
<View
android:id="#+id/view12"
android:layout_width="wrap_content"
android:layout_height="3dp"
android:layout_alignBottom="#+id/button1"
android:layout_alignParentLeft="true"
android:background="#3bbdfa" />
<WebView
android:id="#+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/editText1"
android:layout_centerHorizontal="true" />
<ProgressBar
android:id="#+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:ems="10"
android:hint="Type Your URL Here"
android:textSize="14sp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="#+id/button1"
android:layout_toStartOf="#+id/button1" />
</RelativeLayout>
I make a webview in android . there is a webview and edit text and a go button . I want that when the webview change the edit text automatically change with the current url . example: when i type an url "www.google.com" i go to google if i go to youtube from google but my edit text stands as "www.google.com". but i want my edit text automaticlly change as the current webview
This is my java code
package com.example.ashraful.userinterface;
import android.app.Activity;
import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
public class Main2Activity extends Activity {
WebView web1;
EditText ed1;
Button bt1;
String Address;
String add;
ProgressBar pbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main2); web1 =(WebView)findViewById(R.id.webView1);
ed1 = (EditText)findViewById(R.id.editText1);
bt1 = (Button)findViewById(R.id.button1);
pbar = (ProgressBar)findViewById(R.id.progressBar1);
pbar.setVisibility(View.GONE);
bt1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Address = "http://" + ed1.getText().toString();
WebSettings webSetting = web1.getSettings();
webSetting.setBuiltInZoomControls(true);
webSetting.setJavaScriptEnabled(true);
webSetting.setLoadsImagesAutomatically(true);
web1.setWebViewClient(new WebViewClient());
web1.loadUrl(Address);
}
});
}
public class WebViewClient extends android.webkit.WebViewClient
{
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
pbar.setVisibility(View.VISIBLE);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
pbar.setVisibility(View.GONE);
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK) && web1.canGoBack()) {
web1.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
The this is the xml code
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main2"
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.example.ashraful.userinterface.Main2Activity">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="GO" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/button1"
android:layout_alignParentLeft="true"
android:ems="10"
android:hint="Type Your URL Here"
android:layout_toLeftOf="#+id/button1"
android:layout_toStartOf="#+id/button1" />
<View
android:id="#+id/view12"
android:layout_width="wrap_content"
android:layout_height="3dp"
android:layout_alignBottom="#+id/button1"
android:layout_alignParentLeft="true"
android:background="#3bbdfa" />
<WebView
android:id="#+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/editText1"
android:layout_centerHorizontal="true" />
<ProgressBar
android:id="#+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</RelativeLayout>
Try to set the Edittext in onPagefinished or/and in onPageStarted method:
ed1.setText(""); // clear
ed1.setText(view.getUrl()); // Set current url
Ok so I've been working on an app in Android Studio for school. I have a webview inside of a fragment. But in the app, when ever I open the fragment on my phone, the app crashes and my phone says "Unfortunately, LARKer App has stopped."
Here's the Fragment class:
import android.content.Intent;
import android.net.Uri;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
/**
* Created by Andrew on 12/29/2014.
*/
public class menu4_Fragment extends Fragment {
private WebView mWebView;
View rootview;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootview = inflater.inflate(R.layout.menu4_layout,container,false);
mWebView = (WebView) getView().findViewById(R.id.webView);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.loadUrl("foo");
mWebView.setWebViewClient(new MyWebViewClient());
return rootview;
}
public class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(Uri.parse(url).getHost().contentEquals("foo")) {
// This is my website, so do not override; ler 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;
}
}
}
And here's the XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="LARK: Login"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="35dp"
android:textSize="50dp" />
<WebView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/webView"
android:layout_below="#+id/textView"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
Here's an image of what I got on my phone:
Change this code
mWebView = (WebView) getView().findViewById(R.id.webView);
Like this, it will work
mWebView = (WebView) rootview.findViewById(R.id.webView);
You have to get the view's from your View Group.otherwise it will return error.
change this on your oncreateview() methed of your fragmanet
mWebView = (WebView) rootview.findViewById(R.id.webView);
Try this code,
WebView webViewInfo = (WebView) findViewById(R.id.webview);
webViewInfo.getSettings().setJavaScriptEnabled(true);
webViewInfo.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webViewInfo.setWebViewClient(new MYWEBCLIENT());
webViewInfo.loadData("LOAD_YOUR_URL", "text/html", "UTF-8");
And WebViewClient class implement on webview method.
private class MYWEBCLIENT extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
On keyboard back handle,
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webViewInfo.canGoBack()) {
webViewInfo.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
I hope this code help for you...!!
I've built a webview app that works fine.
I've tried to add admob to the bottom on app, but it doesnt show.
I've figured out if I set the main activity to
setContentView(R.layout.activity_main);
I get ad, but no webview
If I set the main activity to
setContentView(mWebview );
I get the webview but no ad.
How can I get both to show together?
mainActivity.java
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class MainActivity extends Activity {
private WebView mWebview ;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
super.onCreate(savedInstanceState);
mWebview = new WebView(this);
mWebview.getSettings().setJavaScriptEnabled(true);
final Activity activity = this;
mWebview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
});
mWebview .loadUrl("file:///android_asset/app/index.html");
//setContentView(mWebview);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(mWebview.canGoBack() == true){
mWebview.goBack();
}else{
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
}
activity_main.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
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=".MainActivity" >
<WebView
android:id="#+id/webView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<com.google.ads.AdView
android:id="#+id/adView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="MYIDMYIDMYID"
ads:loadAdOnCreate="true"
>
</com.google.ads.AdView>
</LinearLayout>
Make a pointer to the webView1 ex:
mWebview = (WebView)findViewById(R.id.webView1);
try this:
MainActivity.java
public class MainActivity extends Activity {
private WebView mWebview ;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//this will create a pointer to the webView1
mWebview = (WebView)findViewById(R.id.webView1);
mWebview.getSettings().setJavaScriptEnabled(true);
final Activity activity = this;
mWebview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
});
mWebview .loadUrl("file:///android_asset/app/index.html");
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(mWebview.canGoBack() == true){
mWebview.goBack();
}else{
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}}
main_activity.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<com.google.ads.AdView
android:id="#+id/adView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
ads:adSize="BANNER"
ads:adUnitId="MYIDMYIDMYID"
ads:loadAdOnCreate="true" >
</com.google.ads.AdView>
<WebView
android:id="#+id/webView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
</WebView>
I have created an edittext and a go button and a webview in single layout. When a person enters a url and clicks on go button, the webview should load the website content, Instead it is going to default browser to load website how should i slove this issue ?
This is my code
xml file
<FrameLayout 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" >
<WebView
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="50dip" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<EditText
android:id="#+id/etenterurl"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:inputType="textUri" />
<Button
android:id="#+id/buttongo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="3.0"
android:text="Go" />
</LinearLayout>
</FrameLayout>
java code :
import java.util.ArrayList;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.annotation.SuppressLint;
import android.app.Activity;
public class MainActivity extends Activity {
EditText etenter;
Button bgo;
WebView webview;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etenter= (EditText)findViewById(R.id.etenterurl);
bgo =(Button)findViewById(R.id.buttongo);
webview = (WebView)findViewById(R.id.webview);
bgo.setOnClickListener(new View.OnClickListener() {
#SuppressLint("SetJavaScriptEnabled")
#Override
public void onClick(View v) {
String url=etenter.getText().toString();
if(url.trim().length()>0){
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("http://"+url);
}
}
});
}
}
You have to use WebViewClient
wbView.setWebViewClient(new MyWebViewClient());
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
Log.v("uuuurl",url);
view.loadUrl(url);
return true;
}
}
See here for example
you should use WebViewClient
webview.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.d("TAG", " ==> "+url);
view.loadUrl(url);
return true;
}
});