05 January 2017

Android: Tạo ứng dụng Web View mini

Sử dụng tool Android Studio + Genymotion
activity_main.xml
Java Android 2017
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="6"
    tools:context="com.example.lonely.demo.MainActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:background="#90e5eda1">

        <ImageButton
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="3dp"
            android:background="@null"
            android:src="@drawable/back" />

        <ImageButton
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="3dp"
            android:layout_toRightOf="@id/button1"
            android:background="@null"
            android:src="@drawable/next"
            android:visibility="visible" />

        <ImageButton
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="3dp"
            android:layout_toRightOf="@id/button2"
            android:background="@null"
            android:src="@drawable/refresh" />


        <EditText
            android:id="@+id/txtUrl"
            android:layout_width="240dp"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/button3"
            android:hint="Nhập URL"
            android:inputType="text"
            android:overScrollMode="always"
            android:scrollbarStyle="insideOverlay"
            android:scrollbars="vertical" />

        <Button
            android:id="@+id/buttonSearch"
            android:layout_width="30dp"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/txtUrl"
            android:background="@null"
            android:drawableLeft="@drawable/find" />
    </RelativeLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:layout_weight="5">

        <WebView
            android:id="@+id/txtView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="300dp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#90e5eda1"
        android:orientation="horizontal"
        android:weightSum="5">

        <ImageButton
            android:id="@+id/idGoogle"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@null"
            android:src="@drawable/google" />

        <ImageButton
            android:id="@+id/idFacebook"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@null"
            android:src="@drawable/facebook" />

        <ImageButton
            android:id="@+id/idYoutube"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@null"
            android:src="@drawable/youtube" />

        <ImageButton
            android:id="@+id/idMp3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@null"
            android:src="@drawable/mp3" />

        <ImageButton
            android:id="@+id/idAnh"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@null"
            android:src="@drawable/anh" />

    </LinearLayout>

</LinearLayout>
MainActivity.java
Java Android 2017
package com.example.lonely.demoweb;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    ImageButton imgBack, imgNext, imgReload, idGoogle, idFacebook, idYoutube, idMp3, idAnh;
    Button btnSearch;
    EditText editUrl;
    WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getValue();
        webView.setWebViewClient(new WebViewClient());
        btnSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String url = editUrl.getText().toString().trim();
                webView.loadUrl("http://." + url);
                editUrl.setText(webView.getUrl());
            }
        });
        imgBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (webView.canGoBack()) {
                    webView.goBack();
                    editUrl.setText(webView.getUrl());
                    Toast.makeText(MainActivity.this, "Trang trước", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(MainActivity.this, "Không có dữ liệu trang trước để trở về", Toast.LENGTH_SHORT).show();
                }
            }
        });
        imgNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (webView.canGoForward()) {
                    webView.goForward();
                    editUrl.setText(webView.getUrl());
                    Toast.makeText(MainActivity.this, "Trang sau", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(MainActivity.this, "Không có dữ liệu trang sau để trở về", Toast.LENGTH_SHORT).show();
                }
            }
        });
        imgReload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                webView.reload();
                editUrl.setText(webView.getUrl());

            }
        });
        idGoogle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                webView.loadUrl("http://google.com.vn");
                editUrl.setText(webView.getUrl());
            }
        });
        idFacebook.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                webView.loadUrl("https://www.facebook.com/ID1892");
                editUrl.setText(webView.getUrl());
            }
        });
        idYoutube.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                webView.loadUrl("https://www.youtube.com");
                editUrl.setText(webView.getUrl());
            }
        });
        idMp3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                webView.loadUrl("http://music.chiasenhac.vn/mp3/");
                editUrl.setText(webView.getUrl());
            }
        });
        idAnh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                webView.loadUrl("http://henkehan.deviantart.com/favourites/52175450/babes");
                editUrl.setText(webView.getUrl());
            }
        });

        WebSettings webSettings = webView.getSettings();
        webSettings.setBuiltInZoomControls(true);
        webSettings.setDisplayZoomControls(false);
        webSettings.setJavaScriptEnabled(true);
    }

    public void getValue() {
        imgBack = (ImageButton) findViewById(R.id.button1);
        imgNext = (ImageButton) findViewById(R.id.button2);
        imgReload = (ImageButton) findViewById(R.id.button3);
        btnSearch = (Button) findViewById(R.id.buttonSearch);
        editUrl = (EditText) findViewById(R.id.txtUrl);
        webView = (WebView) findViewById(R.id.txtView);

        idGoogle = (ImageButton) findViewById(R.id.idGoogle);
        idFacebook = (ImageButton) findViewById(R.id.idFacebook);
        idYoutube = (ImageButton) findViewById(R.id.idYoutube);
        idMp3 = (ImageButton) findViewById(R.id.idMp3);
        idAnh = (ImageButton) findViewById(R.id.idAnh);
    }

}
Download icon app

Download application
Hỗ trợ android 4.1.1 API 16 - 768x1280

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang