webview inside onclick imagebutton error - android

I am trying to open webview with an imagebutton. I was able to do with an intent and uri.parse like so:
twbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent browserIntent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("website"));
startActivity(browserIntent);
But it exits the application and opens the browser. I want it to open up without leaving the application
fbbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
View rootView = inflater.inflate(R.layout.fragment_socialmedia, container, false);
WebView myWebView = (WebView) rootView.findViewById(R.id.webview);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setWebViewClient(new WebViewClient());
myWebView.loadUrl("website");
}
});
but I get the error:
W/art﹕ Attempt to remove local handle scope entry from IRT, ignoring
W/AwContents﹕ onDetachedFromWindow called when already detached. Ignoring

Are you sure to have the element webview inside the layout fragment_socialmedia.xml ?
View rootView = inflater.inflate(R.layout.fragment_socialmedia, container, false);
WebView myWebView = (WebView) rootView.findViewById(R.id.webview);
thats the reason for what you are getting this exception : android.webkit.WebSettings android.webkit.WebView.getSettings()' on a null object reference

You need to swap these two lines:
myWebView.loadUrl("website");
myWebView.setWebViewClient(new WebViewClient());
Set WebViewClient before doing the load:
myWebView.setWebViewClient(new WebViewClient());
myWebView.loadUrl("website");

Related

White Bar on LEFT of webview

I am getting a solid white bar on the LEFT side of my webview from a fragment. I have seen examples where the bar was on the right but not on the left. I have tried every solution from those examples such as
mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
or
mWebView.setVerticalScrollBarEnabled(false);
and within the XML layout setting scrollbars to
android:scrollbars="none"
Is there any way of removing this white bar? Additionally I have a navigation drawer on the left that I believe could be associated with the issue.
[]
WebFragment.java
public class WebFragment extends Fragment implements IOBackPressed {
public WebView mWebView;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_shop, container, false);
mWebView = (WebView) view.findViewById(R.id.webview);
// 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 myWebViewClient());
mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
mWebView.setVerticalScrollBarEnabled(false);
mWebView.setScrollBarSize(0);
mWebView.loadUrl("");
return view;
}
#Override
public boolean onBackPressed() {
if (mWebView.canGoBack()) {
mWebView.goBack();
//backpress is not considered in the activity
return true;
} else {
return false;
}
}
}
I have checked your code , use this code before return view statement ,it will work fine .
mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.getSettings().setUseWideViewPort(true);

Webview with multiple buttons to multiple URLs - Null pointer exception

I am trying to use a fragment to make some buttons that when clicked will open in a Webview with different URLs. Eg. when i click the first button i go to "https://google.com/", second button to "https://fb.com" and so on.
I have made methods like so for each redirect (as I was using intents to redirect to a web browser in previous version):
private void goToGoogle(View view) {
this.view = view;
goToUrl("https://google.com");
}
The goToURL(String URL) method is simply an intent to open the browser with the URL as parameter.
Now, I have been asked to implement an in-app webview. I have the following code:
public void openWebView (String url){
mWebView.setWebViewClient(new WebViewClient());
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.loadUrl(url);
mWebView.setWebViewClient(new WebViewActivity());
}
which I call with openWebView(String.valueOf(R.string.googleURL));.
My onCreateView method is declared as follows (example for 1 button, there are 16 on the real thing):
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_imp_links, container, false);
LinearLayout activityLayout = new LinearLayout(getActivity());
Button1 = view.findViewById(R.id.goToGoogle);
Button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
goToGoogle(v);
}
});
I have also got this class in my app to handle the WebView:
public class WebViewActivity extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
}
I am getting java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
Any help you can provide is highly appreciated. Thank you.
Definition of mWebView: private WebView mWebView = new WebView(getContext());
To get a string from the resources, do not use String.valueOf(R.string.googleURL).
Instead use Context#getString(R.string.googleURL).

Get the Title for an Html page from WebView History after goBack()?

In my app there is a WebView displaying Html content and a back button to let the user navigate back in the browser history. In the WebChromeClient, I get the Html page title in onReceivedTitle() and display it in my app.
The user is shown the first page and the first page title is displayed. The user can then click a hyperlink in the Html to get to the next page. The second page has a different title, and this is correctly displayed. The problem is if the user than clicks the Back button, the first page is diaplayed from the history but onReceivedTitle() is not called again. So my fist page is displayed with the wrong title from the second page.
How can I get the title of the page being that was loaded by the call to goBack()?
Here is some simplified code to demonstrate. Imagine a layout with a TextView, WebView, and Button. Also, imagine two Html files in the assets folder where pageone.html links to pagetwo.html and the two pages have different titles.
public class MyActivity extends Activity {
WebView webView;
TextView title;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webView = (WebView) findViewById(R.id.webView);
title = (TextView) findViewById(R.id.title);
webView.loadUrl("file:///android_asset/pageone.html");
webView.setWebChromeClient(new WebChromeClient() {
#Override
public void onReceivedTitle(WebView view, String titleText) {
super.onReceivedTitle(view, titleText);
title.setText(String.format("Title: %s", titleText));
}
});
final Button button = (Button) findViewById(R.id.backButton);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (webView.canGoBack()) {
webView.goBack();
}
}
});
}
}
Apparently, this is a bug in certain builds of Android and used to work correctly.
I had been using the suggestion from https://stackoverflow.com/a/12154530/90236, but I think the answer for me is to not use onReceivedTitle() because it isn't called after a goBack.
onPageFinished() in the WebViewClient seems to be better if you need to handle goBack().
This seems to be working:
public class MyActivity extends Activity {
WebView webView;
TextView title;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webView = (WebView) findViewById(R.id.webView);
title = (TextView) findViewById(R.id.title);
webView.loadUrl("file:///android_asset/pageone.html");
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
title.setText(String.format("Title: %s", view.getTitle()));
}
});
final Button button = (Button) findViewById(R.id.backButton);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (webView.canGoBack()) {
webView.goBack();
}
}
});
}
}
However, this changes the timing of when the Android app is notified of the title. What I have found is:
WebChromeClient.onReceivedTitle occurs first. However, this event does not occur when webView.goBack() is used to navigate to a page.
Then the html page loads and the DOM window.onload occurs
Finally WebViewClient.onPageFinished occurs
If you use Javascript to update the page title, using onPageFinished may have quite different behavior than onReceivedTitle because of the event timing.

Android - Using Fragments

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.

Android-How to use a fragment to display a webview

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?

Categories

Resources