Android webview browser edit text automatically change with webview change - android

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

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

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 can't load a specific URL using WebView

I can't load this specific url (http://sia.bogota.unal.edu.co/academia/) using WebView, but I can load other URLs. (See images below)
This is my code
Activity Class:
package co.luisfer.webview;
import android.app.Activity;
import android.app.ProgressDialog;
import android.net.http.SslError;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.webkit.SslErrorHandler;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
Activity activity;
private ProgressDialog progDailog;
private WebView webView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
activity = this;
progDailog = ProgressDialog.show(activity, "Loading", "Please wait...", true);
progDailog.setCancelable(false);
webView = (WebView) findViewById(R.id.webview);
webView.setWebViewClient(new MyWebViewClient());
WebSettings settings = webView.getSettings();
settings.setBuiltInZoomControls(false);
settings.setSupportZoom(false);
settings.setJavaScriptCanOpenWindowsAutomatically(true);
settings.setAllowFileAccess(true);
settings.setDomStorageEnabled(true);
webView.loadUrl("http://sia.bogota.unal.edu.co/academia");
}
private class MyWebViewClient extends WebViewClient {
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed();
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
progDailog.show();
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, final String url) {
progDailog.dismiss();
}
}
}
XML file:
<?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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<WebView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/webview"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true" />
</RelativeLayout>
I don't have any clue what's going on.
Can you help me?
Thanks in advance
PS: I have already set up the internet permission:
<uses-permission android:name="android.permission.INTERNET" />

Save state of edittext

I am making a simple app.
Here are the codes:
Main xml:
<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=".MainActivity" >
<Button
android:id="#+id/goButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#drawable/layout1"
android:text="#string/go" />
<EditText
android:id="#+id/urlField"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/goButton"
android:background="#drawable/layout1"
android:ems="10" android:inputType="textUri"
android:hint="#string/enter_school_name">
<requestFocus />
</EditText>
<ProgressBar
android:id="#+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/urlField" />
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_below="#+id/progressBar" />
Main Java:
package com.nextgenintl.plusportals;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
#SuppressLint("SetJavaScriptEnabled")
public class MainActivity extends Activity {
private WebView webView;
private EditText urlEditText;
private ProgressBar progress;
#Override
public void onBackPressed() {
if (webView.copyBackForwardList().getCurrentIndex() > 0) {
webView.goBack();
}
else {
// Your exit alert code, or alternatively line below to finish
super.onBackPressed(); // finishes activity
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
urlEditText = (EditText) findViewById(R.id.urlField);
webView = (WebView) findViewById(R.id.webView);
webView.setWebChromeClient(new MyWebViewClient());
progress = (ProgressBar) findViewById(R.id.progressBar);
progress.setMax(100);
Button openUrl = (Button) findViewById(R.id.goButton);
openUrl.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
String url = urlEditText.getText().toString();
String urlSchool = "https://www.plusportals.com/"+url;
urlSchool = urlSchool.replace(" ", "");
if (validateUrl(url)) {
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(urlSchool);
MainActivity.this.progress.setProgress(0);
}
}
private boolean validateUrl(String url) {
return true;
}
});
}
private class MyWebViewClient extends WebChromeClient {
#Override
public void onProgressChanged(WebView view, int newProgress) {
MainActivity.this.setValue(newProgress);
super.onProgressChanged(view, newProgress);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void setValue(int progress) {
this.progress.setProgress(progress);
}
}
So, I want to save the user input - the school name, so you don't have to type it every single time. But I can't really figure it out - can you please help me??? Thanks if you post me the full java file.
You can use the following:
SharedPreferences sp = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putString("clever_title", urlEditText.getText().toString());
editor.commit();
Then when you open your app the next time you can get:
SharedPreferences sp = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE);
urlEditText.text(sp.getString("clever_title", null);

How to load WebView in the same page

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;
}
});

WebView inside application?

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);
}

Categories

Resources