Blog of King Software

Learn online with us in simple steps.

CREATE AN APP OF WEBSITE IN ANDROID STUDIO


Created by Bhavesh Gohil & Rikita Vasava on Date: 2/12/2021

If you want to create an android app of some website then you can do it in Android Studio following below steps:
STEP 1: Open Android Studio Software in PC:

STEP 2: Setup of new project
  • Go to the File in Menu. Select New & click on New Project.
  • Select a Project Template window will appear.
  • Choose Template according to your need.
  • Here, we select Empty Activity option and click on Next

STEP 3: Setup Configuration of the project.
  • Configure your project window will appear.
  • Give name to your project.
  • Package name will be created automatically when you enter the Project Name. We will take it as provided by the system.
  • In Save location, you can save your project to defined location. We will keep it as provided by default by system.
  • Select Language from dropdown. We will choose Java for this example.
  • Minimum SDK is for to define that on which version of android and its above this app will run. Here, we will select API 16:Android 4.1 (Jelly Bean).
  • Click on Finish button.

STEP 4: activity_main.xml file
  • Select activity_main.xml from app->res->layout->activity_main.xml.
    or activity_main.xml is already open when project is created. Select it from the open tab.

STEP 5: Set the Webview option.
  • Click on Design if Design View is not opened by default.
  • Click on Widgets to add Webview.
  • Select Webview from the given list & drag and drop into the design window. Delete if any other widget is already add.

STEP 6: Add code in the activity_main.xml file.
  • Click on Code from the top-right corner of the window to add code in the activity_main.xml file.
  • Here, add the below code in the fill.

FILE NAME:   activity_main.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=".MainActivity">


<WebView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/kingsoftware"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>


Note: Give id according to your project. You have to give id by yourself in code.

STEP 7: Edit the code in MainActivity.java file
  • Open MainActivity.java from app->java->com.example.projectname->MainActivity file.
  • Write below code in a class. You have to write code by yourself because the files will be imported automatically during the code writing.

FILE NAME:   MainActivity.java

package com.example.kingsoftware_vacancy;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {
private WebView mykingsoftware;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mykingsoftware = (WebView)findViewById(R.id.kingsoftware);
WebSettings webSettings = mykingsoftware.getSettings();
webSettings.setJavaScriptEnabled(true);
mykingsoftware.loadUrl("https://kingsoftware.co.in/Vacancy");
mykingsoftware.setWebViewClient(new WebViewClient());
}
@Override
public void onBackPressed(){
if (mykingsoftware.canGoBack()){
mykingsoftware.goBack();
}else {
super.onBackPressed();
}
}
}


  • In private WebView mykingsoftware line, the mykingsoftware is a user define name of Webview. You can give name of your choice.
  • In R.id.kingsoftware line of code, the kingsoftware id is the id which we have given in the activity_main.xml file.
  • In loadUrl() function, you have to give the url which you want to create android app of.
  • onBackPressed() function is used for to stay in the app after you click any other link in the app or click back. It is used to prevent to redirect into the webview in browser.

Note: You don't have to write import file manually, it will automatically added when you write the code. You just have to click on suggestion of methods while writing the code.

STEP 8: Add code in AndroidManifest.xml file.
  • Go to the AndroidManifest.xml from app->manifests->AndroidManifest.xml file.
  • Here, you have to give label name, label will be same as project name. By default it will already given by system according to your project name. But if not given than give it same as project name.
  • Add one line of code below <manifest> tag.
  • //Code to add

    <uses-permission android : name = "android.permission.INTERNET">
    </uses-permission>

STEP 9: Setup icon for the app
  • Go to the ImageAsset from app->res->drawable->RIght click->New->Image Asset to add icon of app.
  • Configure Image Asset window will appear.
  • In Foreground Layer:
  • Asset Type will be Image
  • To add icon, click on folder icon in path and than select your image from your PC.
  • After you select logo, the logo will appear in preview in different shapes. You can Resize logo in Scaling option from same window.
  • Click on Next and than click on Finish.
STEP 10: Build APK file.
  • To build APK go to the Build Bundle(s)/APK(s) from Build->Build Bundle(s)/APK(s)->Build APK(s) from main menu.
  • After APK will created the small dialogue box will appear on the right-bottom corner of the screen. To see your downloaded APK click on locate from the box and rename it.
  • Now install the APK in your phone and run it.