Menus

Showing posts with label android webview. Show all posts
Showing posts with label android webview. Show all posts

Wednesday, 15 January 2020

Android WebView


Android – How to display information with justify alignment?






Android WebView is used to display web page in android. The web page can be loaded from same application or URL. It is used to display online content in android activity.



WebviewActivity.java 


package com.example.root.shaneeshprograms;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;

public class WebviewActivity extends AppCompatActivity {

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

        //create html page
        String htmlPage = "<html><body style=\" text-align:justify\">" +
                "<p style=\"text-align: center; font-size: 30px; \">WebView</p>" +
                "<p style=\" font-size: 22px;  LINE_HEIGHT: 32px;\">"+
                "Android <b>WebView</b> is used to display web page in android. The web page can be loaded from same application or URL. It is used to display online content in android activity."+
                "Android WebView is used to display web page in android. The web page can be loaded from same application or URL. It is used to display online content in android activity."+
                "Android WebView is used to display web page in android. The web page can be loaded from same application or URL. It is used to display online content in android activity."+
                "</p>"+
                "<body></html>";



        //add to webview
        WebView webView = (WebView) findViewById(R.id.webView);
        webView.loadData(htmlPage,"text/html","utf-8");


    }

}



activity_webview.xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.root.shaneeshprograms.WebviewActivity">


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

</RelativeLayout>



How to load a webpage in android activity





Java code


WebView webView = (WebView)findViewById(R.id.webView);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("http://yii2ideas.blogspot.com/p/blog-page.html");

xml code

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