Android - Simple Web Viewer

Operating System
In this tutorial we will try to create a Simple Web Viewer using Android. This simple app can search through any sites as a browser. It used in several gadget like smartphone, tablet, and even television. Android is open source to developers who has an interest in developing mobile apps. It also provide an adaptive framework that allow the developer to develop an apps in a simpler way. So let's now do the coding...

Getting Started:

First you will have to download & install the Android Development IDE (Android Studio or Eclipse). Android Studio is an open source development feel free to develop your things. Here's the link for the Android Studio https://developer.android.com/studio/index.html

Layout Design

We will now create the design for the application, first locate the layout file called activity_main.xml, this is the default name when create a new activity. Then write these codes inside your layout file.
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"
  4.     xmlns:tools="http://schemas.android.com/tools"
  5.     android:layout_width="match_parent"
  6.     android:layout_height="match_parent"
  7.     tools:context="com.razormist.simplewebviewer.MainActivity">
  8.  
  9.     <Button
  10.         android:id="@+id/btn_search"
  11.         android:layout_width="wrap_content"
  12.         android:layout_height="wrap_content"
  13.         android:text="Search"
  14.         android:layout_toRightOf="@+id/et_url"
  15.         android:layout_alignParentRight="true"/>
  16.  
  17.     <EditText
  18.         android:id="@+id/et_url"
  19.         android:layout_width="wrap_content"
  20.         android:layout_height="wrap_content"
  21.         android:inputType="text"
  22.         android:lines="1"
  23.         android:ems="14"
  24.         android:maxLines="1"
  25.         android:layout_alignParentTop="true"
  26.         android:hint="https://www."/>
  27.  
  28.     <WebView
  29.         android:id="@+id/wv_browser"
  30.         android:layout_width="match_parent"
  31.         android:layout_height="match_parent"
  32.         android:layout_centerHorizontal="true"
  33.         android:layout_below="@+id/et_url" />
  34. </RelativeLayout>

Android Manifest File

The Android Manifest file provides essential information about your app to the Android system in which the system must required before running the code. It describe the overall information about the application. It contains some libraries that needed to access the several method within the app.
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3.     package="com.razormist.simplewebviewer">
  4.     <uses-permission android:name="android.permission.INTERNET"/>
  5.     <application
  6.         android:allowBackup="true"
  7.         android:icon="@mipmap/ic_launcher"
  8.         android:label="@string/app_name"
  9.         android:roundIcon="@mipmap/ic_launcher_round"
  10.         android:supportsRtl="true"
  11.         android:theme="@style/AppTheme">
  12.         <activity android:name=".MainActivity"
  13.             android:configChanges="orientation"
  14.             android:screenOrientation="portrait">
  15.             <intent-filter>
  16.                 <action android:name="android.intent.action.MAIN" />
  17.  
  18.                 <category android:name="android.intent.category.LAUNCHER" />
  19.             </intent-filter>
  20.         </activity>
  21.     </application>
  22.  
  23. </manifest>

The Main Function

This code contains the main function of the application. This code will try to render the website into the WebView when you clicked the button. To start with first locate your MainActivity java file and open it, then write these some important variables inside the MainActivity class.
  1. private Button btn_search;
  2. private EditText et_url;
  3. private WebView wv_browser;
Then write these several method inside the activity class to make the apps work properly.
  1. public void SearchSite(){
  2.         btn_search = (Button)findViewById(R.id.btn_search);
  3.         et_url = (EditText)findViewById(R.id.et_url);
  4.         wv_browser = (WebView)findViewById(R.id.wv_browser);
  5.  
  6.         btn_search.setOnClickListener(new View.OnClickListener() {
  7.             @Override
  8.             public void onClick(View v) {
  9.                 String url = "https://www." + et_url.getText().toString();
  10.                 wv_browser.getSettings().setLoadsImagesAutomatically(true);
  11.                 wv_browser.getSettings().setJavaScriptEnabled(true);
  12.                 wv_browser.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
  13.                 wv_browser.setWebViewClient(new WebViewClient());
  14.                 wv_browser.loadUrl(url);
  15.             }
  16.         });
  17.     }
Finally, initialize all the methods inside the onCreate method to run the application.
  1. SearchSite();
Try to run the app and see if it worked. Note: Make sure you have an internet connection to test if it browse to the internet. There you have it we create a Simple Web Viewer using Android. I hope that this tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Tags

Add new comment