Considering this LinearLayout containing..
-
One nested Linear Layout with two child views
-
one imageView
-
one Textview
-
-
One textView
- One Button
Why can I successfully show text content on the outer TextView using findViewById and setText while the inner Textview (inside nested linear view) shows blank?
Here is the code for onCreate:
LayoutInflater inflater = LayoutInflater.from(Item.this);
LinearLayout itemLayout = (LinearLayout)inflater.inflate(R.layout.activity_item, null);
Button proformaButton = (Button)itemLayout.getChildAt(2);
roformaButton.setOnClickListener(this);
setContentView(itemLayout);
TextView titleTitla = (TextView)findViewById(R.id.item_title);
TextView itemBody = (TextView)findViewById(R.id.item_body);
titleTitla.setText("Tomatoes");
itemBody.setText("Potatoes");
While debugging I can see that the view is found by Id and I can also see that setText does it work (mText field of TextView), but the activity does not show anything for item_title.
I am asking how to fix this but if it’s not a trivial thing a hint of explanation would be very appreciated.
If it helps, here’s the xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dip"
android:orientation="horizontal"
android:layout_weight="1">
<ImageView
android:id="@+id/item_picture"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:contentDescription="@string/image_item">
</ImageView>
<TextView
android:id="@+id/item_title"
android:layout_width="0dip"
android:layout_height="fill_parent">
</TextView>
</LinearLayout>
<TextView
android:id="@+id/item_body"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="2">
</TextView>
<Button
android:id="@+id/btnOrderItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/order_button"
/>
</LinearLayout>
It’s the
layout_height="0dp". The reason it works on the other LinearLayout is because you have a weight attribute. Change the 0dp towrap_content.