Sunday, February 21, 2016

WebView android studio

WebView


  1. Open the activity_main.xml file in the src/main/res/layout directory if it is not already open. (You may also see afragment_main.xml file. You can ignore this, as it's not required for this tutorial.)
    Select the Text tab at the bottom of the of the activity_main.xml editor to see the XML markup.
    This file defines the layout for your main activity, and the Preview panes show the a preview of the activity. TheBlank Activity layout doesn't include any children. You'll need to add the WebView.
  2. In the XML pane, remove the self-closing slash from the end of the FrameLayout element, and add the <WebView> element and a new closing tag, as shown:
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:tools="http://schemas.android.com/tools"
         android:id="@+id/container"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         tools:context=".MainActivity">
         tools:ignore="MergeRootFrame">
         
             <WebView
             android:id="@+id/activity_main_webview"
             android:layout_width="match_parent"
             android:layout_height="match_parent" />
    </FrameLayout>
  3. To use the WebView you need to reference it in the Activity. Open the Java source file for the main activity,MainActivity.java in the src/main/java/<PackageName> directory.
    Add the lines shown in bold.
    public class MainActivity extends Activity {
        
        private WebView mWebView;
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            mWebView = (WebView) findViewById(R.id.activity_main_webview);
    
    The existing code in the onCreate method does the work of hooking up the Activity with the layout. The added lines create a new member variable, mWebView, to refer to the web view.
    Remove the following code:
    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
            .add(R.id.container, new PlaceholderFragment())
            .commit();
    }
    The WebView is identified by the resource ID, which is specified by this line in the layout file:
    android:id="@+id/activity_main_webview"
    After adding the code, you'll see some warning messages in the margin of the editor. This is because you haven't imported the right classes for WebView. Luckily Android Studio can help you fill in the missing classes. The easiest way to do this is click and hover over an unknown class name and wait for a popup showing a "quick fix" -- in this case, adding an import statment for the WebView class.
    Press Alt + Enter (Option + Enter on Mac) to accept the quick fix.
    WebView in hand you can move on to setting it up and and loading some juicy web content.
WebViews don't allow JavaScript by default. To run a web application in the web view, you need to explicitly enable JavaScript by adding the following lines to the onCreate method:
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);

No comments:

Post a Comment

Comments system

Disqus Shortname

Disqus Shortname

Comments system