I'm learning how to program Android in Android Studio slowly but surely, working on an app to open the mobile Facebook site in a fragment, with the intent to make multiple fragments for multiple social networks. I have my main (Facebook) fragment, but I'm getting this odd error:
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.app.Fragment;
import android.webkit.WebSettings;
import android.webkit.WebView;
public class fragment_main extends Fragment {
private WebView mWebView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_main, container, false);
mWebView = (WebView) getView().findViewById(R.id.activity_main_webview);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.loadUrl("http://m.facebook.com");
mWebView.setWebViewClient(new MyAppWebViewClient());
}
public void onBackPressed() {
if(mWebView.canGoBack()) {
mWebView.goBack();
}
}
}
It keeps wanting to move my "mWebView" to my "private WebView," which gives more, odder errors. Also, apologizes if this is dumb.
Reorder your statements like following; you are returning before you have done anything in your onCreateView() method
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
mWebView = (WebView) getView().findViewById(R.id.activity_main_webview);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.setWebViewClient(new MyAppWebViewClient());
mWebView.loadUrl("http://m.facebook.com");
return inflater.inflate(R.layout.fragment_main, container, false);//
}
Related
I have a bottom navigation bar that switches between 5 fragments. One of these 5 buttons should open WebView (newsfeed) and display some HTML. The code that calls the newsfeed.xml is this:
public class newsfeedFragment extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.newsfeed,container,false);
return v;
}
}
Now I followed a youtube tutorial to pull the HTML from a website (code below). In the tutorial, the code is in it's own activity and not called within a fragment. I am not sure how to combine these 2 code snippets to create the webview inside my fragment. Help would be much appreciated!
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends ActionBarActivity {
private WebView myWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWebView = (WebView)findViewById(R.id.webView);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl("http://www.website.com");
myWebView.setWebViewClient(new WebViewClient());
}
#Override
public void onBackPressed() {
if(myWebView.canGoBack()) {
myWebView.goBack();
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
I assume you want to load the webview into the fragment?
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.newsfeed,container,false)
WebView webview = (WebView) v.findViewById(R.id.webview);
webview.loadUrl("http://www.website.com")
return v;
}
So inflate the Layout and afterwards call findviewbyid with your new layout and then you could do anything you want with your webview and it is loaded in your Fragment. Hopefully the solution for your question.
Take WebView in your newsfeed layout, and do the same thing in fragment.
You can use this code anywhere in the fragment to open the activity:-
startActivity(new Intent(getActivity(),ActivitytoOpen.class));
Ok so I've been working on an app in Android Studio for school. I have a webview inside of a fragment. But in the app, when ever I open the fragment on my phone, the app crashes and my phone says "Unfortunately, LARKer App has stopped."
Here's the Fragment class:
import android.content.Intent;
import android.net.Uri;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
/**
* Created by Andrew on 12/29/2014.
*/
public class menu4_Fragment extends Fragment {
private WebView mWebView;
View rootview;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootview = inflater.inflate(R.layout.menu4_layout,container,false);
mWebView = (WebView) getView().findViewById(R.id.webView);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.loadUrl("foo");
mWebView.setWebViewClient(new MyWebViewClient());
return rootview;
}
public class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(Uri.parse(url).getHost().contentEquals("foo")) {
// This is my website, so do not override; ler my WebView load the page
return false;
}
//Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
}
And here's the XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="LARK: Login"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="35dp"
android:textSize="50dp" />
<WebView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/webView"
android:layout_below="#+id/textView"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
Here's an image of what I got on my phone:
Change this code
mWebView = (WebView) getView().findViewById(R.id.webView);
Like this, it will work
mWebView = (WebView) rootview.findViewById(R.id.webView);
You have to get the view's from your View Group.otherwise it will return error.
change this on your oncreateview() methed of your fragmanet
mWebView = (WebView) rootview.findViewById(R.id.webView);
Try this code,
WebView webViewInfo = (WebView) findViewById(R.id.webview);
webViewInfo.getSettings().setJavaScriptEnabled(true);
webViewInfo.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webViewInfo.setWebViewClient(new MYWEBCLIENT());
webViewInfo.loadData("LOAD_YOUR_URL", "text/html", "UTF-8");
And WebViewClient class implement on webview method.
private class MYWEBCLIENT extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
On keyboard back handle,
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webViewInfo.canGoBack()) {
webViewInfo.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
I hope this code help for you...!!
Here's my webview drawer app..
It's making me crazy, i can't understand how to insert a "goBack" function like in normal WebView. Here's my code:
package com.my.app;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebViewFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Retrieving the currently selected item number
int position = getArguments().getInt("position");
String url = getArguments().getString("url");
// List of rivers
String[] menus = getResources().getStringArray(R.array.menus);
// Creating view corresponding to the fragment
View v = inflater.inflate(R.layout.fragment_layout, container, false);
// Updating the action bar title
getActivity().getActionBar().setTitle(menus[position]);
//Initializing and loading url in webview
WebView webView = (WebView)v.findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(url);
webView.setWebViewClient(new WebViewClient());
return v;
}
}
I've already checked more than 20+ answers here on Stack, but without results.
Hope someone can explain me a bit how to use the function GoBack and help me with this stressing thing..
Thanks in advance,
Davide
Inside of the your OnCreateView:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.your_layout, container, false);
mWebView = (WebView) view.findViewById(R.id.webView);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new MyWebViewClient());
mWebView.loadUrl(expertsUrl);
return view;
}
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
Ok! In your MainActivity you will write it:
#Override
public void onBackPressed() {
if (WebViewFragment.mWebView!=null) {
if (WebViewFragment.mWebView.canGoBack()) {
WebViewFragment.mWebView.goBack();
}
}
}
WebViewFragment= This is your nameFragment that content the Webview
mWebView= The name of your WebView (id)
Now. Inside of your Fragment you have to declare static global variable
public static Webview mWebView;
And that's it! Good luck!
Since you are using a fragment, you have to override your activity onbackpressed() method and then test to see what fragment you are using. If you are using the fragment with the webview in it when the user tries to go back then see if the webview can go back. The code would look something like this.
#override
public void onBackPressed()
{
//get a reference to your webviewfragment before this line
//perhaps find the fragment by tag
if(webviewFragment.isVisible())
{
WebView mWebView = webviewFragment.getWebView();
if(mWebView.canGoBack())
{
mWebView.goBack();
return;
}
}
super.onBackPressed();
}
something like that, not exactly like it but close.
could any one help me in figuring out how to open two webviews in the same screen using fragments Android,each webview must display a certain web page for example :1-google,2,yahoo.
i've tried too many tutorials and samples .nothing works fine for me... :(
The main issue for me that conflicting my thoughts, is what to write in the fragment class to open a webView and what to write in the main activity that runs the whole app .
Thanks in advance for any help.. :)
here is my code that runs fine for the portrait mode with one screen and crashes on landscape mode:
package com.example.androidwebviewfragment;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class Fragment1 extends Fragment {
WebView myWebView;
final static String myBlogAddr = "http://android-er.blogspot.com";
String myUrl;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.layout_webfragment,container,false);
myWebView = (WebView)view.findViewById(R.id.mywebview);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setWebViewClient(new MyWebViewClient());
if(myUrl == null){
myUrl = myBlogAddr;
}
myWebView.loadUrl(myUrl);
return view;
}
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
myUrl = url;
view.loadUrl(url);
return true;
}
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
}
}
and here is the second fragment :
package com.example.androidwebviewfragment;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class Fragment2 extends Fragment {
WebView myWebView;
final static String myBlogAddr = "http://android-er.blogspot.com";
String myUrl;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate
(R.layout.layout_webfragment2,container,false);
myWebView = (WebView)view.findViewById(R.id.mywebview);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setWebViewClient(new MyWebViewClient());
if(myUrl == null){
myUrl = myBlogAddr;
}
myWebView.loadUrl(myUrl);
return view;
}
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
myUrl = url;
view.loadUrl(url);
return true;
}
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
}
}
And here is the main activity:
package com.example.androidwebviewfragment;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
/* (non-Javadoc)
* #see android.app.Activity#onCreate(android.os.Bundle)
*/
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
the .xml files are :
1-Fragment 1 :
<WebView
android:id="#+id/mywebview"
android:layout_height="match_parent"
android:layout_width="match_parent" />
</LinearLayout>
2-fragment 2 :
</LinearLayout>
3-main xml layout:
<fragment
android:name="com.example.androidwebviewfragment.Fragment1"
android:id="#+id/myweb_fragment1"
android:layout_height="match_parent"
android:layout_width="match_parent" />
<fragment
android:name="com.example.androidwebviewfragment.Fragment2"
android:id="#+id/myweb_fragment2"
android:layout_height="match_parent"
android:layout_width="match_parent" />
</RelativeLayout>
And this is the main.xml in the layout-land folder:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<fragment
android:name="com.example.androidwebviewfragment.Fragment1"
android:id="#+id/myweb_fragment"
android:layout_height="match_parent"
android:layout_width="match_parent" />
<fragment
android:name="com.example.androidwebviewfragment.Fragment2"
android:id="#+id/myweb_fragment"
android:layout_height="match_parent"
android:layout_width="match_parent" />
</LinearLayout>
The error on land scape mode from log:
java.lang.RuntimeException:
Unable to start activity ComponentInfo{com.example.androidwebviewfragment
/com.example.androidwebviewfragment.MainActivity}
:android.view.InflateException: Binary XML file line #12: Error inflating class fragment
In the fragment class you create view and returns it to main activity and in the mainactivity you create a fragmentadapter to bind fragments. See (http://developer.android.com/reference/android/support/v4/view/ViewPager.html) link for more details and see (http://www.vogella.com/articles/AndroidFragments/article.html) for examples.
I am new to fragment.I am loading url in webview inside fragment but its not loading .In emulator webpage may temporarily down message is coming .in samsung galaxy tab device coming blank can anybody tell what is problem How to solve
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
public class WebViewFragment1 extends Fragment {
WebView loadWeb;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Tell the framework to try to keep this fragment around
// during a configuration change.
setRetainInstance(true);
// Start up the worker thread.
}
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
ViewGroup layout = (ViewGroup) inflater.inflate(R.layout.gcmweb, container, false);
loadWeb=(WebView)layout.findViewById(R.id.loadWeb);
/*String url = "http://unstructure.org/unstruc/unstruc.php?i="
+ "sample test".replaceAll(" ", "-");*/
String url="http://www.google.com/";
loadWeb.loadUrl(url);
return layout;
}
}
Here I am adding dynamically in activity
FragmentTransaction ft = fm.beginTransaction();
Fragment fragReplace=new WebViewFragment1();
//fm.beginTransaction();
ft.add(R.id.replace_frag_container,fragReplace);
ft.addToBackStack(null);
ft.commit();
Thanks
For using fragments, you should extend activity and have a layout of fragment or vice versa. Check this link This is a great tutorial for beginners
public class DetailsFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return(inflater.inflate(R.layout.details_fragment, container, false));
}
public void loadUrl(String url) {
((WebView)(getView().findViewById(R.id.browser))).loadUrl(url);
}
}
This is working for me can u try this..