blackst0ne
blackst0ne

Reputation: 3264

Align TextView above EditText

I want to align a TextView above an EditText. The following code is working well when the TextView is not higher than the space between the Top Margin of the window (red line) and the EditText (green line), like in the screenshot.

The problem occurs when the TextView has more lines than in the screenshot: it just "overruns" the EditText, but I want to keep the EditText in foreground.

In other words: I would like to place the TextView's bottom margin onto the green line and let it grow towards the red line, in order to maintain the visibility of the EditText.

enter image description here

// LINEAR LAYOUT
LinearLayout layout = new LinearLayout(getApplicationContext());
layout.setOrientation(LinearLayout.VERTICAL);
setContentView(layout);

// TEXTVIEW
// how to set bottom margins programmatically?
layout.addView(tv);

// EDITTEXT
// place the EditText to the bottom of the layout, working well
et.setGravity(Gravity.BOTTOM);
et.setInputType(InputType.TYPE_CLASS_TEXT);

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
params.gravity = Gravity.BOTTOM;
et.setLayoutParams(params);
layout.addView(et);

Upvotes: 2

Views: 1022

Answers (5)

pseudocoder
pseudocoder

Reputation: 36

This should do it for you:

// LINEAR LAYOUT
LinearLayout layout = new LinearLayout(getApplicationContext());
layout.setOrientation(LinearLayout.VERTICAL);
setContentView(layout);

// TEXTVIEW
LinearLayout.LayoutParams paramstv = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT,1f);
tv.setLayoutParams(paramstv);
layout.addView(tv);

// EDITTEXT
et.setGravity(Gravity.BOTTOM);
et.setInputType(InputType.TYPE_CLASS_TEXT);

LinearLayout.LayoutParams etparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT,8f);
etparams.gravity = Gravity.BOTTOM;
et.setLayoutParams(etparams);
layout.addView(et);

I guess the key is really to wrap both the EditText and TextView into LayoutParams. Probably you have to adjust the weights.

If this one doesn't work, try to create your layout with an XML file (the handling there is easier).

Upvotes: 2

user4514461
user4514461

Reputation:

This below xml code can solve your problem...

<RelativeLayout 
android:layout_height="match_parent"
android:layout_width="match_parent">

<SrollView 
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_above="@+id/edittext_id"
android:layout_margin="7dp">

<TextView
        android:id="@+id/text_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</SrollView>
<EditText 
android:id="@+id/edittext_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_alignParentBottom="true"/>

</RelativeLayout>

Upvotes: 0

vinitius
vinitius

Reputation: 3274

As you're using LinearLayout , set weight for your views

LinearLayout.LayoutParams paramTextView = new LinearLayout.LayoutParams(
                         LayoutParams.MATCH_PARENT,
                         LayoutParams.MATCH_PARENT, 1.0f);
tv.setlayoutParams(paramTextView);

LinearLayout.LayoutParams paramEditText = new LinearLayout.LayoutParams(
                         LayoutParams.MATCH_PARENT,
                         LayoutParams.MATCH_PARENT, 3.0f);
et.setlayoutParams(paramEditText);

This shall give you the result you expect

Upvotes: 0

Saurabh Rajpal
Saurabh Rajpal

Reputation: 1557

Try using the relative layout instead and set the edittext at the parent's bottom.. In case you don't want to switch to relative layout then try declaring edit text before the text view..

Upvotes: 1

Javasamurai
Javasamurai

Reputation: 686

You can set the property of EditText type to textMultiline.

Upvotes: 0

Related Questions