I want to hide the URL bar from the browser permanently, and it should not appear even if the person gets redirected from one page to another.
Here is my .java code:
package com.example.com.android.royalcastleapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.support.v4.app.NavUtils;
public class Bookinghome extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bookinghome);
//Get a reference of WebView holder
WebView webview = (WebView) this.findViewById(R.id.webview);
//Get the settings
WebSettings websettings = webview.getSettings();
//Enable Javascript
websettings.setJavaScriptEnabled(true);
//Make the zoom controls visible
websettings.setBuiltInZoomControls(true);
//Load the default url.
webview.loadUrl("http//okRoom.aspx");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_bookinghome, menu);
return true;
}
}
And here is my layout:
<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" >
<WebView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:padding="#dimen/padding_medium"
android:text="#string/hello_world"
tools:context=".Bookinghome"
android:id= "#+id/webview"/>
</RelativeLayout>
If anyone could suggest any changes I could make, that would be great!
Your application starts default browser.
You need to use WebViewClient.shouldOverrideUrlLoading like described here: https://stackoverflow.com/a/2379054/2183804
Related
Good morning,
I created an application with the code below, it allows to display my site with webview
Everything works very well except for a small problem, the message Loading appears for a while and disappears, I want that remains until the total loading of the whole page (images ...).
Thanks for your help
mainactivity.java
package com.example.monsport;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import org.w3c.dom.Text;
public class MainActivity extends AppCompatActivity {
private WebView WebView;
ProgressBar progressBar;
ProgressDialog progressDialog;
#Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar=(ProgressBar)findViewById(R.id.progress) ;
progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Loading wait....");
WebView = (WebView) findViewById(R.id.WebView);
WebView.getSettings().setJavaScriptEnabled(true);
WebView.setWebViewClient(new WebViewClient());
WebView.loadUrl("https://www.mywebsite.com/apps/");
WebView.setWebChromeClient(new WebChromeClient(){
#Override
public void onProgressChanged(android.webkit.WebView view, int newProgress) {
progressBar.setVisibility(View.VISIBLE);
progressBar.setProgress(newProgress);
progressDialog.show();
if (newProgress == 100){
progressBar.setVisibility(View.GONE);
progressDialog.dismiss();
}
super.onProgressChanged(view, newProgress);
}
});
}
//This method require to use back button if want to go previous web page
#Override public void onBackPressed() {
if (WebView.canGoBack()) {
WebView.goBack();
} else {
super.onBackPressed();
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:layout_gravity="center"
android:gravity="center">
<ProgressBar
android:layout_width="match_parent"
android:layout_height="8dp"
android:id="#+id/progress"
style="?android:attr/progressBarStyleHorizontal"
android:layout_marginTop="-3dp"
android:progress="20"
android:visibility="gone">
</ProgressBar>
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/WebView">
</WebView>
</androidx.constraintlayout.widget.ConstraintLayout>
I tried the loading code but it appears constant for all pages and disappears even if the page has not been completely loaded !
Thank you
I am new to android and cannot find a solution online. I have a navigation drawer that leads to the fragment 'RecentNews', which is related to it's XML 'fragment_recent_news'. I have a WebView (webview) setup in the XML, and have set up the fragment so that it should load a page, but it does not. What am I doing incorrectly? I am unsure if the way to properly do this has changed in the past year because nothing online has worked. Thanks
fragment_recent_news(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" tools:context="com.hardingsoftware.hrcfitness.RecentNews">
<WebView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/webview"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
RecentNews:
package com.hardingsoftware.hrcfitness;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class RecentNews extends Fragment {
WebView webView;
public RecentNews() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_recent_news, container, false);
WebView myWebView = (WebView) rootView.findViewById(R.id.webview);
myWebView.loadUrl("www.hrcfitness.com/recent-news/");
return rootView;
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
EDIT: I have the page loading now, however I now have another issue, where the webview seems to ignore the relative layout and part of it is lying under the main bar.
In the eclipse/android AVD, I get "Unfortunately, has stopped"
Help please.. here is the code
I just follow the other codes...
I tried everything I can I just don't know
what to do ...
help help help... not a total programmer ...
package com.somedomain.animatedinteractive;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.webkit.WebSettings.PluginState;
import android.webkit.WebView;
public class MainActivity extends Activity {
WebView mWebView;
/** Called when the activity is first created. */
#SuppressLint("SetJavaScriptEnabled")
#SuppressWarnings("deprecation" )
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mWebView = (WebView) findViewById(R.id.action_settings);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setPluginState(PluginState.ON);
mWebView.loadUrl("file:///android_asset/Parable_Book.swf");
mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
mWebView.setBackgroundColor(Color.parseColor("#000000"));
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
Your main layout does not have a WebView with id action_settings. Change
mWebView = (WebView) findViewById(R.id.action_settings);
to
mWebView = (WebView) findViewById(R.id.webview);
Also, when getting an "unfortunately app has stopped" message, it's a good idea to have a look at the exception stacktrace in logcat. Include it in your question, too.
package com.somedomain.animatedinteractive;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.webkit.WebSettings.PluginState;
import android.webkit.WebView;
public class MainActivity extends Activity {
WebView mWebView;
/** Called when the activity is first created. */
#SuppressLint("SetJavaScriptEnabled")
#SuppressWarnings("deprecation" )
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setPluginState(PluginState.ON);
mWebView.loadUrl("file:///android_asset/Parable_Book.swf");
mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
mWebView.setBackgroundColor(Color.parseColor("#000000"));
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
This is my first app, it is a web app, it simply loads a webView to a url where stuff happens.
It works fine in android browser(gingerbread), but in Chrome(ICS, JB) it was going back to the initial webView url when the device is rotated. In gingerbread you account for this by setting android:configChanges="keyboard|keyboardHidden|orientation" and overriding onConfigurationChanged.
After searching the web, I re-wrote my original activity as per the example here: http://www.devahead.com/blog/2012/01/preserving-the-state-of-an-android-webview-on-screen-orientation-change/ , in which you essentially handle the re-orientation yourself.
However now my app is crashing when I rotate the device.
Testing with Android 4.1.2.
Anyone see anything off with my code? Sorry if the answer is obvious, but I've been stuck on this bug for a couple of days now. Maybe I just need another set of eyes on it. Thanks in advance!
Note: the line #SuppressWarnings("deprecation") is not in the example, it was suggested by the editor in order to compile.
The Main Activity:
package com.example.xxx.xxx;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.content.res.Configuration;
import android.view.Menu;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
#SuppressLint("SetJavaScriptEnabled")
public class MainActivity extends Activity {
protected FrameLayout webViewPlaceholder;
protected WebView myWebView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initUI();
}
#SuppressWarnings("deprecation")
protected void initUI() {
webViewPlaceholder = ((FrameLayout)findViewById(R.id.webViewPlaceholder));
if (myWebView == null){
//Create It
myWebView = new WebView(this);
myWebView.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setSupportZoom(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setLoadsImagesAutomatically(true);
myWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
myWebView.setScrollbarFadingEnabled(true);
myWebView.setWebViewClient(new WebViewClient());
myWebView.setWebChromeClient(new WebChromeClient());
myWebView.setInitialScale(1);
myWebView.loadUrl("http://www.theurl.com");
}
webViewPlaceholder.addView(myWebView);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
#Override
public void onConfigurationChanged(Configuration newConfig){
if (myWebView != null) {
webViewPlaceholder.removeView(myWebView);
}
super.onConfigurationChanged(newConfig);
setContentView(R.layout.activity_main);
initUI();
}
#Override
protected void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
// Save the state of the WebView
myWebView.saveState(outState);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
// Restore the state of the WebView
myWebView.restoreState(savedInstanceState);
}
}
Main Activity XML:
<LinearLayout 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:orientation="horizontal" >
<FrameLayout android:id="#+id/webViewPlaceholder"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
try adding orientation to you activity definition in manifest file as follows:
<activity android:screenOrientation="portrait" ...>
</activity>
I realized I don't even need WebChromeClient at all if I'm not using any of it's features. Found answer here: How to prevent WebView auto refresh when screen rotation
simply add "screenSize" to your configchanges in manifest.
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
I am developing an android game application,I have implemented all the screens.Now i want to change the webview background color,Can anybody guide me.Here is my xml file
<?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"
android:background="#drawable/background">
<WebView
android:id="#+id/webbrowser"
android:layout_width="fill_parent"
android:layout_height="345px"
android:layout_marginTop="46px"/>
<Button
android:id="#+id/Btn"
android:background="#drawable/back_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="109px"
android:layout_marginTop="37px">
</Button>
</LinearLayout>
And My Java file is
package com.tli.roadtripbingo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
public class WebView1 extends Activity {
private Button Back;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.webview);
Back = (Button)findViewById(R.id.back);
WebView webView = (WebView) findViewById(R.id.webbrowser);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://www.vikingredning.no/skilt.aspx");
webView.setWebViewClient(new HelloWebViewClient());
}
class HelloWebViewClient extends WebViewClient
{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
};
}
Thanks in advance
Regards
Tushar
You can find answer here Change Background color and font color
WebView myWebView = (WebView) findViewById(R.id.myWebView);
myWebView.setBackgroundColor(Color.parseColor("#123456"));
You can make the WebView transparent like this:
WebView webView = (WebView) findViewById(R.id.webView);
webView.setBackgroundColor(Color.TRANSPARENT);
<LinearLayout
android:id="#+id/web_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/web_bg_color"
android:gravity="center" >
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
WebView mWebView = (WebView) findViewById(R.id.webview);
mWebView.setBackgroundColor(Color.TRANSPARENT);
mWebView.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null);
It worked for me in lollipop also.
try it once!
You may also want to reload the page after changing colors by using the reload() method:
webView.reload();