Issue
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
Solution
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.
Answered By - MDEVLP
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.