meeDamian
meeDamian

Reputation: 1164

Android set margin/padding for contents (text) instead of a container (EditText)

What I want to do is add some top margin/padding before THE TEXT, and not for the entire container:

Code is as simple as:

<EditText android:id="@+id/mightyText"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:gravity="left|top"
     android:inputType="textAutoComplete|textMultiLine"
     android:paddingLeft="20dp"
     android:paddingRight="20dp"
     android:paddingBottom="80dp"
     android:background="#fff" />

Produces outputs:

Desired behaviour

Once it's on top it works great. I have equal paddings for sides and top...


Naughty padding ;(

...but when I scroll, the padding stays glued to the container instead of going up with the text


And that's how I'd like it to be (when not scrolled to top):

Proper behavior

How do I change it, so it scrolls with the text instead of always being visible?

Upvotes: 17

Views: 34967

Answers (3)

ezpn
ezpn

Reputation: 1748

Put it in ScrollView, which will automatically take care of the scroll. EditText won't be responsible for that, so his padding will work as you wish. Try the code below:

<ScrollView
           android:layout_width="match_parent"
           android:layout_height="wrap_content" >

           <EditText android:id="@+id/mightyText"
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:gravity="left|top"
               android:inputType="textAutoComplete|textMultiLine"
               android:paddingLeft="20dp"
               android:paddingRight="20dp"
               android:paddingBottom="80dp"
               android:background="#fff" />

</ScrollView>

Upvotes: 27

jmodrako
jmodrako

Reputation: 334

On ViewGroup there is attribute called clipToPadding. Docs says: Defines whether the ViewGroup will clip its drawing surface so as to exclude the padding area. This property is set to true by default. Try to find out something similar in EditText.

Upvotes: 0

Lisa Wray
Lisa Wray

Reputation: 2272

Does it have to be an EditText? If the user will not be editing it, then use a normal TextView with a top padding of 10dp and wrap it in a ScrollView.

As far as I know, there's no way to set an internal padding on the top of an EditText.

Upvotes: 0

Related Questions