Showing posts with label Web Browser Demo. Show all posts
Showing posts with label Web Browser Demo. Show all posts

Create Browser using WebView with navigation button(Next and Previous) or reload button.

Hear is the code which explain, How to create Browser using WebView with navigation button Functionality.

Layout File : activity_detailed_news.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"
    tools:context="com.bhadreshtech.Activity.DetailedNewsActivity">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <WebView
            android:id="@+id/mWebNews"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/linearLayout4">
        </WebView>
        <ImageView
            android:layout_width="wrap_content"
            android:src="@drawable/my_progress_indeterminate"
            android:id="@+id/mImgProgress"
            android:layout_centerInParent="true"
            android:layout_height="wrap_content" />
        <RelativeLayout
            android:id="@+id/linearLayout4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true">
            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_alignTop="@+id/mImgRefresh"
                android:layout_alignBottom="@+id/mImgRefresh"
                android:layout_height="wrap_content"
                android:layout_toLeftOf="@+id/mImgRefresh"
                android:layout_toStartOf="@+id/mImgRefresh">
                <TextView
                    android:id="@+id/mLblPrevious"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:layout_gravity="center_vertical"
                    android:padding="10dp"
                    android:gravity="center"
                    android:text="Previous"
                    android:textColor="@color/blue_light"
                    android:textSize="15dp" />
                <TextView
                    android:id="@+id/mLblNext"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="match_parent"
                    android:layout_gravity="center_vertical"
                    android:padding="10dp"
                    android:textColor="@color/blue_light"
                    android:gravity="center"
                    android:paddingLeft="20dp"
                    android:text="Next"
                    android:textSize="15dp" />
            </LinearLayout>
            <ImageView
                android:id="@+id/mImgRefresh"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_gravity="right"
                android:padding="10dp"
                android:src="@drawable/reload_icon"
                android:textSize="15dp" />
        </RelativeLayout>
    </RelativeLayout>
</RelativeLayout>


Class File : DetailedNewsActivity


package com.bhadreshtech.Activity;

import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.bhadreshtech.R;

public class DetailedNewsActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView mLblTitleAction;
    private ImageView mImgMenuToggle;
    private ImageView mImgAction;
    private String url = "http://www.google.com";
    private int from = 0;
    private WebView mWebNews;
    private ImageView mImgBacks, mImgRefresh;
    private TextView mLblNext, mLblPrevious;
    private ImageView mImgProgress;
    private boolean blag_progress;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detailed_news);
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            url = bundle.getString("url") + "";
            from = bundle.getInt("from");
        }
        initializeView();
        setClickEvent();

    }

    private void setClickEvent() {
        mImgRefresh.setOnClickListener(this);
        mLblNext.setOnClickListener(this);
        mLblPrevious.setOnClickListener(this);
        mImgMenuToggle.setOnClickListener(this);
    }

    private void initializeView() {
        getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME);
        getSupportActionBar().setCustomView(R.layout.custome_actiobar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(false);
        mLblTitleAction = (TextView) findViewById(R.id.mLblTitleAction);
        mLblTitleAction.setText("News Detail");
        mImgProgress = (ImageView) findViewById(R.id.mImgProgress);
        mImgProgress.setVisibility(View.VISIBLE);
        mImgMenuToggle = (ImageView) findViewById(R.id.mImgMenuToggle);
        mImgMenuToggle.setImageDrawable(getResources().getDrawable(R.drawable.cloase_bt));
        mImgAction = (ImageView) findViewById(R.id.mImgAction);
        mImgAction.setVisibility(View.INVISIBLE);
        mImgRefresh = (ImageView) findViewById(R.id.mImgRefresh);
        mLblNext = (TextView) findViewById(R.id.mLblNext);
        mLblPrevious = (TextView) findViewById(R.id.mLblPrevious);
        intializeWebView();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.mImgRefresh:
                if (blag_progress) {
                    mWebNews.onPause();
                    mImgProgress.setVisibility(View.GONE);
                    mImgRefresh.setImageDrawable(getResources().getDrawable(R.drawable.reload_icon));
                    blag_progress = false;
                } else {
                    mImgProgress.setVisibility(View.VISIBLE);
                    mWebNews.reload();
                    mImgRefresh.setImageDrawable(getResources().getDrawable(R.drawable.close_icon));
                    blag_progress = true;
                }
                break;
            case R.id.mImgMenuToggle:
                this.finish();
                break;
            case R.id.mLblNext:
                if (mWebNews.canGoForward()) {
                    mWebNews.goForward();
                }
                break;
            case R.id.mLblPrevious:
                if (mWebNews.canGoBack()) {
                    mWebNews.goBack();
                }
                break;
        }
    }

    private void intializeWebView() {
        mWebNews = (WebView) findViewById(R.id.mWebNews);
        mWebNews.getSettings().setJavaScriptEnabled(true);
        mWebNews.loadUrl(url);
        //mWebNews.loadUrl("file:///android_asset/ppp1.html");
        mWebNews.setWebViewClient(new WebViewClient());
        mWebNews.setInitialScale(1);
        mWebNews.getSettings().setBuiltInZoomControls(true);
        mWebNews.getSettings().setUseWideViewPort(true);
        mWebNews.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
                if (progress >= 80) {
                    mImgRefresh.setImageDrawable(getResources().getDrawable(R.drawable.reload_icon));
                    blag_progress = false;
                    mImgProgress.setVisibility(View.GONE);
                } else {
                    mImgRefresh.setImageDrawable(getResources().getDrawable(R.drawable.close_icon));
                    blag_progress = true;
                    mImgProgress.setVisibility(View.VISIBLE);
                }

            }
        });

    }


}