How to Build a Simple Incognito Browser For Secret Search on Android - Hive Programmers

avatar

Greetings to my favorite Science community online, StemSocial.

It's @skyehi and I'm always super excited to be back on Hive and StemSocial to continue my series on Android App development tutorials for beginners.

For today's tutorial we're going to be building another browser but this time, it's going to be an incognito one.

Now I'm not going to judge exactly what people do with Incognito browsers but it's sometimes very useful to use.

Polish_20240102_172826703.jpgOriginal Image Source by Mohamed_hassan from Pixabay

YpihifdXP4WNbGMdjw7e3DuhJWBvCw4SfuLZsrnJYHEpsqZFkiGGNCQTayu6EytKdg7zA3LL2PbZxrJGpWk6ZoZvBcJrADNFmaFEHagho8WsASbAA8jrpnELSvRtvjVuMiU1C5ADFX1vJgcpDvNtue9Pq83tjBKX62dqT5UoxtDk.png

Not everyone wants their activities online tracked but the topic of whether network service providers and browser Apps can still track your activities to some limit is still up for debate.

However Incognito Apps are pretty useful for making online searches that the user would want to make sure it's either not stored on the phone or cache memory or tracked by anyone.

So guys let's go ahead and build a very basic or simple Incognito Browser.

We're not going to be working on the whole project which would include stuff like Browser navigations and other advanced features for now.

The main purpose of this particular episode is to show my readers and followers how exactly the incognito feature is implemented.

In future or more advanced blogs, we'll build entire Apps and this tutorial will certainly come in handy when we want to integrate the incognito feature into our browser Apps.

Let's get started with today's work shall we

YpihifdXP4WNbGMdjw7e3DuhJWBvCw4SfuLZsrnJYHEpsqZFkiGGNCQTayu6EytKdg7zA3LL2PbZxrJGpWk6ZoZvBcJrADNFmaFEHagho8WsASbAA8jrpnELSvRtvjVuMiU1C5ADFX1vJgcpDvNtue9Pq83tjBKX62dqT5UoxtDk.png

Creating a New Android Studio Project

Alright guys so before we begin I'll start with the Prerequisites. If you want to be able to successfully go through with this tutorial, you would need two main softwares;

  • Android Studio IDE which is the platform on which we'll be building all our Android Apps

  • Java Development Kit, JDK installed on your PC so the PC can successfully execute Java codes and instructions.

After getting all these prerequisites sorted out, we'll start creating a new Android Project by opening Android Studio and clicking on "Start a New Android Project"

As we usually do in this series just to keep it basic, we'll select an Empty Activity as the template of our App project.

So guys after clicking on the next button, you can set the unique App name and package name of your App.

Also, we're still using Java programming language as our language for the backend instructions or logic of our App. So guys please ensure that Java is selected and not Kotlin.

When you're through with all the necessary configurations for your Project, click on the "Finish" button and wait for Android Studio to prepare your new App Project Space.

2dk2RRM2dZ8gKjXsrozapsD83FxL3Xbyyi5LFttAhrXxr16mCe4arfLHNDdHCBmaJroMz2VbLrf6Rxy9uPQm7Ts7EnXL4nPiXSE5vJWSfR53VcqDUrQD87CZSpt2RKZcmrYrze8KanjkfyS8XmMCcz4p33NZmfHE4S9oRo3wU2.png

Designing The Incognito Browser User Interface

Alright guys it's time to work on the Frontend or design layout of our App.

This is going to be a basic App that displays the Google search engine website but your browsing activities are all in incognito mode so it cannot be tracked, has no history or cache and it's totally top secret.

So to display the website we would need to use the Webview element.

Of course the design layout code will be written inside our activity_main.xml file.

Here's how your simple design layout code should look like

<?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:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
    />

    (html comment removed:  You can Include other UI elements here, like a search bar, switch for incognito, and a button to clear data as well )

</RelativeLayout>

Like I wrote in the comments above, you can totally include more features.

Including all those features would have made our work very lengthy and I'm trying to avoid that for the basics. We'll be doing that in intermediary and advanced levels of this series.

YpihifdXP4WNbGMdjw7e3DuhJWBvCw4SfuLZsrnJYHEpsqZFkiGGNCQTayu6EytKdg7zA3LL2PbZxrJGpWk6ZoZvBcJrADNFmaFEHagho8WsASbAA8jrpnELSvRtvjVuMiU1C5ADFX1vJgcpDvNtue9Pq83tjBKX62dqT5UoxtDk.png

WebView Configuration

Alright guys it's now time for our backend code. I've divided the steps into four main parts but they'll all be written inside the same file, the MainActivity.java file.

The first thing we want to do is to configure the WebView element by initializing it so that it loads when the Incognito Browser App launches.

We would also include functions like JavaScript enabling so our browser App can display images and other graphics as well.

Here's how that code should look like

// MainActivity.java
// Inside onCreate method

WebView webView = findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);

Implement Search Functionality

The next thing we want to do is to implement a search functionality.

This is the part where we would build an entire search feature for users to search for any website but for the basics we'll create an App that only opens Google's website.

Here's how that code should look like and I would like to make you aware that this would still be done inside our onCreate method in MainActivity.java file.

// MainActivity.java

// Inside onCreate method
String searchQuery = "https://www.google.com/search?q=" + URLEncoder.encode(query, "UTF-8");
webView.loadUrl(searchQuery);

Implement Incognito Mode

Here comes the real difference maker. It's now time to implement the incognito functionality.

I've created it in such a way that you can add a button as a switch from incognito to not incognito.

We'll create a method called incognitoSwitch which would handle the incognito activity.

Here's how your code should look like

// MainActivity.java

// Inside onCreate method
Switch incognitoSwitch = findViewById(R.id.incognitoSwitch);
incognitoSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            // Enable incognito mode
            webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
            // Add other settings for incognito mode
        } else {
            // Disable incognito mode
            webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
            // Add other settings for regular mode
        }
    }
});

Clear Browsing Data

Now guys incase you want to include a button that also clears data instead of doing it automatically when App closes, here's how you do it.

We need to use the webView.clearHistory() method or function to make this happen.

Here's how your code should look like

// MainActivity.java

// Inside onCreate method
Button clearDataButton = findViewById(R.id.clearDataButton);
clearDataButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        webView.clearCache(true);
        webView.clearHistory();
        // Add other data clearing operations
    }
});

YpihifdXP4WNbGMdjw7e3DuhJWBvCw4SfuLZsrnJYHEpsqZFkiGGNCQTayu6EytKdg7zA3LL2PbZxrJGpWk6ZoZvBcJrADNFmaFEHagho8WsASbAA8jrpnELSvRtvjVuMiU1C5ADFX1vJgcpDvNtue9Pq83tjBKX62dqT5UoxtDk.png

Requesting Internet Permission

So guys before we complete our App, we're going to need to include a very important permission request.

I taught on permissions requests yesterday and that blog was quite successful. I'm very grateful for all those that supported.

Now since our Incognito Browser App is a browser, it would most certainly require access to the Internet to work.

So guys, for access to internet service and to also let our users know that the App would use the internet service when they check the details of the App on Google Play Store, we need to request internet access permission.

This will of course be done inside our AndroidManifest.xml file

Here's how the permission request code should look like

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.incognitobrowser">

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

    <application>
        (html comment removed:  Add other application configurations here )
    </application>
</manifest>

2dk2RRM2dZ8gKjXsrozapsD83FxL3Xbyyi5LFttAhrXxr16mCe4arfLHNDdHCBmaJroMz2VbLrf6Rxy9uPQm7Ts7EnXL4nPiXSE5vJWSfR53VcqDUrQD87CZSpt2RKZcmrYrze8KanjkfyS8XmMCcz4p33NZmfHE4S9oRo3wU2.png

Congratulations guys, we have completed our work for the day and have successfully built a basic Android Incognito Browser.

It's time to run the App guys. As always you can either run it on your physical Android device or an emulator, however just make sure that your device has a working Internet or WiFi to test the App.

When your App launches, you'll see a WebView taking up the entire screen, displaying the Google search engine website.

Thank you so much for taking the time to read today's blog. I hope you enjoyed this episode guys. Would love to get your feedback on today's tutorial.

Definitely comment down below if you're having troubles building the App.

Have a Great Day And Catch You Next Time On StemSocial, Goodbye 👨‍💻


You Can Follow Me @skyehi For More Like This And Others



0
0
0.000
1 comments
avatar

Thanks for your contribution to the STEMsocial community. Feel free to join us on discord to get to know the rest of us!

Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).

Thanks for including @stemsocial as a beneficiary, which gives you stronger support. 
 

0
0
0.000