Reputation: 2587
I am following along with this tutorial and it says
The default weight for all views is 0, so if you specify any weight value greater than 0 to only one view, then that view fills whatever space remains after all views are given the space they require. So, to fill the remaining space in your layout with the EditText element, give it a weight of 1 and leave the button with no weight.
You achieve this by writing
<EditText
android:layout_weight="1"
... />
which I have done. I have also set the layout_width
to 0dp
as the tutorial says.
Now, in Android Studio, everything appears fine in the Preview pane. But when I debug my app, the EditText
control does not appear. But the Button appears further down the screen and to the very left as if the EditText
control has actually filled the entire width of the screen and pushed it down (except the EditText
does not appear so I cannot confirm this).
I have removed the weight property and set a fixed width on it, and that gets it to display, but obviously, that is not desirable.
Why won't the EditText
control appear?
Here is my code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
tools:ignore="MergeRootFrame" android:orientation="horizontal">
<EditText android:id="@+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/edit_message" />
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send" />
</LinearLayout>
As you can see below, there is no EditText control.
Upvotes: 0
Views: 1151
Reputation: 2386
The culprit is this in you editText
:
android:layout_width="0dp"
Replace with this :
android:layout_width="107dip"
Upvotes: 1
Reputation: 1420
Try this code: Your editText will cover appx. 70% and your button will cover remaining of total width...actually if you use 0dp width approach, you have to set it for all views afaik....
<EditText android:id="@+id/edit_message"
android:layout_weight="3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/edit_message" />
<Button android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/button_send" />
Upvotes: 1