1

How can I create a layout that has an always present EditText that obeys the following three rules:

The EditText view:

  1. fills the entire parent if no image is present (at runtime, the visibility will be set to View.GONE in this case.)
  2. fills 50% of the parent height if the image is taller than it is wide.
  3. fills the remainder of the parent if the image is wider than it is tall.

The image, of course, is stretched to fit inside the bottommost 50% of the parent rectangle while preserving aspect ratio.

enter image description here

Jeff Axelrod
  • 27,676
  • 31
  • 147
  • 246

3 Answers3

0

Try this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<EditText
    android:id="@+id/editText1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:ems="10" >
</EditText>

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0"
    android:src="@drawable/ic_launcher"/>
</LinearLayout>
Wenhui
  • 648
  • 4
  • 13
  • This cannot solve the problem; at minimum it doesn't satisfy the condition that the image is stretched to fill the bottom 50% of the parent height" – Jeff Axelrod Oct 08 '12 at 16:02
  • You can always resize the image at runtime since you can get the height of the screen. Here is one sample of resize image: http://stackoverflow.com/questions/477572/android-strange-out-of-memory-issue-while-loading-an-image-to-a-bitmap-object/823966#823966 – Wenhui Oct 08 '12 at 16:21
  • Sorry, I appreciate the effort, but this really does not answer the question. See [my solution](http://stackoverflow.com/a/12785682/403455). I'd request that you please delete this answer to clean up the page. Thanks! – Jeff Axelrod Oct 08 '12 at 16:31
0

I don't believe this can be done with a single xml layout but the easy way to do it would be to simply have multiple xml layout files that you inflate depending on the image's presence and aspect ratio.

Emil Davtyan
  • 13,808
  • 5
  • 44
  • 66
0

Here's how I did it. The resulting layouts look like this:

enter image description here enter image description here

enter image description here

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <EditText
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:text="blah" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:visibility="gone"
        android:src="@drawable/photo1" />

</LinearLayout>
Jeff Axelrod
  • 27,676
  • 31
  • 147
  • 246