Created
November 17, 2017 08:24
-
-
Save hilfritz/b2a8f4d66c8e63093ba17f0ea7a86b69 to your computer and use it in GitHub Desktop.
Android Kotlin ViewUtil class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ViewUtil { | |
fun disableButtonsTemporarily(viewList:ArrayList<View>){ | |
try{ | |
//DISABLE THE VIEW | |
updateViewsClickableAttribute(viewList, false) | |
//ENABLE THE VIEWS | |
val handler = Handler() | |
handler.postDelayed( { | |
// Do something after 5s = 5000ms | |
updateViewsClickableAttribute(viewList, true) | |
}, 500) | |
}catch (e: Exception){ | |
e.printStackTrace() | |
} | |
} | |
fun updateViewsClickableAttribute(viewList:ArrayList<View>, clickable:Boolean){ | |
var size = 0; | |
if (viewList!=null) | |
size = viewList.size | |
if (size==0){ | |
return | |
} | |
for (i in 0..size-1) { | |
val view = viewList.get(i) | |
if (view!=null){ | |
var canUpdateAttribute = true | |
if (canUpdateAttribute) | |
view.isClickable = clickable | |
} | |
} | |
} | |
fun removeView(view: View) { | |
val parent = getParent(view) | |
if (parent != null) { | |
parent!!.removeView(view) | |
} | |
} | |
fun replaceView(currentView: View, newView: View) { | |
val parent = getParent(currentView) ?: return | |
val index = parent!!.indexOfChild(currentView) | |
removeView(currentView) | |
removeView(newView) | |
parent!!.addView(newView, index) | |
} | |
private fun getParent(view: View): ViewGroup { | |
return view.parent as ViewGroup | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment