[Android] Add extension to retrieve drawable by identifier name
Robert Stone
git at videolan.org
Thu Feb 3 09:01:34 UTC 2022
vlc-android | branch: master | Robert Stone <rhstone at gmail.com> | Fri Jan 21 22:02:56 2022 -0800| [91dbca4c830967d482eab926d95c9f5183cc8dbe] | committer: Nicolas Pomepuy
Add extension to retrieve drawable by identifier name
> https://code.videolan.org/videolan/vlc-android/commit/91dbca4c830967d482eab926d95c9f5183cc8dbe
---
.../tools/src/main/java/org/videolan/tools/KotlinExtensions.kt | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/application/tools/src/main/java/org/videolan/tools/KotlinExtensions.kt b/application/tools/src/main/java/org/videolan/tools/KotlinExtensions.kt
index b330912bc..783fc8607 100644
--- a/application/tools/src/main/java/org/videolan/tools/KotlinExtensions.kt
+++ b/application/tools/src/main/java/org/videolan/tools/KotlinExtensions.kt
@@ -17,6 +17,7 @@ import android.util.Patterns
import android.util.TypedValue
import android.view.View
import androidx.annotation.AttrRes
+import androidx.annotation.DrawableRes
import androidx.core.content.getSystemService
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
@@ -166,4 +167,10 @@ fun Intent.isCallable(context: Context): Boolean {
val list: List<ResolveInfo> = context.packageManager.queryIntentActivities(this,
PackageManager.MATCH_DEFAULT_ONLY)
return list.isNotEmpty()
+}
+
+fun Resources.getDrawableOrDefault(name: String, defPackage: String, @DrawableRes defaultDrawable: Int): Int {
+ return getIdentifier(name, "drawable", defPackage).let {
+ if (it == 0) defaultDrawable else it
+ }
}
\ No newline at end of file
More information about the Android
mailing list