Browser Android App
13, July 2020
I have completed the basic coding of my browser app for android which is based on Kotlin language. I am writing the detail code here
#Activity_Main
<<<<<<
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:id="@+id/toolbar"
android:elevation="4dp"
android:background="@color/colorPrimary"
app:contentInsetStart="0dp"
app:contentInsetLeft="0dp"
android:contentInsetLeft="0dp"
android:contentInsetStart="0dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/search_layout"
android:orientation="horizontal"
android:layout_margin="10dp"
android:background="@android:color/white"
android:gravity="center"
>
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:gravity="center"
android:background="@android:color/white"
android:padding="10dp"
android:src="@drawable/google"
android:id="@+id/toolbar_search_imageview_favicon"
/>
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:maxLines="1"
android:inputType="textNoSuggestions"
android:gravity="top|left"
android:id="@+id/edit_text"
android:background="@android:color/white"
android:hint="Search or Enter URL"
android:textSize="13sp"
android:padding="10dp"
android:textColor="@android:color/black"
android:textColorHint="@android:color/black"
/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:background="@android:color/white"
android:textSize="15sp"
android:onClick="go"
android:textColor="@android:color/black"
android:text="go"
/>
</LinearLayout>
</android.support.v7.widget.Toolbar>
<ProgressBar
android:layout_width="match_parent"
android:layout_height="3dp"
style="?android:attr/progressBarStyleHorizontal"
android:id="@+id/progressbar"
android:background="@color/colorPrimary"
android:visibility="gone"
android:progressDrawable="@drawable/custom_progress"
/>
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="7"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_gravity="center"
android:layout_weight="0.43"
android:background="@color/bottom_bar_background"
android:orientation="horizontal"
>
<ImageButton
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="?android:selectableItemBackground"
android:onClick="goBack"
android:src="@drawable/ic_baseline_arrow_back_24" />
<ImageButton
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="?android:selectableItemBackground"
android:onClick="goForward"
android:src="@drawable/ic_baseline_arrow_forward_24" />
<ImageButton
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="?android:selectableItemBackground"
android:onClick="goHome"
android:src="@drawable/ic_baseline_home_24" />
<ImageButton
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="?android:selectableItemBackground"
android:onClick="refresh"
android:src="@drawable/ic_baseline_refresh_24" />
<ImageButton
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="?android:selectableItemBackground"
android:onClick="share"
android:src="@drawable/ic_baseline_share_24" />
</LinearLayout>
</LinearLayout>
>>>>>>
#MainActivity.kt
<<<<<<
package com.example.browser
import android.app.Activity
import android.content.pm.PackageManager
import android.os.Build
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.app.AppCompatViewInflater
import android.util.Patterns
import android.view.View
import android.view.inputmethod.InputMethodManager
import kotlinx.android.synthetic.main.activity_main.*
import android.Manifest
import android.annotation.SuppressLint
import android.app.AlertDialog
import android.app.DownloadManager
import android.content.Context
import android.content.DialogInterface
import android.content.Intent
import android.graphics.Bitmap
import android.net.Uri
import android.os.Environment
import android.support.v4.app.ActivityCompat
import android.view.KeyEvent
import android.webkit.*
@Suppress("DEPRECATION")
class MainActivity : AppCompatActivity()
{
var share_url:String? = null
@SuppressLint("SetJavaScriptEnabled")
override fun onCreate(savedInstanceState: Bundle?)
{
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
webview.loadUrl("https://www.google.com/")
setSupportActionBar(toolbar)
webview.settings.javaScriptEnabled = true
webview.webChromeClient = object : WebChromeClient()
{
override fun onProgressChanged(view: WebView?, newProgress: Int)
{
progressbar.progress = newProgress
super.onProgressChanged(view, newProgress)
if (newProgress == 100)
{
progressbar.visibility = View.GONE
}
}
}
webview.webViewClient = object: WebViewClient()
{
override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?)
{
progressbar.visibility = View.VISIBLE
share_url = url
if ("https://www.google.com/" != url)
{
edit_text.setText(url)
}
else
{
edit_text.text.clear()
}
super.onPageStarted(view, url, favicon)
}
}
webview.setDownloadListener { url, userAgent, contentDisposition, mimetype, contentLength ->
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.M)
{
if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)
{
downloadDialog(url, userAgent, contentDisposition, mimetype)
}
else
{
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), 1)
}
}
else
{
//code for devices below API 29
downloadDialog(url, userAgent, contentDisposition, mimetype)
}
}
}
private fun downloadDialog(url: String?, userAgent: String?, contentDisposition: String?, mimetype: String?)
{
//file name from url
val filename = URLUtil.guessFileName(url,contentDisposition,mimetype)
//alert Dialog
val builder = AlertDialog.Builder(this@MainActivity)
//title for dialog
builder.setTitle("Download")
builder.setMessage("Do you want to save $filename")
builder.setPositiveButton("Yes") { dialogInterface, i ->
val request = DownloadManager.Request(Uri.parse(url))
//cookies
val cookie = CookieManager.getInstance().getCookie(url)
request.addRequestHeader("Cookie",cookie)
request.addRequestHeader("User-Agent",userAgent)
request.allowScanningByMediaScanner()
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
val downloadManager = getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,filename)
downloadManager.enqueue(request)
}
builder.setNegativeButton("Cancel") {dialogInterface, i ->
dialogInterface.cancel()
}
val dialog:AlertDialog = builder.create()
dialog.show()
}
override fun onPause()
{
super.onPause()
webview.onPause()
webview.pauseTimers()
}
override fun onResume()
{
super.onResume()
webview.onResume()
webview.resumeTimers()
}
fun goBack(view: View)
{
if (webview.canGoBack())
webview.goBack()
}
fun goForward(view: View)
{
if (webview.canGoForward())
webview.goForward()
}
fun goHome(view: View)
{
webview.loadUrl("https://www.google.com/")
}
fun refresh(view: View)
{
webview.reload()
}
fun go(view: View)
{
var text = edit_text.text.toString()
searchorLoad(text)
}
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean
{
if (keyCode == KeyEvent.KEYCODE_BACK && webview.canGoBack())
{
webview.goBack()
return true
}
return super.onKeyDown(keyCode, event)
}
fun share(view: View)
{
val shareIntent = Intent()
shareIntent.action = Intent.ACTION_SEND
shareIntent.type = "text/plain"
shareIntent.putExtra(Intent.EXTRA_TEXT,share_url)
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "URL")
startActivity(Intent.createChooser(shareIntent,"Share with your friends"))
}
private fun searchorLoad(text: String)
{
if (Patterns.WEB_URL.matcher(text.toLowerCase()).matches())
{
if (text.contains("http://")||text.contains("https://"))
{
webview.loadUrl(text)
}
else
{
webview.loadUrl("https://$text")
}
}
else
{
webview.loadUrl("https://www.google.com/search?q=$text")
}
hideKeyboard()
}
private fun hideKeyboard()
{
val inputMethodManager = getSystemService (Activity.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow (currentFocus?.windowToken,InputMethodManager.SHOW_FORCED)
}
}
>>>>>>
#AndroidManifest.xml
<<<<<<
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.browser">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<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"
android:windowSoftInputMode="stateAlwaysHidden|adjustNothing"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
>>>>>>
#Colors.xml
<<<<<<
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#707cff</color>
<color name="colorPrimaryDark">#3250cb</color>
<color name="colorAccent">#ffa7b7</color>
<color name="bottom_bar_background">#e0e0e0</color>
</resources>
>>>>>>
#Styles.xml
<<<<<<
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
>>>>>>
#Custom_Progress
<<<<<<
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/progress">
<clip>
<shape>
<corners android:radius="1dp"/>
<solid android:color="#ffffff"/>
</shape>
</clip>
</item>
</layer-list>
>>>>>>
Comments
Post a Comment