Created
May 6, 2020 15:01
-
-
Save paulproteus/50f02509188b57b9b530fdfeb0e05022 to your computer and use it in GitHub Desktop.
RelativeLayout abused for absolute layout :)
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.Button | |
import android.widget.RelativeLayout | |
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: RelativeLayout | |
private var handler: Handler = Handler() | |
override fun onCreate(savedInstanceState: Bundle?) { | |
dynamicLayout = RelativeLayout(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) | |
val params = RelativeLayout.LayoutParams(button2.measuredWidth, button2.measuredHeight) | |
params.topMargin = 2000 | |
params.leftMargin = button1.measuredWidth + 70 | |
dynamicLayout.addView(button2, params ) | |
//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 { | |
val params = RelativeLayout.LayoutParams(button2.measuredWidth, button2.measuredHeight) | |
params.topMargin = 500 | |
params.leftMargin = button1.measuredWidth + 70 | |
dynamicLayout.updateViewLayout(button2, params) | |
}, 2000) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment