<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginHorizontal="@dimen/para"
android:layout_weight="0.5"
android:background="@color/gray_medium_dark"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:id="@+id/messages_parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout style="@style/hor_layout">
<TextView
style="@style/message_text"
android:background="@color/gray_blue_medium"
android:text="@string/dummy_short" />
<Space style="@style/message_space" />
</LinearLayout>
</LinearLayout>
</ScrollView>
Above is my xml and I want to add a LinearLayout
packed with a TextView
and Space
to the LinearLayout
with the id messages_parent. I want it to happen when a button is pressed.
private fun sendMessage(message: String) {
var newMessage: LinearLayout = LinearLayout(context)
newMessage.style(R.style.hor_layout)
// <style name="hor_layout">
// <item name="android:layout_width">match_parent</item>
// <item name="android:layout_height">wrap_content</item>
// <item name="android:orientation">horizontal</item>
// </style>
var newMessageTxt: TextView = TextView(context)
newMessageTxt.style(R.style.message_text)
newMessageTxt.text = message
// <style name="message_text" parent="myText">
// <item name="android:layout_width">0dp</item>
// <item name="android:layout_height">wrap_content</item>
// <item name="android:layout_weight">0.85</item>
// <item name="android:layout_margin">@dimen/sec</item>
// <item name="android:padding">@dimen/sec</item>
// <item name="android:background">@color/gray_blue_medium</item>
// </style>
// message_text's parent
// <style name="myText">
// <item name="android:textColor">@color/gray_light</item>
// <item name="fontFamily">monospace</item>
// <item name="android:lineSpacingExtra">4sp</item>
// <item name="android:letterSpacing">0.1</item>
// <item name="android:textSize">@dimen/para</item>
// I've checked all my dimens are correct.
// </style>
// setting height by the lines below won't help either
// newMessageTxt.layoutParams.height = 500
// newMessageTxt.height = 500
println("orientation: ${newMessage.orientation.toString()}\n" +
"text size: ${newMessageTxt.textSize}\n" +
"text: ${newMessageTxt.text}\n" +
"height: ${newMessageTxt.height}\n" +
"width: ${newMessageTxt.width}")
// log-
// 2021-03-27 11:32:48.428 31468-31468/com.example.firechat3 I/System.out: orientation: 0
// 2021-03-27 11:32:48.428 31468-31468/com.example.firechat3 I/System.out: text size: 32.0
// 2021-03-27 11:32:48.428 31468-31468/com.example.firechat3 I/System.out: text: Why am I invisible??
// 2021-03-27 11:32:48.428 31468-31468/com.example.firechat3 I/System.out: height: 0
// 2021-03-27 11:32:48.428 31468-31468/com.example.firechat3 I/System.out: width: 0
var newMessageSpace: Space = Space(context)
newMessageSpace.style(R.style.message_space)
// <style name="message_space">
// <item name="android:layout_width">0dp</item>
// <item name="android:layout_height">match_parent</item>
// <item name="android:layout_weight">0.15</item>
// </style>
// adding the TextView and Space to the new LinearLayout
newMessage.addView(newMessageTxt)
newMessage.addView(newMessageSpace)
// adding the new LinearLayout to the parent
requireView().findViewById<LinearLayout>(R.id.messages_parent).addView(newMessage)
}
But the height and width of the newly added views remain 0 no matter how I try to do it and the orientation too whether I set it horizontal or vertical. I am using a package Paris by airbnb to set the style as told in this story on medium and I am using Paris because the solutions from this thread didn't helped me. The first message is added before runtime in the xml by hand and I want the other messages to look like that.