Issue
In my simple app I have three ListItems in the ListView. One of the item from the list is called Homepage. The purpose of the creation of this item is I want that once a user click on that item, it should display a webpage in the WebView section. Let's say, after clicking on the Homepage the facebook website (facebook.com) will be displayed on the Webview. How could I do that?
My code:
MainActivity.java:
public class MainActivity extends ActionBarActivity {
TextView textView;
WebView webView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] list = {"About Company", "HomePage", "Contact"};
Adapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
ListView listView = (ListView) findViewById(R.id.listId);
listView.setAdapter((ListAdapter) adapter);
textView=(TextView)findViewById(R.id.textViewId);
webView=(WebView)findViewById(R.id.webViewId);
//click the first Item
listView.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id ) {
//String listPosition = String.valueOf(parent.getItemAtPosition(position))
textView.setText("NAME: Facebook\nADDRESS: USA);
}
}
);
//click the second item (Homepage)
//then display the facebook.com
}
}
.xml:
<
TextView
android:layout_width="match_parent"
android:layout_height="120dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/textViewId"
android:layout_centerHorizontal="true"
android:layout_marginTop="210dp"
android:background="#58b0b0"
android:textAlignment="center" />
<ListView
android:layout_width="match_parent"
android:layout_height="150dp"
android:id="@+id/listId"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="#f79727" />
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/webViewId"
android:layout_below="@+id/textViewId"
android:layout_centerHorizontal="true"
android:layout_marginTop="56dp" />
[Note: I have RelativeLayout for design. ]
Solution
First add on your AndroidManifest Internet permissions.
<uses-permission android:name="android.permission.INTERNET"/>
Now on your class:
public class MainActivity extends ActionBarActivity {
WebView webViewId;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Get webview
webViewId = (WebView) findViewById(R.id.webViewId);
webViewId.getSettings().setJavaScriptEnabled(true);
webViewId.getSettings().setLoadWithOverviewMode(true);
webViewId.getSettings().setUseWideViewPort(true);
webViewId.getSettings().setBuiltInZoomControls(true);
webViewId.getSettings().setPluginState(WebSettings.PluginState.ON);
webViewId.getSettings().setPluginsEnabled(true);
webViewId.setWebViewClient(new HelloWebViewClient());
//load url
webViewId.loadUrl("http://www.facebook.com");
//.........//
}//end oncreate
private class HelloWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
}
Now you can change webViewId.loadUrl("http://www.facebook.com");
that url for each itemlist clicked on your ListView
Advice
Don't use RelativeLayouts, use always FrameLayout or LinearLayouts It's better to adapt to different screens.
Answered By - Aspicas
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.