Skip to content Skip to sidebar Skip to footer

How to Read a Websites Javascript on a Android

Hither, we are going to brand an application for the "GeeksForGeeks" website. By making this awarding we will be able to learn that how we can catechumen a website to an Android Application simply past following simple steps. You can use this concept for your personal website too and learn something new.

How-to-Convert-Any-Website-to-Android-App-in-Android-Studio

What we are going to build in this commodity?

In this application, we volition learn that how we tin use different portals of a website and show them equally fragments in our android application. In this application 3 portals of Geeksforgeeks website- Habitation, Practice and Contribute will be used as fragments in our application. So, that you lot tin can come across a live example to convert a website to an awarding. The concept of WebView is used to do this desired work. A sample video is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language.

Simple Steps to Convert Your Website into an Android Application:

  • To add the logo of your awarding.
  • To add together a splash screen to your awarding.
  • To use Navigation drawer in our awarding so that, dissimilar portals of our website can exist used every bit fragments in the navigation drawer.
  • To utilise a WebView so that, the web content can be accessed easily.
  • To use WebViewController class then that the content on the Website tin be straight shown in the awarding rather than opening it in the browser.
  • To add a helpline activity.

And by following these steps you can convert your website to an application in the simplest mode. And then, let united states of america come across step-past-stride implementation to convert GeeksForGeeks Website to an application.

Footstep by Step Implementation

Footstep 1: Creating a new project

  • Open a new project.
  • We will be working on Navigation Drawer Activity with language every bit Java. Leave all other options unchanged.
  • You can modify the name of the project at your convenience.
  • At that place volition be many default files.

If you don't know how to create a new project in Android Studio then you tin refer to How to Create/Start a New Project in Android Studio?

Footstep 2: To add together a logo to our application

  • Paste the logo of your awarding in res > drawable.
  • Follow "How to modify the default icon of Android App" for further steps.

Step 3: To add Splash Screen to our awarding

Follow-"Creating a Splash Screen" to learn how to add a splash screen to our awarding. Sample design of splash screen of our application.

Pace iv: Working with xml files

Open up layout > nav_header_main.xml file to design the header of our Navigation Drawer. For that utilise the post-obit code in it.

XML

<? xml version = "1.0" encoding = "utf-8" ?>

< androidx.constraintlayout.widget.ConstraintLayout

android:layout_width = "match_parent"

android:layout_height = "@dimen/nav_header_height"

android:background = "#6C6B74"

android:gravity = "lesser"

android:orientation = "vertical"

android:paddingLeft = "@dimen/activity_horizontal_margin"

android:paddingTop = "@dimen/activity_vertical_margin"

android:paddingRight = "@dimen/activity_horizontal_margin"

android:paddingBottom = "@dimen/activity_vertical_margin"

android:theme = "@way/ThemeOverlay.AppCompat.Night" >

< ImageView

android:id = "@+id/imageView"

android:layout_width = "107dp"

android:layout_height = "87dp"

android:layout_gravity = "centre"

android:contentDescription = "@cord/nav_header_desc"

android:foregroundGravity = "center"

android:paddingTop = "@dimen/nav_header_vertical_spacing"

app:layout_constraintBottom_toBottomOf = "parent"

app:layout_constraintEnd_toEndOf = "parent"

app:layout_constraintStart_toStartOf = "parent"

app:layout_constraintTop_toTopOf = "parent"

app:layout_constraintVertical_bias = "0.247"

app:srcCompat = "@drawable/gfg_round" />

< TextView

android:layout_width = "wrap_content"

android:layout_height = "51dp"

android:layout_gravity = "middle"

android:gravity = "center"

android:paddingTop = "@dimen/nav_header_vertical_spacing"

android:text = "GeeksForGeeks"

android:textAppearance = "@style/TextAppearance.AppCompat.Body1"

android:textColor = "#01A109"

android:textSize = "24sp"

android:textStyle = "bold"

app:layout_constraintBottom_toBottomOf = "parent"

app:layout_constraintEnd_toEndOf = "parent"

app:layout_constraintStart_toStartOf = "parent"

app:layout_constraintTop_toTopOf = "@+id/imageView"

app:layout_constraintVertical_bias = "1.0" />

</ androidx.constraintlayout.widget.ConstraintLayout >

After implementing the above lawmaking, the header of the Navigation Drawer looks like this:

Alter the color of the Activeness Bar to "#6C6B74" and then that it tin friction match the color lawmaking of the logo of our application and our UI tin become more attractive. If you do non know how to modify the color of the ActionBar then you tin click here to learn it. Open up card > activity_main_drawer.xml file and utilise the following code in it and then that nosotros can add together different items(portals of our website) to our navigation drawer and employ their fragments.

XML

<? xml version = "ane.0" encoding = "utf-8" ?>

< group android:checkableBehavior = "unmarried" >

< item

android:id = "@+id/nav_home"

android:icon = "@drawable/dwelling"

android:menuCategory = "secondary"

android:title = "@string/menu_home" />

< item

android:id = "@+id/nav_gallery"

android:icon = "@drawable/practice_gfg"

android:menuCategory = "secondary"

android:title = "Practise" />

< detail

android:id = "@+id/nav_slideshow"

android:icon = "@drawable/contribute_gfg"

android:menuCategory = "secondary"

android:championship = "Contribute" />

</ group >

</ menu >

Subsequently implementing the above code our design of the activity_main_drawer.xml file looks like this.

Get to the layout > activity_main.xml and use the following lawmaking in it.

XML

<? xml version = "1.0" encoding = "utf-8" ?>

< androidx.drawerlayout.widget.DrawerLayout

android:id = "@+id/drawer_layout"

android:layout_width = "match_parent"

android:layout_height = "match_parent"

android:fitsSystemWindows = "truthful"

tools:openDrawer = "start" >

< include

layout = "@layout/app_bar_main"

android:layout_width = "match_parent"

android:layout_height = "match_parent" />

< com.google.android.material.navigation.NavigationView

android:id = "@+id/nav_view"

android:layout_width = "wrap_content"

android:layout_height = "match_parent"

android:layout_gravity = "start"

android:groundwork = "#2E303E"

android:fitsSystemWindows = "true"

app:headerLayout = "@layout/nav_header_main"

app:itemIconTint = "#fff"

app:itemTextColor = "#fff"

app:card = "@carte/activity_main_drawer" />

</ androidx.drawerlayout.widget.DrawerLayout >

After implementing the above code design of activity_main.xml file looks like.

Go to the navigation > mobile_navigation.xml file and utilise the post-obit code in it and so that we can specify the title and label of our website portals and can easily employ them in java files.

XML

<? xml version = "1.0" encoding = "utf-8" ?>

< navigation

android:id = "@+id/mobile_navigation"

app:startDestination = "@+id/nav_home" >

< fragment

android:id = "@+id/nav_home"

android:name = "com.example.geeksforgeeks.ui.home.HomeFragment"

android:characterization = "@cord/menu_home"

tools:layout = "@layout/fragment_home" />

< fragment

android:id = "@+id/nav_gallery"

android:name = "com.instance.geeksforgeeks.ui.gallery.GalleryFragment"

android:characterization = "Practise"

tools:layout = "@layout/fragment_gallery" />

< fragment

android:id = "@+id/nav_slideshow"

android:name = "com.example.geeksforgeeks.ui.slideshow.SlideshowFragment"

android:characterization = "Contribute"

tools:layout = "@layout/fragment_slideshow" />

</ navigation >

Now information technology's time to insert WebView in all the fragments – Open fragment_home, fragment_gallery, fragment_slideshow XML files and use the code respectively.

XML

<? xml version = "1.0" encoding = "utf-8" ?>

< androidx.constraintlayout.widget.ConstraintLayout

android:layout_width = "match_parent"

android:layout_height = "match_parent"

tools:context = ".ui.dwelling house.HomeFragment" >

< WebView

android:id = "@+id/web_view_home"

android:layout_width = "match_parent"

android:layout_height = "match_parent"

app:layout_constraintBottom_toBottomOf = "parent"

app:layout_constraintEnd_toEndOf = "parent"

app:layout_constraintStart_toStartOf = "parent"

app:layout_constraintTop_toTopOf = "parent" />

</ androidx.constraintlayout.widget.ConstraintLayout >

XML

<? xml version = "1.0" encoding = "utf-8" ?>

< androidx.constraintlayout.widget.ConstraintLayout

android:layout_width = "match_parent"

android:layout_height = "match_parent"

tools:context = ".ui.gallery.GalleryFragment" >

< WebView

android:id = "@+id/web_view_practice"

android:layout_width = "match_parent"

android:layout_height = "match_parent"

app:layout_constraintBottom_toBottomOf = "parent"

app:layout_constraintEnd_toEndOf = "parent"

app:layout_constraintHorizontal_bias = "0.0"

app:layout_constraintStart_toStartOf = "parent"

app:layout_constraintTop_toTopOf = "parent"

app:layout_constraintVertical_bias = "0.0" />

</ androidx.constraintlayout.widget.ConstraintLayout >

XML

<? xml version = "1.0" encoding = "utf-8" ?>

< androidx.constraintlayout.widget.ConstraintLayout

android:layout_width = "match_parent"

android:layout_height = "match_parent"

tools:context = ".ui.slideshow.SlideshowFragment" >

< WebView

android:id = "@+id/web_view_contribute"

android:layout_width = "match_parent"

android:layout_height = "match_parent"

app:layout_constraintBottom_toBottomOf = "parent"

app:layout_constraintEnd_toEndOf = "parent"

app:layout_constraintStart_toStartOf = "parent"

app:layout_constraintTop_toTopOf = "parent" />

</ androidx.constraintlayout.widget.ConstraintLayout >

Now we have to create a new Activity named "help". So that, the application user tin can get info to take help from the service provider. Become to layout > right-click > new > activity > Empty Activity.

Put the name of the activity according to your choice(name used in this application-"aid"). Open layout > activity_help.xml and utilise the following code in it.

XML

<? xml version = "1.0" encoding = "utf-eight" ?>

< androidx.constraintlayout.widget.ConstraintLayout

android:layout_width = "match_parent"

android:layout_height = "match_parent"

android:background = "#6C6B74"

tools:context = ".help" >

< ImageView

android:id = "@+id/imageView3"

android:layout_width = "255dp"

android:layout_height = "173dp"

app:layout_constraintBottom_toBottomOf = "parent"

app:layout_constraintEnd_toEndOf = "parent"

app:layout_constraintStart_toStartOf = "parent"

app:layout_constraintTop_toTopOf = "parent"

app:layout_constraintVertical_bias = "0.222"

app:srcCompat = "@drawable/aid" />

< TextView

android:id = "@+id/textView2"

android:layout_width = "371dp"

android:layout_height = "95dp"

android:background = "#2E303E"

android:text = "For any kind of queries or help you lot tin can contact us at-"

android:textColor = "#FFFCFC"

android:textSize = "30sp"

app:layout_constraintBottom_toBottomOf = "parent"

app:layout_constraintEnd_toEndOf = "parent"

app:layout_constraintStart_toStartOf = "parent"

app:layout_constraintTop_toBottomOf = "@+id/imageView3"

app:layout_constraintVertical_bias = "0.296" />

< TextView

android:id = "@+id/textView3"

android:layout_width = "393dp"

android:layout_height = "59dp"

android:groundwork = "#2E303E"

android:text = "careers@geeksforgeeks.org"

android:textColor = "#FFFFFF"

android:textSize = "30sp"

app:layout_constraintBottom_toBottomOf = "parent"

app:layout_constraintEnd_toEndOf = "parent"

app:layout_constraintHorizontal_bias = "0.666"

app:layout_constraintStart_toStartOf = "parent"

app:layout_constraintTop_toBottomOf = "@+id/textView2"

app:layout_constraintVertical_bias = "0.159" />

</ androidx.constraintlayout.widget.ConstraintLayout >

Afterwards implementing the above code, the design of the activity_help.xml file looks like this.

Now we take added a piece of lawmaking to have permission for access to the net then that our WebView can work easily. Become to manifests > AndroidManifest.xml file and add the following code to it.

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

Step v: Working with java files

Create a new coffee class every bit shown beneath and name it as "WebViewController"

Use the following code in the WebViewController.java file so that code to utilise the URL of a website tin can be executed.

Java

package com.example.geeksforgeeks;

import android.webkit.WebView;

import android.webkit.WebViewClient;

public form WebViewController extends WebViewClient {

@Override

public boolean shouldOverrideUrlLoading(WebView view, String url) {

view.loadUrl(url);

render true ;

}

}

Open up HomeFragment, GalleryFragment, SlideshowFragment java files and utilise the code respectively.

Java

parcel com.example.geeksforgeeks.ui.habitation;

import android.bone.Bundle;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.webkit.WebView;

import androidx.notation.NonNull;

import androidx.fragment.app.Fragment;

import androidx.lifecycle.ViewModelProvider;

import com.example.geeksforgeeks.R;

import com.example.geeksforgeeks.WebViewController;

public grade HomeFragment extends Fragment {

individual HomeViewModel homeViewModel;

public View onCreateView( @NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

homeViewModel = new ViewModelProvider( this ).get(HomeViewModel. grade );

View root = inflater.inflate(R.layout.fragment_home, container, false );

WebView webView = root.findViewById(R.id.web_view_home);

webView.setWebViewClient( new WebViewController());

return root;

}

}

Java

bundle com.example.geeksforgeeks.ui.gallery;

import android.bone.Bundle;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.webkit.WebView;

import androidx.annotation.NonNull;

import androidx.fragment.app.Fragment;

import androidx.lifecycle.ViewModelProvider;

import com.example.geeksforgeeks.R;

import com.case.geeksforgeeks.WebViewController;

public class GalleryFragment extends Fragment {

private GalleryViewModel galleryViewModel;

public View onCreateView( @NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

galleryViewModel = new ViewModelProvider( this ).get(GalleryViewModel. course );

View root = inflater.inflate(R.layout.fragment_gallery, container, false );

WebView webView = root.findViewById(R.id.web_view_practice);

webView.setWebViewClient( new WebViewController());

render root;

}

}

Coffee

package com.case.geeksforgeeks.ui.slideshow;

import android.os.Bundle;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.webkit.WebView;

import androidx.annotation.NonNull;

import androidx.fragment.app.Fragment;

import androidx.lifecycle.ViewModelProvider;

import com.example.geeksforgeeks.R;

import com.example.geeksforgeeks.WebViewController;

public class SlideshowFragment extends Fragment {

private SlideshowViewModel slideshowViewModel;

public View onCreateView( @NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

slideshowViewModel = new ViewModelProvider( this ).become(SlideshowViewModel. grade );

View root = inflater.inflate(R.layout.fragment_slideshow, container, false );

WebView webView = root.findViewById(R.id.web_view_contribute);

webView.setWebViewClient( new WebViewController());

return root;

}

}

Now all of our work is done and the last work is to connect the help action to the floating button in our application with aid of intent in the MainActivity.java file. Use the following code to do and then.

Java

package com.example.geeksforgeeks;

import android.content.Intent;

import android.bone.Packet;

import android.view.Bill of fare;

import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

import androidx.appcompat.widget.Toolbar;

import androidx.drawerlayout.widget.DrawerLayout;

import androidx.navigation.NavController;

import androidx.navigation.Navigation;

import androidx.navigation.ui.AppBarConfiguration;

import androidx.navigation.ui.NavigationUI;

import com.google.android.material.floatingactionbutton.FloatingActionButton;

import com.google.android.material.navigation.NavigationView;

public grade MainActivity extends AppCompatActivity {

individual AppBarConfiguration mAppBarConfiguration;

@Override

protected void onCreate(Packet savedInstanceState) {

super .onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Toolbar toolbar = findViewById(R.id.toolbar);

setSupportActionBar(toolbar);

FloatingActionButton fab = findViewById(R.id.fab);

fab.setOnClickListener( new View.OnClickListener() {

@Override

public void onClick(View view) {

Intent activity2Intent = new Intent(getApplicationContext(), assistance. form );

startActivity(activity2Intent);

}

});

DrawerLayout drawer = findViewById(R.id.drawer_layout);

NavigationView navigationView = findViewById(R.id.nav_view);

mAppBarConfiguration = new AppBarConfiguration.Builder(

R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow)

.setDrawerLayout(drawer)

.build();

NavController navController = Navigation.findNavController( this , R.id.nav_host_fragment);

NavigationUI.setupActionBarWithNavController( this , navController, mAppBarConfiguration);

NavigationUI.setupWithNavController(navigationView, navController);

}

@Override

public boolean onCreateOptionsMenu(Menu bill of fare) {

getMenuInflater().inflate(R.bill of fare.chief, card);

return true ;

}

@Override

public boolean onSupportNavigateUp() {

NavController navController = Navigation.findNavController( this , R.id.nav_host_fragment);

return NavigationUI.navigateUp(navController, mAppBarConfiguration)

|| super .onSupportNavigateUp();

}

}

Annotation: In MainActivity.coffee whole code is by default or pre-existing we have simply added intent code to connect aid activity with the floating button.

Output:

If y'all want to have aid or import the project then you tin can visit the GitHub link: https://github.com/Karan-Jangir/GeeksForGeeks/tree/master


wrighttheor1980.blogspot.com

Source: https://www.geeksforgeeks.org/how-to-convert-any-website-to-android-app-in-android-studio/

Post a Comment for "How to Read a Websites Javascript on a Android"