I'm new to android development. I'm attempting to do something that I thought was simple but after much reading, trial and error and determination I've failed to find a solution I find simple and elegant and therefore I'm looking for help on how to approach the following:
I have two UI elemnents, a textview that reads "TextView" and an EditText box:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:ems="10"
android:gravity="top|left"
android:inputType="textMultiLine"
android:scrollbarAlwaysDrawVerticalTrack="false"
android:scrollbarStyle="insideOverlay" >
<requestFocus />
</EditText>
</LinearLayout>
The behavior I'm attempting to implement is: As a user adds text to the EditText box it expands and grows -- at some point it grows large enough to scroll the TextView off screen. I want the TextView content to remain visible at all times. Therefore the EditText or possibly a container it lives within needs to clip it's contents instead of scrolling the entire screen.
Along with EditText I tried every possible iteration of using following elements and could not figure out a solution.
If it's at all possible, I'd prefer to implement this solution in pure xml so that I can maintain good separation of concerns.
Try to wrap EditText
inside ScrollView
to make it scrollable. Setting ScrollView
layout_weight
attribute to 1
and fillViewport
to true
will force ScrollView to fill available space but never push TextView
out of the screen.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:fillViewport="true" >
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:gravity="top|left"
android:inputType="textMultiLine"
android:scrollbarAlwaysDrawVerticalTrack="false"
android:scrollbarStyle="insideOverlay" >
<requestFocus />
</EditText>
</ScrollView>
</LinearLayout>