Skip to content

Commit 139a465

Browse files
committed
Renamed pdfView_actionBarTint to pdfView_toolbarColor for better readability
1 parent 812414e commit 139a465

File tree

6 files changed

+115
-58
lines changed

6 files changed

+115
-58
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ Custom:
252252
|pdfView_backIcon|drawable|Navigation icon|
253253
|pdfView_downloadIcon|drawable|Download icon|
254254
|pdfView_downloadIconTint|color|Download icon tint|
255-
|pdfView_actionBarTint|color|Actionbar background color|
255+
|pdfView_toolbarColor|color|Actionbar background color|
256256
|pdfView_titleTextStyle|style|Actionbar title text appearance|
257257
|pdfView_progressBar|style|Progress bar style|
258258

app/src/main/res/values/styles.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<resources>
22
<style name="Theme.PdfView.SelectedTheme" parent="@style/Theme.PdfView.Light">
33
<item name="pdfView_showToolbar">true</item>
4-
<item name="pdfView_actionBarTint">@color/design_default_color_error</item>
4+
<item name="pdfView_toolbarColor">@color/design_default_color_primary</item>
55
<item name="pdfView_downloadIconTint">#ffffff</item>
66
<item name="pdfView_enableLoadingForPages">true</item>
77
<item name="pdfView_titleTextStyle">@style/actionBarTitleAppearance</item>

pdfViewer/src/main/java/com/rajat/pdfviewer/PdfViewerActivity.kt

+104-45
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
11
package com.rajat.pdfviewer
22

33
import android.Manifest.permission
4-
import android.app.Activity
54
import android.app.AlertDialog
65
import android.content.Context
76
import android.content.DialogInterface
87
import android.content.Intent
98
import android.content.pm.PackageManager
9+
import android.content.res.ColorStateList
10+
import android.content.res.Configuration
11+
import android.graphics.Color
1012
import android.net.Uri
1113
import android.os.Build
1214
import android.os.Bundle
1315
import android.os.Environment
16+
import android.text.Spannable
17+
import android.text.SpannableString
1418
import android.text.TextUtils
19+
import android.text.style.TextAppearanceSpan
1520
import android.util.Log
1621
import android.view.Menu
1722
import android.view.MenuInflater
1823
import android.view.MenuItem
19-
import android.view.View
2024
import android.view.View.GONE
2125
import android.view.View.VISIBLE
2226
import android.widget.Toast
@@ -33,13 +37,17 @@ import androidx.core.view.WindowInsetsCompat
3337
import androidx.core.view.WindowInsetsControllerCompat
3438
import androidx.core.view.updatePadding
3539
import androidx.lifecycle.lifecycleScope
40+
import com.google.android.material.color.MaterialColors
3641
import com.rajat.pdfviewer.databinding.ActivityPdfViewerBinding
3742
import com.rajat.pdfviewer.util.FileUtils.createPdfDocumentUri
3843
import com.rajat.pdfviewer.util.FileUtils.fileFromAsset
3944
import com.rajat.pdfviewer.util.FileUtils.uriToFile
4045
import com.rajat.pdfviewer.util.NetworkUtil.checkInternetConnection
4146
import com.rajat.pdfviewer.util.saveTo
4247
import java.io.File
48+
import java.io.FileNotFoundException
49+
import java.net.SocketTimeoutException
50+
import java.net.UnknownHostException
4351

4452
/**
4553
* Created by Rajat on 11,July,2020
@@ -116,28 +124,62 @@ class PdfViewerActivity : AppCompatActivity() {
116124
val typedArray = theme.obtainStyledAttributes(R.styleable.PdfRendererView_toolbar)
117125
try {
118126
// Retrieve attributes safely
119-
val showToolbar = typedArray.getBoolean(R.styleable.PdfRendererView_toolbar_pdfView_showToolbar, true)
120-
val backIcon = typedArray.getDrawable(R.styleable.PdfRendererView_toolbar_pdfView_backIcon)
121-
val titleTextStyle = typedArray.getResourceId(R.styleable.PdfRendererView_toolbar_pdfView_titleTextStyle, -1)
127+
val showToolbar =
128+
typedArray.getBoolean(R.styleable.PdfRendererView_toolbar_pdfView_showToolbar, true)
129+
val backIcon =
130+
typedArray.getDrawable(R.styleable.PdfRendererView_toolbar_pdfView_backIcon)
131+
val titleTextStyle = typedArray.getResourceId(
132+
R.styleable.PdfRendererView_toolbar_pdfView_titleTextStyle,
133+
-1
134+
)
135+
136+
// Retrieve action bar tint with a fallback to Material3 colorPrimary
137+
val toolbarColor = typedArray.getColor(
138+
R.styleable.PdfRendererView_toolbar_pdfView_toolbarColor,
139+
MaterialColors.getColor(
140+
this,
141+
com.google.android.material.R.attr.colorPrimary,
142+
Color.BLUE
143+
)
144+
)
145+
146+
// Adjust toolbar color in dark mode
147+
val isDarkMode = (resources.configuration.uiMode and
148+
Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES
149+
val adjustedToolbarColor = if (isDarkMode) {
150+
MaterialColors.getColor(
151+
this,
152+
com.google.android.material.R.attr.colorSurface,
153+
Color.DKGRAY
154+
)
155+
} else {
156+
toolbarColor
157+
}
158+
159+
binding.myToolbar.setBackgroundColor(adjustedToolbarColor)
122160

123-
// Retrieve action bar tint safely
124-
val actionBarTint = if (typedArray.hasValue(R.styleable.PdfRendererView_toolbar_pdfView_actionBarTint)) {
125-
typedArray.getColor(R.styleable.PdfRendererView_toolbar_pdfView_actionBarTint, 0)
126-
} else null
127161

128162
// Apply toolbar visibility
129-
binding.myToolbar.visibility = if (showToolbar) View.VISIBLE else View.GONE
163+
binding.myToolbar.visibility = if (showToolbar) VISIBLE else GONE
130164

131165
// Set back icon if available
132166
backIcon?.let { binding.myToolbar.navigationIcon = it }
133167

134-
// Apply title text appearance if defined
168+
// Apply title text appearance safely
135169
if (titleTextStyle != -1) {
136-
binding.myToolbar.setTitleTextAppearance(this, titleTextStyle)
170+
val spannable = SpannableString(binding.myToolbar.title)
171+
val textAppearance = TextAppearanceSpan(this, titleTextStyle)
172+
spannable.setSpan(
173+
textAppearance,
174+
0,
175+
spannable.length,
176+
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
177+
)
178+
binding.myToolbar.title = spannable
137179
}
138180

139-
// Apply action bar tint only if defined
140-
actionBarTint?.let { binding.myToolbar.setBackgroundColor(it) }
181+
// Apply action bar tint using backgroundTintList for better theming
182+
binding.myToolbar.backgroundTintList = ColorStateList.valueOf(toolbarColor)
141183

142184
} finally {
143185
typedArray.recycle()
@@ -212,12 +254,13 @@ class PdfViewerActivity : AppCompatActivity() {
212254
R.styleable.PdfRendererView_pdfView_backgroundColor,
213255
ContextCompat.getColor(applicationContext, android.R.color.white)
214256
)
215-
binding.parentLayout.setBackgroundColor(backgroundColor)
216257

217258
// Set progress bar style
218-
val progressBarStyleResId = typedArray.getResourceId(R.styleable.PdfRendererView_pdfView_progressBar, -1)
259+
val progressBarStyleResId =
260+
typedArray.getResourceId(R.styleable.PdfRendererView_pdfView_progressBar, -1)
219261
if (progressBarStyleResId != -1) {
220-
binding.progressBar.indeterminateDrawable = ContextCompat.getDrawable(this, progressBarStyleResId)
262+
binding.progressBar.indeterminateDrawable =
263+
ContextCompat.getDrawable(this, progressBarStyleResId)
221264
}
222265
} finally {
223266
typedArray.recycle()
@@ -236,28 +279,39 @@ class PdfViewerActivity : AppCompatActivity() {
236279

237280
// Load string resources from XML attributes
238281
val typedArray = obtainStyledAttributes(R.styleable.PdfRendererView_Strings)
239-
error_pdf_corrupted = typedArray.getString(R.styleable.PdfRendererView_Strings_error_pdf_corrupted)
240-
?: getString(R.string.error_pdf_corrupted)
241-
error_no_internet_connection = typedArray.getString(R.styleable.PdfRendererView_Strings_error_no_internet_connection)
242-
?: getString(R.string.error_no_internet_connection)
243-
file_saved_successfully = typedArray.getString(R.styleable.PdfRendererView_Strings_file_saved_successfully)
244-
?: getString(R.string.file_saved_successfully)
245-
file_saved_to_downloads = typedArray.getString(R.styleable.PdfRendererView_Strings_file_saved_to_downloads)
246-
?: getString(R.string.file_saved_to_downloads)
247-
file_not_downloaded_yet = typedArray.getString(R.styleable.PdfRendererView_Strings_file_not_downloaded_yet)
248-
?: getString(R.string.file_not_downloaded_yet)
249-
permission_required = typedArray.getString(R.styleable.PdfRendererView_Strings_permission_required)
250-
?: getString(R.string.permission_required)
251-
permission_required_title = typedArray.getString(R.styleable.PdfRendererView_Strings_permission_required_title)
252-
?: getString(R.string.permission_required_title)
253-
pdf_viewer_error = typedArray.getString(R.styleable.PdfRendererView_Strings_pdf_viewer_error)
254-
?: getString(R.string.pdf_viewer_error)
255-
pdf_viewer_retry = typedArray.getString(R.styleable.PdfRendererView_Strings_pdf_viewer_retry)
256-
?: getString(R.string.pdf_viewer_retry)
257-
pdf_viewer_cancel = typedArray.getString(R.styleable.PdfRendererView_Strings_pdf_viewer_cancel)
258-
?: getString(R.string.pdf_viewer_cancel)
259-
pdf_viewer_grant = typedArray.getString(R.styleable.PdfRendererView_Strings_pdf_viewer_grant)
260-
?: getString(R.string.pdf_viewer_grant)
282+
error_pdf_corrupted =
283+
typedArray.getString(R.styleable.PdfRendererView_Strings_error_pdf_corrupted)
284+
?: getString(R.string.error_pdf_corrupted)
285+
error_no_internet_connection =
286+
typedArray.getString(R.styleable.PdfRendererView_Strings_error_no_internet_connection)
287+
?: getString(R.string.error_no_internet_connection)
288+
file_saved_successfully =
289+
typedArray.getString(R.styleable.PdfRendererView_Strings_file_saved_successfully)
290+
?: getString(R.string.file_saved_successfully)
291+
file_saved_to_downloads =
292+
typedArray.getString(R.styleable.PdfRendererView_Strings_file_saved_to_downloads)
293+
?: getString(R.string.file_saved_to_downloads)
294+
file_not_downloaded_yet =
295+
typedArray.getString(R.styleable.PdfRendererView_Strings_file_not_downloaded_yet)
296+
?: getString(R.string.file_not_downloaded_yet)
297+
permission_required =
298+
typedArray.getString(R.styleable.PdfRendererView_Strings_permission_required)
299+
?: getString(R.string.permission_required)
300+
permission_required_title =
301+
typedArray.getString(R.styleable.PdfRendererView_Strings_permission_required_title)
302+
?: getString(R.string.permission_required_title)
303+
pdf_viewer_error =
304+
typedArray.getString(R.styleable.PdfRendererView_Strings_pdf_viewer_error)
305+
?: getString(R.string.pdf_viewer_error)
306+
pdf_viewer_retry =
307+
typedArray.getString(R.styleable.PdfRendererView_Strings_pdf_viewer_retry)
308+
?: getString(R.string.pdf_viewer_retry)
309+
pdf_viewer_cancel =
310+
typedArray.getString(R.styleable.PdfRendererView_Strings_pdf_viewer_cancel)
311+
?: getString(R.string.pdf_viewer_cancel)
312+
pdf_viewer_grant =
313+
typedArray.getString(R.styleable.PdfRendererView_Strings_pdf_viewer_grant)
314+
?: getString(R.string.pdf_viewer_grant)
261315

262316
typedArray.recycle()
263317
}
@@ -291,22 +345,27 @@ class PdfViewerActivity : AppCompatActivity() {
291345
}
292346
}
293347
}
348+
294349
override fun onError(error: Throwable) {
295350
runOnUiThread {
296351
false.showProgressBar()
297352

298353
val errorMessage = when {
299-
error is java.net.UnknownHostException -> error_no_internet_connection
300-
error is java.net.SocketTimeoutException -> "Network timeout! Please check your connection."
301-
error is java.io.FileNotFoundException -> "File not found on the server."
354+
error is UnknownHostException -> error_no_internet_connection
355+
error is SocketTimeoutException -> "Network timeout! Please check your connection."
356+
error is FileNotFoundException -> "File not found on the server."
302357
error.message?.contains("Invalid content type received") == true ->
303358
"The server returned a non-PDF file. Please check the URL."
359+
304360
error.message?.contains("Downloaded file is not a valid PDF") == true ->
305361
"The file appears to be corrupted or is not a valid PDF."
362+
306363
error.message?.contains("Incomplete download") == true ->
307364
"The download was incomplete. Please check your internet connection and try again."
365+
308366
error.message?.contains("Failed to download after") == true ->
309367
"Failed to download the PDF after multiple attempts. Please check your internet connection."
368+
310369
else -> "An unexpected error occurred: ${error.localizedMessage}"
311370
}
312371

@@ -341,8 +400,8 @@ class PdfViewerActivity : AppCompatActivity() {
341400

342401

343402
private fun isRetryable(error: Throwable): Boolean {
344-
return error is java.net.UnknownHostException ||
345-
error is java.net.SocketTimeoutException ||
403+
return error is UnknownHostException ||
404+
error is SocketTimeoutException ||
346405
error.message?.contains("Failed to download") == true ||
347406
error.message?.contains("Incomplete download") == true
348407
}
@@ -543,7 +602,7 @@ class PdfViewerActivity : AppCompatActivity() {
543602

544603
private val createFileLauncher =
545604
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
546-
if (result.resultCode == Activity.RESULT_OK) {
605+
if (result.resultCode == RESULT_OK) {
547606
result.data?.data?.let { uri ->
548607
contentResolver.openOutputStream(uri)?.use { outputStream ->
549608
downloadedFilePath?.let { filePath ->

pdfViewer/src/main/res/layout/activity_pdf_viewer.xml

+5-7
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,26 @@
44
xmlns:app="http://schemas.android.com/apk/res-auto"
55
xmlns:tools="http://schemas.android.com/tools"
66
android:layout_width="match_parent"
7-
android:id="@+id/parentLayout"
87
android:layout_height="match_parent"
9-
android:fitsSystemWindows="true"
108
tools:context=".PdfViewerActivity">
119

1210
<!-- Material 3 Toolbar -->
1311
<com.google.android.material.appbar.MaterialToolbar
1412
android:id="@+id/my_toolbar"
1513
android:layout_width="match_parent"
1614
android:layout_height="?attr/actionBarSize"
17-
android:background="?attr/colorPrimary"
1815
android:theme="@style/ThemeOverlay.PdfView.Toolbar"
16+
android:background="?attr/colorPrimary"
1917
app:navigationIcon="@drawable/pdf_viewer_ic_arrow_back"
20-
app:titleTextColor="?attr/colorOnPrimary"
2118
app:title="@string/pdfView_appName"
19+
app:titleTextColor="?attr/colorOnPrimary"
2220
app:layout_constraintTop_toTopOf="parent"/>
2321

2422
<!-- Main Layout for PDF Viewer -->
2523
<FrameLayout
2624
android:id="@+id/mainLayout"
2725
android:layout_width="match_parent"
28-
android:layout_height="match_parent"
26+
android:layout_height="0dp"
2927
app:layout_constraintTop_toBottomOf="@id/my_toolbar"
3028
app:layout_constraintBottom_toBottomOf="parent">
3129

@@ -35,7 +33,7 @@
3533
android:layout_width="match_parent"
3634
android:layout_height="match_parent"
3735
app:pdfView_divider="@drawable/pdf_viewer_divider"
38-
app:pdfView_showDivider="false" />
36+
app:pdfView_showDivider="false"/>
3937

4038
<!-- Centered ProgressBar -->
4139
<ProgressBar
@@ -49,4 +47,4 @@
4947

5048
</FrameLayout>
5149

52-
</androidx.constraintlayout.widget.ConstraintLayout>
50+
</androidx.constraintlayout.widget.ConstraintLayout>

pdfViewer/src/main/res/values/attrs.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
<attr name="pdfView_backIcon" format="reference" />
3939
<attr name="pdfView_downloadIcon" format="reference" />
4040
<attr name="pdfView_downloadIconTint" format="reference|color" />
41-
<attr name="pdfView_actionBarTint" format="reference|color" />
41+
<attr name="pdfView_toolbarColor" format="reference|color" />
4242
<attr name="pdfView_titleTextStyle" format="reference" />
4343
<attr name="pdfView_showToolbar" format="boolean" />
4444
</declare-styleable>

pdfViewer/src/main/res/values/themes.xml

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<item name="colorSurface">?attr/colorSurface</item>
99
<item name="colorOnSurface">?attr/colorOnSurface</item>
1010
<item name="pdfView_backgroundColor">?attr/colorSurface</item>
11-
<item name="pdfView_actionBarTint">?attr/colorPrimary</item>
11+
<item name="pdfView_toolbarColor">?attr/colorPrimary</item>
1212
<item name="android:statusBarColor">?attr/colorPrimary</item>
1313
<item name="android:windowLightStatusBar">?attr/isLightTheme</item>
1414
<item name="android:navigationBarColor">?attr/colorSurface</item>
@@ -19,7 +19,7 @@
1919
<item name="pdfView_backIcon">@drawable/pdf_viewer_ic_arrow_back</item>
2020
<item name="pdfView_downloadIcon">@android:drawable/stat_sys_download</item>
2121
<item name="pdfView_downloadIconTint">?attr/colorOnSurfaceVariant</item>
22-
<item name="pdfView_actionBarTint">?attr/colorPrimary</item>
22+
<item name="pdfView_toolbarColor">?attr/colorPrimary</item>
2323
<item name="pdfView_titleTextStyle">@style/pdfView_titleTextAppearance</item>
2424
<item name="pdfView_backgroundColor">?attr/colorSurface</item>
2525
</style>
@@ -29,7 +29,7 @@
2929
<item name="pdfView_backIcon">@drawable/pdf_viewer_ic_arrow_back_alt</item>
3030
<item name="pdfView_downloadIcon">@android:drawable/stat_sys_download</item>
3131
<item name="pdfView_downloadIconTint">?attr/colorOnSurfaceVariant</item>
32-
<item name="pdfView_actionBarTint">?attr/colorPrimary</item>
32+
<item name="pdfView_toolbarColor">?attr/colorPrimary</item>
3333
<item name="pdfView_titleTextStyle">@style/pdfView_titleTextAppearanceDark</item>
3434
<item name="pdfView_backgroundColor">?attr/colorSurface</item>
3535
</style>

0 commit comments

Comments
 (0)