Wednesday, 28 October 2015

Using HTML pages loading in Android

activity_main.xml
-----------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
   
xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:tools="http://schemas.android.com/tools"
   
xmlns:app="http://schemas.android.com/apk/res-auto"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
app:layout_behavior="@string/appbar_scrolling_view_behavior"
   
tools:showIn="@layout/activity_main"
   
tools:context=".MainActivity"
   
android:orientation="vertical"
   
android:background="#c78bd3">

    <
Button
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:text="Load Html Page"
       
android:id="@+id/button"
        
android:layout_gravity="center_horizontal"
       
android:layout_marginTop="5dp" />

    <
WebView
       
android:layout_width="match_parent"
       
android:layout_height="fill_parent"
       
android:id="@+id/webView"
       
android:layout_marginTop="20dp" />
</
LinearLayout>


MainActivity.Java
-----------------------
package com.android.anil.htmlpagesinandroid;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    Button
loadhtmlpage;
    WebView
webView;
   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.
toolbar);
        setSupportActionBar(toolbar);
       
webView=(WebView)findViewById(R.id.webView);
       
loadhtmlpage=(Button)findViewById(R.id.button);
       
loadhtmlpage.setOnClickListener(new View.OnClickListener() {
           
@Override
           
public void onClick(View v) {
               
webView.loadUrl("file:///android_asset/myfile.html");

            }
        });

    }
}


Output is:
-------------------------


No comments:

Post a Comment