Skip to content

Commit

Permalink
Add open in browser button to webviews
Browse files Browse the repository at this point in the history
  • Loading branch information
Domi04151309 committed Nov 27, 2021
1 parent d9cf17c commit f95d6a3
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ import android.content.Context
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import android.util.Base64
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import android.widget.ProgressBar
import android.widget.RelativeLayout
Expand All @@ -24,6 +20,7 @@ import androidx.core.app.ActivityCompat
import android.content.pm.PackageManager
import android.webkit.*
import android.content.Intent
import android.view.*
import androidx.activity.result.contract.ActivityResultContracts

class WebActivity : AppCompatActivity() {
Expand Down Expand Up @@ -178,6 +175,20 @@ class WebActivity : AppCompatActivity() {
"})()")
}

override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.activity_web_actions, menu)
return true
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
return if (item.itemId == R.id.action_open) {
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(webView.url)))
true
} else {
super.onOptionsItemSelected(item)
}
}

override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack() && !errorOccurred) {
webView.goBack()
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/ic_nav_open_in_new.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="24dp"
android:viewportHeight="24"
android:viewportWidth="24"
android:width="24dp" >

<path
android:fillColor="?android:attr/textColorSecondary"
android:pathData="M19,19H5V5h7V3H5c-1.11,0 -2,0.9 -2,2v14c0,1.1 0.89,2 2,2h14c1.1,0 2,-0.9 2,-2v-7h-2v7zM14,3v2h3.59l-9.83,9.83 1.41,1.41L19,6.41V10h2V3h-7z"/>
</vector>
2 changes: 1 addition & 1 deletion app/src/main/res/drawable/ic_nav_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:fillColor="?android:attr/textColorSecondary"
android:pathData="M19.43,12.98c0.04,-0.32 0.07,-0.64 0.07,-0.98 0,-0.34 -0.03,-0.66 -0.07,-0.98l2.11,-1.65c0.19,-0.15 0.24,-0.42 0.12,-0.64l-2,-3.46c-0.09,-0.16 -0.26,-0.25 -0.44,-0.25 -0.06,0 -0.12,0.01 -0.17,0.03l-2.49,1c-0.52,-0.4 -1.08,-0.73 -1.69,-0.98l-0.38,-2.65C14.46,2.18 14.25,2 14,2h-4c-0.25,0 -0.46,0.18 -0.49,0.42l-0.38,2.65c-0.61,0.25 -1.17,0.59 -1.69,0.98l-2.49,-1c-0.06,-0.02 -0.12,-0.03 -0.18,-0.03 -0.17,0 -0.34,0.09 -0.43,0.25l-2,3.46c-0.13,0.22 -0.07,0.49 0.12,0.64l2.11,1.65c-0.04,0.32 -0.07,0.65 -0.07,0.98 0,0.33 0.03,0.66 0.07,0.98l-2.11,1.65c-0.19,0.15 -0.24,0.42 -0.12,0.64l2,3.46c0.09,0.16 0.26,0.25 0.44,0.25 0.06,0 0.12,-0.01 0.17,-0.03l2.49,-1c0.52,0.4 1.08,0.73 1.69,0.98l0.38,2.65c0.03,0.24 0.24,0.42 0.49,0.42h4c0.25,0 0.46,-0.18 0.49,-0.42l0.38,-2.65c0.61,-0.25 1.17,-0.59 1.69,-0.98l2.49,1c0.06,0.02 0.12,0.03 0.18,0.03 0.17,0 0.34,-0.09 0.43,-0.25l2,-3.46c0.12,-0.22 0.07,-0.49 -0.12,-0.64l-2.11,-1.65zM17.45,11.27c0.04,0.31 0.05,0.52 0.05,0.73 0,0.21 -0.02,0.43 -0.05,0.73l-0.14,1.13 0.89,0.7 1.08,0.84 -0.7,1.21 -1.27,-0.51 -1.04,-0.42 -0.9,0.68c-0.43,0.32 -0.84,0.56 -1.25,0.73l-1.06,0.43 -0.16,1.13 -0.2,1.35h-1.4l-0.19,-1.35 -0.16,-1.13 -1.06,-0.43c-0.43,-0.18 -0.83,-0.41 -1.23,-0.71l-0.91,-0.7 -1.06,0.43 -1.27,0.51 -0.7,-1.21 1.08,-0.84 0.89,-0.7 -0.14,-1.13c-0.03,-0.31 -0.05,-0.54 -0.05,-0.74s0.02,-0.43 0.05,-0.73l0.14,-1.13 -0.89,-0.7 -1.08,-0.84 0.7,-1.21 1.27,0.51 1.04,0.42 0.9,-0.68c0.43,-0.32 0.84,-0.56 1.25,-0.73l1.06,-0.43 0.16,-1.13 0.2,-1.35h1.39l0.19,1.35 0.16,1.13 1.06,0.43c0.43,0.18 0.83,0.41 1.23,0.71l0.91,0.7 1.06,-0.43 1.27,-0.51 0.7,1.21 -1.07,0.85 -0.89,0.7 0.14,1.13zM12,8c-2.21,0 -4,1.79 -4,4s1.79,4 4,4 4,-1.79 4,-4 -1.79,-4 -4,-4zM12,14c-1.1,0 -2,-0.9 -2,-2s0.9,-2 2,-2 2,0.9 2,2 -0.9,2 -2,2z"
tools:ignore="VectorPath" />
</vector>
3 changes: 1 addition & 2 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
android:alpha="0.6"
android:padding="13dp"
android:layout_centerVertical="true"
android:contentDescription="@string/nav_settings"
app:tint="?android:attr/textColorPrimary" />
android:contentDescription="@string/nav_settings" />

<TextView
android:layout_width="match_parent"
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/res/menu/activity_web_actions.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
android:id="@+id/action_open"
android:title="@string/webView_open"
android:icon="@drawable/ic_nav_open_in_new"
app:showAsAction="always" />
</menu>
1 change: 1 addition & 0 deletions app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<string name="tasmota_execute_once_summary">Führt einen Befehl ein Mal aus ohne ihn der Liste hinzuzufügen</string>

<string name="webView">Internetansicht</string>
<string name="webView_open">Im Browser öffnen</string>
<string name="webView_error">Laden der Webseite fehlgeschlagen</string>
<string name="webView_error_summary">Bitte überprüfen Sie Ihre Verbindung oder versuchen Sie es später erneut</string>
<string name="webView_authentication">Authentifizierung</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
<string name="tasmota_execute_once_summary">Execute command once without adding it to the list</string>

<string name="webView">Web View</string>
<string name="webView_open">Open in browser</string>
<string name="webView_error">Loading the website failed</string>
<string name="webView_error_summary">Please check your network connection or try again later</string>
<string name="webView_authentication">Authentication</string>
Expand Down

0 comments on commit f95d6a3

Please sign in to comment.