Using WebView in Fragment - android

recently I started creating an Android app with WebView. The app has grown and I changed the activity with WebView to tabbed activity using fragments and ViewPager. And there started my issue. After some headaches I finally managed how to display proper fragments for each tab. Then I simply copied the code for WebView to one of the fragment but the fragment isn't showing the web page I want. Any help will be appreciated!
Here's the code:
fragment with webView
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 WebViewFragment extends Fragment {
public WebView mWebView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mWebView = (WebView) mWebView.findViewById(R.id.webview);
mWebView.loadUrl("google.com");
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
// Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebViewClient(new WebViewClient());
return inflater.inflate(R.layout.fragment_bugtracker, container, false);
}
}
and 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"
tools:context=".Tabs$BugtrackerFragment">
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>

Things to do like
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.fragment_bugtracker, container, false);
mWebView = (WebView) v.findViewById(R.id.webview);
mWebView.loadUrl("https://google.com");
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
// Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebViewClient(new WebViewClient());
return v;
}

Related

How to use webView in read mode kotlin?

I have an app and i want to open the URL with Reading Mode
Without ads or something the reader don't need to see
How can i do that haw can i enable the reading mode on in kotlin ?
Try it with WebView,
In .xml file add this code:
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
And inside activity(or fragment)
import android.os.Bundle;
import android.webkit.WebView;
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.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://www.google.com");
}
}

WebView in XML not working with Navigation Drawer Fragment

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.

"Unfortunately, has stopped" eclipse android avd

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" />

how to add web view cache in external storage on android fragment screen?

hi i have three fragment tab view, the three tab having web view am trying to add website in my app .. i fragment not supported to getApplicationContext().. plz help me to add cache in ma activity.
import info.androidhive.tabsswipe.R;
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.WebView;
import android.webkit.WebViewClient;
public class ServicesFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_services, container, false);
WebView webView = (WebView)rootView.findViewById(R.id.webView1);
webView.loadUrl("http://www.zudioz.com/services.html");
webView.getSettings().setSupportZoom(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.setScrollbarFadingEnabled(true);
webView.getSettings().setLoadsImagesAutomatically(true);
webView.getSettings().setDomStorageEnabled(true);
webView.setWebViewClient(new WebViewClient());
return rootView;
}
}
Use getActivity().getApplicationContext().

Changing the background of webview in android

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

Categories

Resources