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..
Related
I read online, that to call another fragment, to take up the FrameLayout, you need to create an interface that talks to the activity and when the button clicked the function inside the interface that is defined in the activity replaces the Fragment with another fragment, but instead of creating the interface, i tried doing it directly from inside the fragment and it worked. So any reasons why i should not be doing this ?
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class FragmentOne extends Fragment {
public FragmentOne() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_fragment_one, container, false);
Button btnONe = view.findViewById(R.id.btnOne);
btnONe.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onButtonPressed();
}
});
return view;
}
public void onButtonPressed() {
FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragmentContainer, new FragmentTwo("Femin Dharamshi did it!"));
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}
Fragments are part of the Activity and fragment should be hosted through their parent activities as stated by google in Android developers docs.
You can refer this link:-
https://developer.android.com/training/basics/fragments/communicating
You should call getFragmentManager().beginTransaction().replace(int id, Fragment fr).commit(); in onViewCreated() method, not onCreateView() method.
Just move your code in onCreateView() into onViewCreated() method.
I have an empty fragment where I want to add a Google View map which can find the location of the nearest hospital. I have no clue how to do this.
Here is the code for the empty fragment:
package com.iotaconcepts.aurum;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class TwoFragment extends Fragment
{
public TwoFragment()
{
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_two, container, false);
}
}
This tutorial will be useful for you to get started with your MAP Fragment
http://www.joellipman.com/articles/google/android/application-development/android-os-add-googlemap-as-fragment.html
And for finding the hospitals near you you can use the following tutorial
http://javapapers.com/android/find-places-nearby-in-google-maps-using-google-places-apiandroid-app/
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.
I have the following code in the Main Activity and I have a FragmentLayout class Which is responsible for calling the fragment.
Code
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends FragmentActivity
{
Button btn;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=(Button)findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
FragmentLayout f1=new FragmentLayout();
getSupportFragmentManager().beginTransaction().add(R.id.main_id, f1).commit();
}
});
}
}
I get this error-
The method add(int, Fragment) in the type FragmentTransaction is not applicable for the arguments (int, FragmentLayout) MainActivity.java.
What is the problem here?
You can use transaction manager only for Fragments.
I provide you simple example:
Create some Fragment and layour for it:
public class FragmentOne extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
//Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_one, container, false);
}
}
And in your public void onClick(View v)
just call:
FragmentOne f1=new FragmentOne();
getSupportFragmentManager().beginTransaction().add(R.id.main_id, f1).commit();
Here
getSupportFragmentManager().beginTransaction().add(R.id.main_id, f1).commit();
f1 must extend Fragment in order to be added as part of a FragmentTransaction.
I came across an error today in android studio. I am trying to create a about us screen in the app. The layout xml file has been created. Any help is appreciated. Thanks.
error: cannot resolve method setcontentview(int)
package example.com;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
public class AboutFragment extends android.app.Fragment {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.about_screen);
}
}
Your class extends Fragment and you have setContentView(R.layout.about_screen);. setContentView is a method of Activity class.
Instead inflate about_screen.xml in onCreateView of Fragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.about_screeen, container, false);
return rootView;
}