html_open

Open Html in android application

Hi friend we are going to learn today,how use html in android. sometime we need to use html in android project or need to open some website pages in android project. you can open html page within your application using WebView or can open website page outside of your application with device default browser. Here shown both types.

First open ‘HtmlActivity.java’

there have two button,under the two button two other function has written, one for within application and another to open website outside of the application.

Now Check The Code

HtmlActivity.java


package com.androidfromhome.html;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class HtmlActivity extends Activity implements OnClickListener {

    private Button btn_within;
    private Button btn_out;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_html);
        getWidget();
        
    }

    
    public void getWidget(){
        btn_within=(Button)findViewById(R.id.btn_within);
        btn_out=(Button)findViewById(R.id.btn_out);
        btn_out.setOnClickListener(this);
        btn_within.setOnClickListener(this);
        
}

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        
        switch (v.getId()) {
        case R.id.btn_out:
            Intent out_intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.androidfromhome.com"));
            startActivity(out_intent);
            break;
            
        case R.id.btn_within:

            Intent in_intent = new Intent(HtmlActivity.this, SecondActivity.class);
            startActivity(in_intent);
            break;

        default:
            break;
        }
        
    }
    
}


Another Activity has taken named SecondActivity.java, now check the source code of

SecondActivity.java


package com.androidfromhome.html;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;

public class SecondActivity extends Activity{
 private WebView wv_show;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);
    wv_show=(WebView)findViewById(R.id.wv_show);
    
    wv_show.loadUrl("file:///android_asset/html/index.html");
}
}


Now have a look to xml files.

activity_html.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.androidfromhome.html.HtmlActivity" >

    <LinearLayout
        android:id="@+id/LinearLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#A758AB"
        android:gravity="center"
        android:orientation="vertical"
        android:padding="15dp" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="2dp"
            android:text="Android From Home"
            android:textColor="#ffffff"
            android:textSize="22dp"
            android:textStyle="bold" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:gravity="center"
        android:orientation="vertical" >

        <Button
            android:id="@+id/btn_within"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:text="  Open Within app  " />

        <Button
            android:id="@+id/btn_out"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Open Outside of app" />

    </LinearLayout>

</LinearLayout>


activity_second.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.androidfromhome.html.HtmlActivity" >

    <LinearLayout
        android:id="@+id/LinearLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#A758AB"
        android:gravity="center"
        android:orientation="vertical"
        android:padding="15dp" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="2dp"
            android:text="Android From Home"
            android:textColor="#ffffff"
            android:textSize="22dp"
            android:textStyle="bold" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:gravity="center"
        android:orientation="vertical" >

        <WebView
            android:id="@+id/wv_show"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </LinearLayout>

</LinearLayout>


Download Source Code

Leave a Reply

Your email address will not be published. Required fields are marked *

Get Widget