Skip to content

Instantly share code, notes, and snippets.

@diegohkd
Last active April 28, 2020 10:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diegohkd/06d35a67a4d16f18c60fb0785381704d to your computer and use it in GitHub Desktop.
Save diegohkd/06d35a67a4d16f18c60fb0785381704d to your computer and use it in GitHub Desktop.
This is a simple code to help understand how Android draws the UI
<?xml version="1.0" encoding="utf-8"?>
<example.com.exampledrawview.CustomLinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

    <example.com.exampledrawview.CustomTextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"/>

</example.com.exampledrawview.CustomLinearLayout>
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        Log.d("Drawing View", "onCreate() executed")
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
    override fun onStart() {
        Log.d("Drawing View", "onStart() executed")
        super.onStart()
    }
    override fun onResume() {
        Log.d("Drawing View", "onResume() executed")
        super.onResume()
    }
    override fun onWindowFocusChanged(hasFocus: Boolean) {
        Log.d("Drawing View", "onWindowFocusChanged() executed")
        super.onWindowFocusChanged(hasFocus)
    }
}

class CustomLinearLayout(context: Context?, attrs: AttributeSet?) : LinearLayout(context, attrs) {
    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        Log.d("Drawing View", "LinearLayout: entering onMeasure(). Measured width: $measuredWidth")
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        Log.d("Drawing View", "LinearLayout: leaving onMeasure(). Measured width: $measuredWidth")
    }
    override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
        Log.d("Drawing View", "LinearLayout: entering onLayout(). Actual width: $width")
        super.onLayout(changed, l, t, r, b)
        Log.d("Drawing View", "LinearLayout: leaving onLayout(). Actual width: $width")
    }
}

class CustomTextView(context: Context?, attrs: AttributeSet?) : TextView(context, attrs) {
    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        Log.d("Drawing View", "TextView: entering onMeasure(). Measured width: $measuredWidth")
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        Log.d("Drawing View", "TextView: leaving onMeasure(). Measured width: $measuredWidth")
    }
    override fun layout(l: Int, t: Int, r: Int, b: Int) {
        Log.d("Drawing View", "TextView: entering layout(). Actual width: $width")
        super.layout(l, t, r, b)
        Log.d("Drawing View", "TextView: leaving layout(). Actual width: $width")
    }
    override fun draw(canvas: Canvas?) {
        Log.d("Drawing View", "TextView: draw() executed")
        super.draw(canvas)
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment