You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+62-3Lines changed: 62 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,4 @@
1
-
AndroidUtils is a collection of util classes that I use in basically every project - I might as well give them their own module. This is more for personal use (I don't expect most people to use this as a dependency in their own projects, other than stealing a snippet or two), but feel free to add your own contributions as you wish.
1
+
AndroidUtils is a collection of util classes that I use in almost every project - I might as well give them their own module. This is more for personal use (I don't expect most people to use this as a dependency in their own projects, other than stealing a snippet or two), but feel free to add your own contributions as you wish.
To add the dependency, copy this line into your app module's build.gradle file.
21
21
22
22
```gradle
23
-
implementation 'me.jfenn:AndroidUtils:0.0.2'
23
+
implementation 'me.jfenn:AndroidUtils:0.0.5'
24
24
```
25
25
26
26
### Methods
27
27
28
-
Most of these methods are fairly self-explanatory in terms of what they actually do. As I'm lazy, you can find auto-generated docs for all of them [here](https://jfenn.me/projects/androidutils/docs), or just browse the source code to see what they do.
28
+
#### ViewUtils
29
+
30
+
Various extension functions/delegates to safely reference view ids in activities, fragments, and other Android components.
31
+
32
+
```kotlin
33
+
val button:MaterialButton?= findView(R.id.button)
34
+
val recyclerView:RecyclerView? by bind(R.id.recycler)
35
+
```
36
+
37
+
Also: `View.setBackgroundTint(color)` will wrap & tint a view's background drawable.
38
+
39
+
#### Prefs
40
+
41
+
A bunch of generic-typed preference utility functions.
42
+
43
+
```kotlin
44
+
// editing a SharedPreferences object (automatically runs .apply())
45
+
sharedPrefs.edit {
46
+
putInt("themeKey", THEME_DARK)
47
+
}
48
+
49
+
// setter/getter operator functions
50
+
val theme:Int= sharedPrefs["themeKey"]
51
+
sharedPrefs["themeKey"] =THEME_LIGHT
52
+
```
53
+
54
+
Property delegates can be used to greatly simplify SharedPreferences operations.
55
+
56
+
```kotlin
57
+
// binding a single property
58
+
val theme by sharedPrefs.get<Int>("themeKey", defaultValue =THEME_LIGHT)
59
+
60
+
// uses the delegated property name ("themeKey") if a key is not provided
61
+
val themeKey by sharedPrefs.get<Int>(defaultValue =THEME_LIGHT)
62
+
```
63
+
64
+
##### TypedPreference
65
+
66
+
TypedPreference objects can be used to create a set of typed "pointers" to reference throughout your project, instead of relying on a hardcoded preference key/string.
67
+
68
+
```kotlin
69
+
// constructs a TypedPreference<Int> with the key "PREF_THEME"
70
+
valPREF_THEME by pref<Int>(defaultValue =THEME_LIGHT)
71
+
72
+
// in another file...
73
+
var theme by sharedPrefs.get(PREF_THEME)
74
+
75
+
// alternatively, in an activity/fragment/etc (if using PreferenceManager.getDefaultSharedPreferences)
76
+
var theme by prefs(PREF_THEME)
77
+
```
78
+
79
+
#### DimenUtils
80
+
81
+
Contains two `Context` extension functions: `getStatusBarHeight()` and `getNavigationBarHeight()`, that both work accordingly.
82
+
83
+
Also includes some unit conversion functions that use `Resources.getSystem()` to find the display density:
0 commit comments