Skip to content

Instantly share code, notes, and snippets.

@prateem
Last active May 18, 2019 02:34
Show Gist options
  • Save prateem/ef455aed329fcb404a50d0503593f4f1 to your computer and use it in GitHub Desktop.
Save prateem/ef455aed329fcb404a50d0503593f4f1 to your computer and use it in GitHub Desktop.
Clipping problem with height-weighted buttons in a vertical LinearLayout
class MyActivity {
// ...
override fun onCreate(savedInstanceState: Bundle?) {
setContentView(R.layout.my_activity)
// ...
val container: LinearLayout = findViewById(R.id.container)
for (i in 0 until getNumBtnsToAdd()) {
val margin10 = dpToPx(10f).toInt()
val btn = Button(this, null, 0, R.style.button_style).apply {
layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0).apply {
weight = 1f
gravity = Gravity.CENTER
setMargins(margin10, margin10, margin10, margin10)
}
text = if (i == 0) "Button${i+1} with short text"
else "Button${i+1} with text that will span multiple lines showing my clipping problem"
setOnClickListener { doSomething() }
}
container.addView(btn)
}
// ...
}
// ...
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<CoordinatorLayout android:layout_width="match_parent" android:layout_height="match_parent" ...>
<AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" ...></AppBarLayout>
<ScrollView app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_width="match_parent" android:layout_height="match_parent" ...>
<!-- Other content -->
<LinearLayout android:id="@+id/container" android:orientation="vertical"
android:layout_width="match_parent" android:layout_height="wrap_content" />
<!-- Other content -->
</ScrollView>
</CoordinatorLayout>
</RelativeLayout>
<style name="button_style" parent="Base.Widget.AppCompat.Button">
<item name="android:textSize">16sp</item>
<item name="android:paddingStart">12dp</item>
<item name="android:paddingEnd">12dp</item>
<item name="android:paddingTop">12dp</item>
<item name="android:paddingBottom">12dp</item>
<item name="android:textAllCaps">false</item>
</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment