Here i'm going to show you how to create Browser App in Android.
Functionality in App :
Example ::
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:focusableInTouchMode="true"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/editTextURL"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="example.com" />
<Button
android:id="@+id/buttonGo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go" />
</LinearLayout>
<WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"></WebView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="4">
<Button
android:id="@+id/buttonBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Back" />
<Button
android:id="@+id/buttonForward"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Forward" />
<Button
android:id="@+id/buttonReload"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Reload" />
<Button
android:id="@+id/buttonClearHistory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="C.H." />
</LinearLayout>
</LinearLayout>
Functionality in App :
- Back Button
- Forward Button
- Reload Button
- Clear History Button
- Search Button
- Hide KeyBoard while Press Go Button
- Hide Keyboard Focus when activity_main.xml file first open
Example ::
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:focusableInTouchMode="true"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/editTextURL"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="example.com" />
<Button
android:id="@+id/buttonGo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go" />
</LinearLayout>
<WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"></WebView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="4">
<Button
android:id="@+id/buttonBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Back" />
<Button
android:id="@+id/buttonForward"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Forward" />
<Button
android:id="@+id/buttonReload"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Reload" />
<Button
android:id="@+id/buttonClearHistory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="C.H." />
</LinearLayout>
</LinearLayout>
MainActivity.java
package com.example.malik.blog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.IOException;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
EditText editTextUrl;
WebView webView;
Button btnGo, btnBack, btnForward, btnReload, btnClearHistory;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextUrl = (EditText) findViewById(R.id.editTextURL);
btnGo = (Button) findViewById(R.id.buttonGo);
btnBack = (Button) findViewById(R.id.buttonBack);
btnForward = (Button) findViewById(R.id.buttonForward);
btnReload = (Button) findViewById(R.id.buttonReload);
btnClearHistory = (Button) findViewById(R.id.buttonClearHistory);
webView = (WebView) findViewById(R.id.webView1);
btnGo.setOnClickListener(this);
btnBack.setOnClickListener(this);
btnForward.setOnClickListener(this);
btnReload.setOnClickListener(this);
btnClearHistory.setOnClickListener(this);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("http://www.google.com");
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.buttonGo:
String URL = editTextUrl.getText().toString();
webView.loadUrl("http://www." + URL);
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(editTextUrl.getWindowToken(), 0);
break;
case R.id.buttonBack:
if (webView.canGoBack()) {
webView.goBack();
}
break;
case R.id.buttonForward:
if (webView.canGoForward()) {
webView.goForward();
}
break;
case R.id.buttonReload:
webView.reload();
break;
case R.id.buttonClearHistory:
webView.clearHistory();
break;
}
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.malik.blog">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
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>
</manifest>
build.gradle
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.malik.blog">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
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>
</manifest>