Created
May 6, 2020 14:53
-
-
Save paulproteus/80326d10c93e58bd6f2f8c12ca5e0e5b to your computer and use it in GitHub Desktop.
AbsoluteLayout Kotlin demo with two buttons
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
package org.asheesh.fourbuttonsattwohundredpixels | |
import android.R.attr.delay | |
import android.os.Bundle | |
import android.os.Handler | |
import android.view.View | |
import android.widget.AbsoluteLayout | |
import android.widget.Button | |
import android.widget.ScrollView | |
import androidx.appcompat.app.AppCompatActivity | |
class MainActivity : AppCompatActivity() { | |
private lateinit var button1: Button | |
private lateinit var button2: Button | |
private lateinit var scrollThing: ScrollView | |
private lateinit var dynamicLayout: AbsoluteLayout | |
private var handler: Handler = Handler() | |
override fun onCreate(savedInstanceState: Bundle?) { | |
dynamicLayout = AbsoluteLayout(this) | |
scrollThing = ScrollView(this) | |
scrollThing.addView(dynamicLayout) | |
setContentView(scrollThing) | |
button1 = Button(this) | |
button1.text = "HELLO YOW" | |
button1.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED) | |
println("Button1 measuredWidth after measure() ${button1.measuredWidth}") | |
button2 = Button(this) | |
button2.text = "Zounds" | |
button2.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED) | |
println("Button2 measuredWidth after measure() ${button1.measuredWidth}") | |
//println("Button1 width ${button1.width}") | |
dynamicLayout.addView(button1) | |
dynamicLayout.addView(button2, AbsoluteLayout.LayoutParams(button2.measuredWidth, button2.measuredHeight, button1.measuredWidth + 700, 2000) ) | |
//println("Button1 width after addView ${button1.width}") | |
//println("Button1 measuredWidth after addView ${button1.measuredWidth}") | |
//println("Button1 width after measure() ${button1.width}") | |
//println("Button1 maxWidth after measure() ${button1.maxWidth}") | |
super.onCreate(savedInstanceState) | |
} | |
override fun onResume() { | |
super.onResume() | |
println("Button1 width in onResume ${button1.width}") | |
println("Button1 measuredWidth in onResume ${button1.measuredWidth}") | |
button1.measure(View.MeasureSpec.AT_MOST, View.MeasureSpec.AT_MOST) | |
// Move button2 higher up, 2 sec later | |
handler.postDelayed(Runnable { | |
dynamicLayout.updateViewLayout(button2, AbsoluteLayout.LayoutParams(button2.measuredWidth, button2.measuredHeight, button1.measuredWidth + 700, 500) ) | |
//do something | |
}, 2000) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment