Android-How to use a fragment to display a webview - android

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?

Related

navigateUp() and navigate(action_id) not working as expected

Using the Navigation framework, I navigate from a Homefragment to a WhsSearchWebDB and back to the Homefragment.
When coming back via navController.navigate(R.id.action_whsSearchWebDB_to_nav_home) or navController.navigateUp(), the screen is completely white and the toolbar is only partially updated: the name of the fragment is correctly set, but the navigation button shows a back arrow instead of the three-bar-home icon.
When coming back via the back button (navController.navigate(R.id.action_whsSearchWebDB_to_nav_home) and navController.navigateUp() commented, of course), everything works fine.
I need to come back programmatically, not through a user click on the back button. What must I change?
mobile_navigation.xml:
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/mobile_navigation"
app:startDestination="#id/nav_home">
<fragment
android:id="#+id/nav_home"
android:name="be.ema.golfclubdataconversion.ui.home.HomeFragment"
android:label="#string/menu_home"
tools:layout="#layout/fragment_home" >
<action
android:id="#+id/action_nav_home_to_whsSearchWebDB"
app:destination="#id/whsSearchWebDB" />
</fragment>
<fragment
android:id="#+id/nav_gallery"
android:name="be.ema.golfclubdataconversion.ui.gallery.GalleryFragment"
android:label="#string/menu_gallery"
tools:layout="#layout/fragment_gallery" />
<fragment
android:id="#+id/nav_slideshow"
android:name="be.ema.golfclubdataconversion.ui.slideshow.SlideshowFragment"
android:label="#string/menu_slideshow"
tools:layout="#layout/fragment_slideshow" />
<fragment
android:id="#+id/whsSearchWebDB"
android:name="be.ema.golfclubdataconversion.ui.WhsSearchWebDB"
android:label="WhsSearchWebDB"
tools:layout="#layout/search_web_db">
<action
android:id="#+id/action_whsSearchWebDB_to_nav_home"
app:destination="#id/nav_home" />
</fragment>
home_fragment code:
public class HomeFragment extends Fragment {
public static View root = null;
private HomeViewModel homeViewModel;
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_home, container, false);
}
#Override
public void onViewCreated(View root, Bundle savedInstanceState) {
// super.onViewCreated(root, savedInstanceState);
// homeViewModel = new ViewModelProvider(this).get(HomeViewModel.class);
Button openUrlBtn = root.findViewById(R.id.openUrlBtn);
openUrlBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
NavController navController = Navigation.findNavController(getActivity(), R.id.nav_host_fragment);
navController.navigate(R.id.action_nav_home_to_whsSearchWebDB);
}
});
}
}
WhsSearchWebDB code :
public class WhsSearchWebDB extends Fragment {
public static Activity activity;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.search_web_db, container, false);
return v;
}
#Override
public void onViewCreated(View root, Bundle savedInstanceState) {
super.onViewCreated(root, savedInstanceState);
activity = getActivity();
String urlToBeOpened = "http://ncrdb.usga.org";
WebView webView = (WebView) root.findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");
webView.setWebViewClient(new WebViewClient(){
#Override
public void onPageFinished(WebView view, String url)
{
/* This call inject JavaScript into the page which just finished loading. */
webView.loadUrl("javascript:window.HTMLOUT.processHTML('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");
}
});
webView.loadUrl(urlToBeOpened);
}
public static class MyJavaScriptInterface {
#JavascriptInterface
public void processHTML(String html) {
NavController navController = Navigation.findNavController(activity, R.id.nav_host_fragment);
// navController.navigate(R.id.action_whsSearchWebDB_to_nav_home);
// if (!navController.navigateUp()) {
// System.out.println("nooooooo !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
// }
}
}
}
**** EDIT ****
This is the expected screen (picture taken before going to the WhsSearchWebDB fragment:
And this is the incorrect result after trying to come back:
The Navigation Action in the whsSearchWebDB Fragment is creating a new fragment not popping back to the original fragment.
<fragment
android:id="#+id/whsSearchWebDB"
android:name="be.ema.golfclubdataconversion.ui.WhsSearchWebDB"
android:label="WhsSearchWebDB"
tools:layout="#layout/search_web_db">
<action
android:id="#+id/action_whsSearchWebDB_to_nav_home"
// Change this
app:destination="#id/nav_home"
// To this to pop back
app:popUpTo="#id/nav_home" />
I didn't include popUpToInclusive.
PopUPToInclusive needs to be set to true, the popUpTo attribute will remove all destinations up to and including the given destination from the back stack.
Open the navigation graph
Select the action, you want
In the attributes pane, set popUptToInclusive the homeFragment, Select the popUpToInclusive check box to true.
For more details please check out the Navigation code lab.
https://developer.android.com/codelabs/android-navigation#6
I finally discovered that the processHTML method executes on a background thread, hence not allowed to manipulate UI widgets. I had preferred a crash/error instead of the unexpected behavior I have reported in my question.
I have fixed the issue by implementing a ViewModel, selecting the result in the processHTML method and observing it on the UI thread as follows:
#Override
public void onViewCreated(View root, Bundle savedInstanceState) {
super.onViewCreated(root, savedInstanceState);
activity = getActivity();
String urlToBeOpened = "http://ncrdb.usga.org";
WebView webView = (WebView) root.findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");
webView.setWebViewClient(new WebViewClient(){
#Override
public void onPageFinished(WebView view, String url)
{
/* This call inject JavaScript into the page which just finished loading. */
webView.loadUrl("javascript:window.HTMLOUT.processHTML('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");
}
});
webView.loadUrl(urlToBeOpened);
TeeboxViewModel teeboxViewModel = new ViewModelProvider((ViewModelStoreOwner) activity).get(TeeboxViewModel.class);
teeboxViewModel.getSelected().observe(getViewLifecycleOwner(), item -> {
...
NavController navController = Navigation.findNavController(activity, R.id.nav_host_fragment);
navController.navigate(R.id.action_whsSearchWebDB_to_nav_newResult);
});
}
public static class MyJavaScriptInterface {
public static Map<String, String> coursesList = new HashMap<>();
#JavascriptInterface
public void processHTML(String html) {
...
TeeboxViewModel model = new ViewModelProvider((ViewModelStoreOwner) activity).get(TeeboxViewModel.class);
model.select(teeboxesList);
}
}

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).

Prevent WebView fragment from reloading page on screen rotation

I know this has been asked too many times and that SO is full of similar questions. I went through most of them and I've been researching this issue for a couple of days and I have yet to find the definitive solution.
I could easily avoid all this trouble adding configChanges="orientation|screenSize" to the activity containing the WebView; I've tested this and it worked as intended (the WebView didn't reload). But I really wanted to avoid this solution.
Here's my current WebView implementation:
public class BrowserFragment extends Fragment {
private WebView mWebView;
public BrowserFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (mWebView == null) {
mWebView = new WebView(getActivity());
}
return mWebView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return super.shouldOverrideUrlLoading(view, url);
}
});
mWebView.getSettings().setAllowFileAccess(true);
mWebView.getSettings().setBuiltInZoomControls(false);
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setSupportZoom(false);
if (savedInstanceState == null) {
mWebView.loadUrl("http://developer.android.com/");
} else {
mWebView.restoreState(savedInstanceState);
}
}
#Override
public void onResume() {
mWebView.onResume();
super.onResume();
}
#Override
public void onPause() {
super.onPause();
mWebView.onPause();
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mWebView.saveState(outState);
}
#Override
public void onDestroyView() {
if (getRetainInstance() && mWebView.getParent() instanceof ViewGroup) {
((ViewGroup) mWebView.getParent()).removeView(mWebView);
}
super.onDestroyView();
}
}
As for the MainActivity, there's nothing there besides setting the content view to the following layout file:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="#+id/fragment_browser"
android:name="com.example.app.BrowserFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</merge>
Notice above that I'm retaining the fragment instance and I'm also saving the state of the WebView so I can late restore that same state. This is what the documentation has to say about the restoreState method:
Restores the state of this WebView from the given Bundle. This method
is intended for use in onRestoreInstanceState(Bundle) and should be
called to restore the state of this WebView. If it is called after
this WebView has had a chance to build state (load pages, create a
back/forward list, etc.) there may be undesirable side-effects. Please
note that this method no longer restores the display data for this
WebView.
Meaning, the display data will not be restored but things like the scroll position will (I also tested this and it's working nicely). That's why I am creating a new instance of the WebView, only if it's null during onCreateView and removing it from the parent view during onDestroyView. This was taken from this answer: https://stackoverflow.com/a/32801278/40480
This mechanism will make the rotate process a little bit more smooth (without it, a full blank page was shown) but won't prevent the WebView from still reloading the page.
Any ideas how can I possibly solve this problem?
I had a similar problem. It's a bit late but my workaround is similar than yours but adding the webview again to the layout in onCreateView:
create new WebView if instance is null (first time) and add it to layout (in my case parent is a FrameLayout, at position 0).
if already created (activity recreated) just add to frame and invalidate (important to redraw webview on rotation)
mFrame = (FrameLayout) mView.findViewById(R.id.dq_fragment_browser_common_frame);
if (mWebView == null){
mWebView = new CommonWebView(getContext());
mFrame.addView(mWebView,0);
} else {
mFrame.addView(mWebView,0);
mWebView.invalidate();
}
As in your case I remove webview from frame in OnDestroyView() to be able to add it again in onCreateView:
#Override
public void onDestroyView() {
mFrame.removeView(mWebView);
super.onDestroyView();
}
NOTE: I don't save and store the webview state as it reloads the page (at least in my case). Furthermore, the scroll position does not normally match in portrait/landscape but same problem in Chrome as far as I've checked.
Regards.

Webview ( or 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

WebViewFragment webView is null after doing a FragmentTransaction

I currently have my application set up with a ListFragment on the left and a DetailsFragment on the right (similar to the layout on the tablet below).
On the details fragment (fragment next to the list) I have a goto deal button, which when pressed should replace the detailsFragment with a WebViewFragment.
The problem I am having is that when trying to load a url in the webviewfragment the WebView is null.
WebViewFragment webViewFragment = new WebViewFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.deal_details_fragment, webViewFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
// Set the url
if (webViewFragment.getWebView()==null)
Log.d("webviewfragment", "is null");
webViewFragment.getWebView().loadUrl("http://www.google.com");
Below is my main layout which has the original two fragments defined.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_activity_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<fragment
android:name="com.bencallis.dealpad.DealListFragment"
android:id="#+id/deal_list_fragment"
android:layout_weight="1"
android:layout_width="0px"
android:layout_height="match_parent" >
<!-- Preview: layout=#layout/deal_list_fragment -->
</fragment>
<fragment
android:name="com.bencallis.dealpad.DealDetailsFragment"
android:id="#+id/deal_details_fragment"
android:layout_weight="2"
android:layout_width="0px"
android:layout_height="match_parent" >
<!-- Preview: layout=#layout/deal_details_fragment -->
</fragment>
</LinearLayout>
It seems that the webViewFragment is not being created fully as the WebView has not been initialised. I have looked online but there is very little information regarding the WebViewFragment.
Any ideas how to ensure WebView is initialised in the WebViewFragment?
With great help from Espiandev I have managed to get a working WebView. To ensure that links opened in the fragment and not in a web browser application I created a simple InnerWebView client which extends WebViewClinet.
public class DealWebViewFragment extends Fragment {
private WebView mWebView;
private boolean mIsWebViewAvailable;
private String mUrl = null;
/**
* Creates a new fragment which loads the supplied url as soon as it can
* #param url the url to load once initialised
*/
public DealWebViewFragment(String url) {
super();
mUrl = url;
}
/**
* Called to instantiate the view. Creates and returns the WebView.
*/
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (mWebView != null) {
mWebView.destroy();
}
mWebView = new WebView(getActivity());
mWebView.setOnKeyListener(new OnKeyListener(){
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return false;
}
});
mWebView.setWebViewClient(new InnerWebViewClient()); // forces it to open in app
mWebView.loadUrl(mUrl);
mIsWebViewAvailable = true;
WebSettings settings = mWebView.getSettings();
settings.setJavaScriptEnabled(true);
return mWebView;
}
/**
* Convenience method for loading a url. Will fail if {#link View} is not initialised (but won't throw an {#link Exception})
* #param url
*/
public void loadUrl(String url) {
if (mIsWebViewAvailable) getWebView().loadUrl(mUrl = url);
else Log.w("ImprovedWebViewFragment", "WebView cannot be found. Check the view and fragment have been loaded.");
}
/**
* Called when the fragment is visible to the user and actively running. Resumes the WebView.
*/
#Override
public void onPause() {
super.onPause();
mWebView.onPause();
}
/**
* Called when the fragment is no longer resumed. Pauses the WebView.
*/
#Override
public void onResume() {
mWebView.onResume();
super.onResume();
}
/**
* Called when the WebView has been detached from the fragment.
* The WebView is no longer available after this time.
*/
#Override
public void onDestroyView() {
mIsWebViewAvailable = false;
super.onDestroyView();
}
/**
* Called when the fragment is no longer in use. Destroys the internal state of the WebView.
*/
#Override
public void onDestroy() {
if (mWebView != null) {
mWebView.destroy();
mWebView = null;
}
super.onDestroy();
}
/**
* Gets the WebView.
*/
public WebView getWebView() {
return mIsWebViewAvailable ? mWebView : null;
}
/* To ensure links open within the application */
private class InnerWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
Hopefully this is useful to someone in the future.
EDIT: So I played around with this for a while and it seems that the WVF is a bit rubbish and designed to be overridden. However, there's no documentation on this at all! The problem stems from the fact you can call getWebView() before the Fragments view is loaded, hence your NullPointerException. Except there isn't any way to detect when the Fragment's view has been loaded, so you're kind of stuck!
Instead I overrode the class, adding bits and changing bits, so that now it will work fine.
Check this link for the code. Then instead of using:
WebViewFragment webViewFragment = new WebViewFragment();
to load your Fragment, use:
ImprovedWebViewFragment wvf = new ImprovedWebViewFragment("www.google.com");
This class also includes a convenience method for loading a url, that won't throw an Exception if there's no WebView.
So, no, I don't think there's a particularly simple way for using the built-in WebViewFragment, but it is pretty easy to make something that works instead. Hope it helps!
WebViewFragment as is is not that straightforward to use. Try this simple extension (You can copy/paste):
public class UrlWebViewFragment extends WebViewFragment{
private String url;
public static UrlWebViewFragment newInstance(String url) {
UrlWebViewFragment fragment = new UrlWebViewFragment();
fragment.url = url;
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
WebView webView = (WebView) super.onCreateView(inflater, container, savedInstanceState);
webView.loadUrl(url);
return webView;
}
}
Call where you need using the factory method:
WebViewFragment fragment = UrlWebViewFragment.newInstance("http://ur-url.com");
Fragments can only be replaced if they were initiallized in Java, not XML. I think so, I had the same problem and it solved it. Change your XML to this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_activity_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<fragment
android:name="com.bencallis.dealpad.DealListFragment"
android:id="#+id/deal_list_fragment"
android:layout_weight="1"
android:layout_width="0px"
android:layout_height="match_parent" >
<!-- Preview: layout=#layout/deal_list_fragment -->
</fragment>
<View
android:id="#+id/my_container"
android:layout_weight="2"
android:layout_width="0px"
android:layout_height="match_parent" >
</View>
</LinearLayout>
and then in Java, your onCreate method:
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.my_container, new DealDetailsFragment());
transaction.commit();
or even better create whole method to just deal with Transactions.
Now Transaction from your question should work. :)

Categories

Resources