How to create Android Application for a Website using Android WebView in Android Studio?

WebView is a view in Android  that displays a browser page within the Activity. 
In this post, we are going to see how to create an android app for a blog or website.It is recommended to create the app for responsive blog or website using WebView.In below example, we are going to create the app for  www.javainstance.com.
We are using the Android Studio for this project.
Create Android application for blog or website in 5 minutes
  • Create the new project in the Android Studio.


Code:

MainActivity.java


package com.javainstance.web;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

public class MainActivity extends Activity {

    private WebView webView;
    ProgressBar progressBar;
    String url="http://www.javainstance.com";


    @Override    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        webView = (WebView) findViewById(R.id.main_webview);
        progressBar = (ProgressBar) findViewById(R.id.progressBar);
        WebSettings webSettings = webView.getSettings();

        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setUseWideViewPort(true);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setJavaScriptEnabled(true);

        webView.setWebViewClient(new myWebClient());



        webView.loadUrl(url);
    }

    public class myWebClient extends WebViewClient {
        @Override        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            // TODO Auto-generated method stub            super.onPageStarted(view, url, favicon);
        }

      @Override        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // TODO Auto-generated method stub
            view.loadUrl(url);
            return true;

        }

    }}

activity_main.xml

<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:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <ProgressBar        style="@android:style/Widget.ProgressBar.Horizontal"        android:layout_centerHorizontal="true"        android:layout_height="wrap_content"        android:layout_width="wrap_content"        android:layout_gravity="center_vertical|center_horizontal"        android:id="@+id/progressBar"/>

    <TextView        android:layout_below="@+id/progressBar"        android:layout_height="wrap_content"        android:id="@+id/LoadingText"        android:layout_width="fill_parent"        android:text="Loading, Please Wait.."        android:textSize="25sp"        android:gravity="center_horizontal">
    </TextView>

    <WebView        android:id="@+id/main_webview"        android:layout_width="wrap_content"        android:layout_height="wrap_content"/>

</RelativeLayout>



Don't forget to add below line in AndroidManifest.xml above application tag to give the internet permission to the app.

<uses-permission android:name="android.permission.INTERNET" />

You can configure App name in strings.xml.

Run the application so see your mobile app on emulator.


You may be interested in 
Create hello world app in android studio

Give your suggestion in comments.
If you like the post Share with your friends on social network.







How to create Android Application for a Website using Android WebView in Android Studio? How to create Android Application for a Website using Android WebView in Android Studio? Reviewed by JavaInstance on 11:37:00 AM Rating: 5

No comments:

Powered by Blogger.