09 January 2017

Android: Open Browser - Click button run URL web

activity_main.xml
Java Android 2017
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/tv_url"
        android:text="URL"
        android:layout_marginTop="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp" />

    <EditText
        android:id="@+id/edt_site"
        android:layout_toRightOf="@id/tv_url"
        android:layout_marginLeft="10dp"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:textColor="#ff0000"
        android:textSize="20sp" />

    <Button
        android:id="@+id/btn_go"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/edt_site"
        android:text="Go" />
</RelativeLayout>
MainActivity.java
Java Android 2017
package com.example.lonely.openbrowser;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText edtSite;
    private Button btnGo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        edtSite = (EditText) findViewById(R.id.edt_site);
        btnGo = (Button) findViewById(R.id.btn_go);
        btnGo.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_go:
                //TO DO
                Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://" + edtSite.getEditableText().toString()));
                startActivity(intent);
                break;
            default:
                break;
        }
    }
}
AndroidManifest.xml
Java Android 2017
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.lonely.openbrowser">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-permission android:name="android.permission.INTERNET"/>
</manifest>

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang