Created
May 12, 2020 04:18
-
-
Save paulproteus/439643f38a22bcc6a7867280dd0d8a97 to your computer and use it in GitHub Desktop.
A weirdo spinner I suppose
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.database.DataSetObserver | |
import android.util.Log | |
import android.view.View | |
import android.view.ViewGroup | |
import android.widget.SpinnerAdapter | |
import android.widget.TextView | |
class CustomSpinnerAdapter: SpinnerAdapter { | |
override fun isEmpty(): Boolean { | |
return false | |
} | |
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View { | |
val view = TextView(MainActivity.singletonThis) | |
view.text = "getView position=$position" | |
Log.d("CustomSpinnerAdapter", "getView call position=$position") | |
return view | |
} | |
override fun registerDataSetObserver(observer: DataSetObserver?) { | |
// do nothing for now | |
} | |
override fun getItemViewType(position: Int): Int { | |
return 0 // Using 0 to indicate TextView | |
} | |
override fun getItem(position: Int): Any? { | |
if (position == 0) { | |
return "Yup, first item" | |
} | |
return null | |
} | |
override fun getViewTypeCount(): Int { | |
return 1 | |
} | |
override fun getItemId(position: Int): Long { | |
return position * 1L | |
} | |
override fun hasStableIds(): Boolean { | |
return false | |
} | |
override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup?): View { | |
val view = TextView(MainActivity.singletonThis) | |
view.text = "getDropDownView position=$position" | |
return view | |
} | |
override fun unregisterDataSetObserver(observer: DataSetObserver?) { | |
return | |
} | |
override fun getCount(): Int { | |
return 1 | |
} | |
} |
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.* | |
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 lateinit var button_2_props: RelativeLayout.LayoutParams | |
private var handler: Handler = Handler() | |
companion object { | |
public lateinit var singletonThis: MainActivity | |
} | |
override fun onCreate(savedInstanceState: Bundle?) { | |
singletonThis = this | |
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}") | |
val button1params = RelativeLayout.LayoutParams(button1.measuredWidth, button1.measuredHeight) | |
button1params.leftMargin = 500 | |
dynamicLayout.addView(button1, button1params) | |
val params = RelativeLayout.LayoutParams(button2.measuredWidth, button2.measuredHeight) | |
button_2_props = params | |
params.topMargin = 2000 | |
params.leftMargin = button1.measuredWidth + 70 | |
dynamicLayout.addView(button2, params ) | |
// Create a Spinner | |
val spinner = Spinner(this, Spinner.MODE_DROPDOWN) | |
spinner.adapter = CustomSpinnerAdapter() | |
dynamicLayout.addView(spinner) | |
//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 { | |
button_2_props.topMargin = 500 | |
button_2_props.leftMargin = button1.measuredWidth + 70 | |
val params = RelativeLayout.LayoutParams(button2.measuredWidth, button2.measuredHeight) | |
}, 2000) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Before click:
After click: