-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathViewExtensions.kt
74 lines (65 loc) · 1.52 KB
/
ViewExtensions.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
* Extension method to make a view gone
*/
fun View.hide() {
visibility = View.GONE
}
/**
* Extension method to make a view visible.
*/
fun View.show() {
visibility = View.VISIBLE
}
/**
* Extension method to make a view invisible
*/
fun View.invisible() {
visibility = View.INVISIBLE
}
/**
* Show depending upon a condition
*/
inline fun <T : View> T.showIf(condition: (T) -> Boolean): T {
if (condition(this)) {
show()
} else {
hide()
}
return this
}
/**
* Hide depending upon a condition
*/
inline fun <T : View> T.hideIf(condition: (T) -> Boolean): T {
if (condition(this)) {
hide()
} else {
show()
}
return this
}
/**
* Extension method to inflate a layout file
* ~BASIC USAGE~
* val view = parent.inflate(R.layout.layout_file)
*/
fun ViewGroup.inflate(@LayoutRes resource: Int, attachToRoot: Boolean = false): View =
LayoutInflater.from(context).inflate(resource, this, attachToRoot)
/**
* Extension method to show keyboard.
*/
fun Activity.showKeyboard(view: View) {
val imm: InputMethodManager =
getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT)
}
/**
* Extension method to hide keyboard.
*/
fun Activity.hideSoftKeyboard() {
if (currentFocus != null) {
val inputMethodManager = getSystemService(Context
.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(currentFocus!!.windowToken, 0)
}
}