Skip to content

Instantly share code, notes, and snippets.

@DevFatani
Created July 6, 2019 04:02
Show Gist options
  • Save DevFatani/f712e3645e49dc10fcc45321e4d79fad to your computer and use it in GitHub Desktop.
Save DevFatani/f712e3645e49dc10fcc45321e4d79fad to your computer and use it in GitHub Desktop.
import android.app.Activity
import android.content.Context
import android.util.TypedValue
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.util.DisplayMetrics
object Dimension {
enum class Type(val value: Int) {
FILL(value = ViewGroup.LayoutParams.MATCH_PARENT),
WRAP(value = ViewGroup.LayoutParams.WRAP_CONTENT),
NONE(0)
}
enum class Unit(val value: Int) {
DP(value = TypedValue.COMPLEX_UNIT_DIP),
SP(value = TypedValue.COMPLEX_UNIT_SP)
}
fun getSystemSize(context: Context, size: Float, unit: Unit) =
TypedValue.applyDimension(
unit.value,
size,
context.resources.displayMetrics
)
fun getViewParams(
width: Type = Type.NONE,
height: Type = Type.NONE,
numericWidth: Int = 0,
numericHeight: Int = 0
): ViewGroup.LayoutParams =
ViewGroup.LayoutParams(
if (width != Type.NONE) width.value else numericWidth,
if (height != Type.NONE) height.value else numericHeight
)
fun getRLayoutParams(
width: Type = Type.NONE,
height: Type = Type.NONE,
numericWidth: Int = 0,
numericHeight: Int = 0
): RelativeLayout.LayoutParams =
RelativeLayout.LayoutParams(
if (width != Type.NONE) width.value else numericWidth,
if (height != Type.NONE) height.value else numericHeight
)
fun getLLayoutParams(
width: Type = Type.NONE,
height: Type = Type.NONE,
numericWidth: Int = 0,
numericHeight: Int = 0,
weight: Float = 0f
): LinearLayout.LayoutParams =
LinearLayout.LayoutParams(
if (width != Type.NONE) width.value else numericWidth,
if (height != Type.NONE) height.value else numericHeight,
weight
)
fun getFrameLayoutParams(
width: Type = Type.NONE,
height: Type = Type.NONE,
numericWidth: Int = 0,
numericHeight: Int = 0,
gravity: Int = 0
): FrameLayout.LayoutParams =
FrameLayout.LayoutParams(
if (width != Type.NONE) width.value else numericWidth,
if (height != Type.NONE) height.value else numericHeight,
gravity
)
fun getScreenSize(activity: Activity): Pair<Int, Int> {
val displayMetrics = DisplayMetrics()
activity.windowManager.defaultDisplay.getMetrics(displayMetrics)
val height = displayMetrics.heightPixels
val width = displayMetrics.widthPixels
return Pair(height, width)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment