I'm very new to Java, and Android development. I want to be able to display a local page page within my App using the fragments (because I don't want the entire app to be web based).
So, I've gone into my arview.xml and added a fragment:
<fragment
android:id="#+id/fragment_bbc"
android:name="android.webkit.WebViewFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" />
Now, I want to place a web page in there (for now, I'll just use the bbc website).
Within my 'onCreate(Bundle savedInstanceState)' I have this:
webFrag = (WebViewFragment) mGUIView.findViewById(R.id.fragment_bbc);
mWebView = (WebView) mGUIView.findViewById(R.id.fragment_bbc);
myWebView.loadUrl("http://www.bbc.co.uk");
I have errors of all 3 of those lines, all saying they can't be resolved.
Where am I going wrong? Thanks
You shouldn't use the "tag" fragment in xml, just do a classic layout and use it in a Class that extend Fragment
Example :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup group, Bundle saved) {
_view = inflater.inflate(R.layout.fragment_accueil, group, false);
// get views
_textViewTitle = (TextView) _view.findViewById(R.id.title_home);
_buttonLocalSearch = (Button) _view.findViewById(R.id.btn_local_search);
return _view;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
{
_textViewTitle.setText(getActivity().getString(R.string.tle_home));
_buttonLocalSearch.setOnClickListener(this);
}
In Java you need to initialize the components you want to use and select the variable-type on your own. In your case try changing
webFrag = (WebViewFragment) mGUIView.findViewById(R.id.fragment_bbc);
mWebView = (WebView) mGUIView.findViewById(R.id.fragment_bbc);
to
WebViewFragment webFrag = (WebViewFragment) mGUIView.findViewById(R.id.fragment_bbc);
WebView mWebView = (WebView) mGUIView.findViewById(R.id.fragment_bbc);
or you do it entirely different and try something like this:
WebView wv = new WebView();
wv.findViewById();
Did you previously instantiate those variables?
Try:
WebViewFragment webFrag = (WebViewFragment) findViewById(R.id.fragment_bbc);
WebView mWebView = (WebView) findViewById(R.id.fragment_bbc);
mWebView.loadUrl("http://www.bbc.co.uk");
I thik your way of implementation is wrong.
First of all simply you have to do this
Add Fragment to your parent activity
Then using instance of your WebViewFragment call the method loadUrl()
And here is the code for WebViewFragment just supply your url to loadUrl() method thats it.
Please check that link that will guide you well.
Related
Well , when I was trying to get a webView from a fragment in my activity,it doesn't works.My code below:
WebView webView;
webView = (WebView) getFragmentManager()
.findFragmentById(R.id.fragment_goods_details).getView()
.findViewById(R.id.goods_details);
Basically, this is a bad idea. Since a fragment has it own life-cycle.
In your activity, you can control your fragment to add it/replace it but you won't know exactly when your fragment is ready for manipulating.
Rather than that, you can have support from Event Bus or RxAndroid to dispatch event asyn to your Fragment from your activity. Within the fragment, then you can safely handle action/data/state of your webview
YourFragmentClass {
WebView webview;
public WebView getWebview(){ return webview;}
}
In your activtiy:
Fragment f = getFragmentManager().findFragmentById(xxxx);
if(f instanceOf YourFragmentClass) {
webView = (YourFragmentClass)f.getWebview();
}
I'm making an app with webviews.
I just have a single activity containing
a FrameLayout to dynamically add fragments.
Every fragment contains a webView.
Everything works fine, but when I remove a fragment
from the stack, the webview of the fragment in the
top of the stack is reloaded so the content inside the
script of the html is called again.
The restoreState() method is not working.
This is the onCreateView of the fragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.wv_container, container, false);
webView = (WebView)view.findViewById(R.id.webviewcontainer);
if(webViewBundle == null)
webView.loadUrl("file:///android_asset/www/main.html");
else
webView.restoreState(webViewBundle);
return view;
}
This is the onPause() method:
#Override
public void onPause() {
super.onPause();
webViewBundle = new Bundle();
webViewContent.saveState(webViewBundle);
}
How should I prevent the page reloading for my case?
I don't want to hack the html with flags or something like that.
Thanks in advance!
The saveState method in your onPause returns a WebBackForwardList, which contains a list of WebHistory items. These are basically the titles, urls and favicons from recently visited pages. It does not cache the contents of the downloaded url.
Try looking at the method saveWebArchive instead.
I'm currently having a WebView placed in a fragment like this:
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
mContext = container.getContext();
LayoutInflater mInflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout view = (LinearLayout) mInflater.inflate(R.layout.post_view_layout, container,false);
view.setBackgroundColor(Color.WHITE);
_viewCache = view;
_post_WebView = (WebView) view.findViewById(R.id.post_webview);
setupWebView(_post_WebView); //This method sets up the webview (javascript mode etc.) and set the url to load
return view;
}
Now whenever I rotate, the activity that contains this fragment gets recreated (I can't use the configChanges="orientation" attribute for the manifest because I'm using ABS)
Because of setRetainInstance(true) the fragment, it's layout and all variables get retained fine and all, but whenever I try to scroll or click in the retained WebView I get a BadTokenException (probably because the original activity used for it's context is destroyed during rotation).
I could solve this by just recreating the WebView with the new activity context after rotation, but since the webview shows a input form, recreating it after rotation might prove tedious to users. (I already tried saving the state using the saveState and restoreState methods of the WebView, but to no avail)
Is there any way to restore or retain the WebView in it's complete pre-rotation state without generating a BadTokenException, just like it would when the configChanges="orientation" attribute has been added?
Im updating some of my older projects and using fragments to update the look of things. I tried to use a fragment to launch a webview but when I try to run it I get the following error in my logcat.
E/Web Console(22464): Uncaught TypeError: Cannot read property 'addEventListener' of null at
http://m.yahoo.com/?.tsrc=yahoo&mobile_view_default=true:1
The way I used to use a webview was to just create a class that was its own activity that took place in a webview but I would like to have a small view within a fragment and then when I wanted to use the class I would launch it via intent and pass anything I needed to the webview like a url and other parameters in extras within the intent. I tried just setting up a webview within a fragment but I havent gotten it to work yet. This is the code Im using for the moment.
public class WebViewer extends Fragment {
WebView Wv;
String url = "http://www.yahoo.com";
Activity act;
public WebViewer(){}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
this.act = activity;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View mView = inflater.inflate(R.layout.webview, container);
Wv = (WebView) mView.findViewById(R.id.webview1);
Wv.getSettings().setJavaScriptEnabled(true);
Wv.getSettings().setRenderPriority(RenderPriority.HIGH);
Wv.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
Wv.getSettings().setDomStorageEnabled(true);
Wv.setWebViewClient(new HelloWebViewClient());
Wv.getSettings().setBuiltInZoomControls(true);
Wv.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
act.setTitle("Loading...");
act.setProgress(progress * 100);
if(progress == 100)
getActivity().setTitle(R.string.app_name);
}
});
Wv.loadUrl(url);
return mView;
}
}
And then this is the layout for the activity that uses this fragment.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#drawable/bggreydotted"
>
<fragment
android:id="#+id/webFragment"
android:layout_width="150dip"
android:layout_height="match_parent"
android:name="my.proj.WebViewer"></fragment>
</LinearLayout>
So how can I get a webview to open inside a fragment I can use in a view.
Have you thought about just extending WebViewFragment?
I am kind of lost here so please give me some help.
I've been working on an app on ICS 4.0.2 for my GNex.
As of yet my app structure is as follows :
DemoActivity extends Activity contains three fragments that show up as tabs :
MapFragmentTab extends Fragment
SettingsFragmentTab extends Fragment
AboutFragmentTab extends Fragment
In the map fragment, I want to have a WebView display Google Maps.
This is my MapFragmentTab :
public class MapFragmentTab extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View mainView = (View) inflater.inflate(R.layout.map, container, false);
WebView webView = (WebView) mainView.findViewById(R.id.webview);
webView.setWebViewClient(new MyWebViewClient());
webView.loadUrl("http://maps.google.com");
return mainView;
//return inflater.inflate(R.layout.map, container, false);
}
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
Right now, it seems to be working, however Google Maps ( although the page frame does load ) won't load.
I use Chrome Beta, haven't tried the default browser yet.
I'm really confused when it comes to WebViews, WebViewFragments and Fragments...
any help would be appreciated.
You need to enable JavaScript specifically on WebView, as it is disabled by default. Use something like: webView.getSettings().setJavaScriptEnabled(true);
From: http://developer.android.com/reference/android/webkit/WebSettings.html#setJavaScriptEnabled(boolean)
http://developer.android.com/resources/tutorials/views/hello-webview.html