derekantrican
derekantrican

Reputation: 2285

Arrangement of LinearLayout in android

I'm just starting out with Android development and am trying to learn the intricacies of everything. I'm trying to create a view that is something like $________ (a dollar sign followed by a text box), but I can't quite get the LinearLayout to work properly (I'm imagining it works just like XAML's StackPanel?).

Here's what I've got:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

  <LinearLayout android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="75dp">
    <TextView android:text="$"
              android:textSize="65dp"
              android:layout_width="match_parent"
              android:layout_height="match_parent"/>
    <EditText android:layout_width="150dp"
      android:layout_height="match_parent"/>
  </LinearLayout>
</LinearLayout>

But only the dollar sign shows up. If I switch it to be this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

  <LinearLayout android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="75dp">
    <EditText android:layout_width="150dp"
      android:layout_height="match_parent"/>
    <TextView android:text="$"
              android:textSize="65dp"
              android:layout_width="match_parent"
              android:layout_height="match_parent"/>
  </LinearLayout>
</LinearLayout>

then it shows up like I would expect: _________$, but the opposite doesn't seem to work well. Any help is greatly appreciated!

Upvotes: 0

Views: 70

Answers (4)

santosh kumar
santosh kumar

Reputation: 2972

you can use

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView android:text="$"
        android:textSize="65sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <EditText
        android:textSize="65sp"
        android:hint="Edit text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

And

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <EditText
        android:textSize="65sp"
        android:layout_width="wrap_content"
        android:hint="Edit text"
        android:layout_height="wrap_content"/>

        <TextView android:text="$"
            android:textSize="65sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

</LinearLayout>

Upvotes: 1

Lutaaya Huzaifah Idris
Lutaaya Huzaifah Idris

Reputation: 4010

Hey try to wrap_content the width of a TextView

    <TextView android:text="$"
                  android:textSize="65dp"
                  android:layout_width="wrap_content"
                  android:layout_height="match_parent"/>
   <EditText android:layout_width="150dp"
      android:layout_height="match_parent"/>

Upvotes: 0

OneCricketeer
OneCricketeer

Reputation: 191963

You need to not match the parent width on the dollar sign.

How about a relative layout?

Or you could set 0dp width on the dollar sign and the input field, then set the layout weight attribute. Then the LinearLayout has a weight sum attribute.

Upvotes: 0

Vladimir Seriakov
Vladimir Seriakov

Reputation: 26

You should use wrap_content for layout_width of the TextView.

Upvotes: 0

Related Questions