Created
May 6, 2020 16:10
-
-
Save paulproteus/418405d5c404ea38b82de266c6e08f53 to your computer and use it in GitHub Desktop.
Constrain button width to a max width
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.os.Bundle | |
import android.os.Handler | |
import android.util.DisplayMetrics | |
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) | |
val displayMetrics = DisplayMetrics() | |
windowManager.defaultDisplay.getMetrics(displayMetrics) | |
val height: Int = displayMetrics.heightPixels | |
val width: Int = displayMetrics.widthPixels | |
println("width is ${width}") | |
println("height is ${height}") | |
button1 = Button(this) | |
button1.text = "Kale chips food truck pop-up distillery prism. Craft beer art party copper mug shaman whatever quinoa try-hard synth meditation vexillologist mixtape readymade. Poutine microdosing keffiyeh, offal 8-bit chia twee. Salvia flexitarian coloring book sriracha meggings microdosing brunch vaporware craft beer. Fam green juice everyday carry, pitchfork forage retro health goth. Banjo messenger bag mlkshk VHS mumblecore austin single-origin coffee la croix. Whatever umami lumbersexual poutine organic marfa mustache raclette yuccie try-hard kickstarter tumblr cliche brooklyn food truck." | |
val atMostFourHundred = View.MeasureSpec.makeMeasureSpec(400, View.MeasureSpec.AT_MOST); | |
button1.measure(atMostFourHundred, 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() ${button2.measuredWidth}") | |
//println("Button1 width ${button1.width}") | |
dynamicLayout.addView(button1, RelativeLayout.LayoutParams(button1.measuredWidth, button1.measuredHeight)) | |
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