I'm trying to add a horizontal line between two EditText widgets, but strangely the line has no padding at the bottom, so it appears "glued" to the widget that is below it.
This is my code (in the layout.xml, inside a LinearLayout with vertical orientation):
<EditText android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:capitalize="sentences"
android:singleLine="true"
android:maxLength="30" />
<View android:background="#FF00FF00"
android:layout_width="fill_parent"
android:layout_height="1dip" />
<EditText android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:scrollbars="vertical"
android:capitalize="none"
android:autoLink="all"
android:maxLines="8" />
And this is how it looks like:
I'd like to add some padding below the line. I tried with android:paddingBottom in the View widget, and with android:paddingTop in the below EditText widget, but the result is the same (it's ignored).
Instead of using padding use margin.
Use the following property in xml:
android:layout_marginTop="5dip"
android:layout_marginBottom="5dip"
Hope this will solve your problem...:)
Use a margin instead. Padding is the area around the content inside a View. If you give padding to an EditText for instance, you increase the size of the box (the area around the text), but don't give any space around it.
Related
I want to make drawable and text center in my EditText. The text is centered but the drawable is not. I want something like this:
http://i.imgur.com/cacxR2C.jpg?1
Please help. This is what I have done so far
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:gravity="center_horizontal"
android:drawableLeft="#drawable/lol2"
/>
When the user types in something in the EditText, the drawable should move itself to the left if the text is lenghty.
You can do something like this
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:background="#android:drawable/editbox_background"
>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Username"
android:background="#null"
android:gravity="center"
android:drawableLeft="#drawable/ic_launcher"
/>
</RelativeLayout>
This worked for me:
<EditText
android:id="#+id/searchET"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="75dp"
android:paddingEnd="75dp"
android:inputType="text"
android:singleLine="true"
android:gravity="center"
android:hint=" Username"
android:drawableStart="#drawable/your_icon"
android:background="#android:drawable/editbox_background"/>
The kicker here is that you wrap_content for the width, align the text however, center the view however you'd like, then add padding to each end of the edit text.
This shoves the drawable to the start of your text but still lets you expand your edit text background.
instead of
android:drawableLeft="#drawable/lol2"
try using
android:drawableStart="#drawable/lol2"
Basically gravity attribute is used to apply changes of aligning the contents of lay out so you can put the gravity to center to align contents in center and center_vertical to to align contents on left side and gravity to center_horizontal to put contents in horizontal center. i.e
android:gravity="center"
I have the following :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:gravity="left|center_vertical" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text"
android:gravity="left|center_vertical" />
</LinearLayout
I want to get my button's text to align left. Right now its aligned in the center. The textview's text is aligned left without any problems. Is there something else I need to add? I don't really have to close and reopen my IDE because I use maven to build. Any suggestions?
ANSWER
Figured it out : set android:paddingLeft="0dp" . Done. No need for gravity there.
Instead of wrap_content give some value. Actually its working but you can't see because your width is set as wrap_content
<Button
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Button"
android:gravity="left|center_vertical" />
When you set an object's gravity property, you're telling that object where you want its contents to be aligned. In your code, you're trying to align the text in a box that only as big as the text itself, which does nothing.
There are 2 ways to solve this problem.
A) You can set the button's width to be not wrap the content.
<Button
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Button"
android:gravity="left|center_vertical" />
B) You can set the width of the text-view to not be wrap content.
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Text"
android:gravity="left|center_vertical" />
Otherwise, what you're doing right now should have some text in a button where there is no space between the edge of the text and the outside of the button. If there is space there, make sure you're not setting padding or margins in the button or text.
I am wondering how to have a TextView display its content on several lines without hardcoding the width in the XML.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="false"
android:text="Long multiline text"/>
<TextView
android:textColor="#color/text_color"
android:layout_width="130dp"
android:layout_height="wrap_content"
/>
</LinearLayout>
Any thought welcome.
EDIT: my problem is that when the text exceeds the width set (because it reaches the end of the screen) a portion of the text is just not displayed. I would expect the text to be split on two lines
Though I cannot reproduce the not wrapping problem, you can fix the positioning problem by using a weight on the first TextView. Using the following XML gives the expected output in the graphical layout view in Eclipse:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:singleLine="false"
android:text="Long multiline text"/>
<TextView
android:textColor="#color/text_color"
android:layout_width="130dp"
android:layout_height="wrap_content"
/>
</LinearLayout>
Also add
android:minLines="2"
android:scrollHorizontally="false"
You could try
android:inputType="textMultiLine"
in your TextView XML. This worked for me.
I think I had very similar problem. I had a TextView with a text, where I was not sure how much lines will it take. It was encapsulated by a LinearLayout having android:layout_width="match_parent" to ensure my text will fill out all the space horizontally. However, the problem was that my text did not fit into 1 line and when it did break into a new line, the next view component below it did not move downwards to give enough space for the second line to be viewable fully.
I could achieve the solution by changing the LinearLayout that was containing my TextView into a RelativeLayout. By this way, the element below the text (actually below the Layout itself) was moved automatically to give enough space for the multi-line text.
It seems there is a padding between the bottom input section and bottom margin of an edittext by default. I have a linearlayout(horizon) including one edittext and one button. I just can't make them in one single line perfectly, since the padding between the bottom input section and bottom margin make the edittext lower than the button through I set the same height and alignment for them.
I also tried Relative layout but cannot solve the problem either, here is my layout
<RelativeLayout
android:id="#+id/SearchLinearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_marginTop="10dp" >
<EditText
android:id="#+id/FilterEditText"
android:layout_width="fill_parent"
android:layout_height="38dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="5dp"
android:layout_toLeftOf="#+id/SearchButton"
android:gravity="center_vertical"
android:hint="#string/search_or_add_a_place"
android:imeOptions="flagNoExtractUi"
android:inputType="text|textNoSuggestions"
android:textSize="15dp" />
<Button
android:id="#+id/SearchButton"
android:layout_width="38dp"
android:layout_height="38dp"
android:layout_alignParentRight="true"
android:background="#drawable/search_btn"
android:enabled="false"
android:gravity="center_vertical" />
</RelativeLayout>
changing the background of editText can solve my problem
i prefer relative layout and if you write android:layout_toLeft or android:layout_toRight it puts them beside correctly but even after that it doesnt, there is ALIGN_TOP or ALIGN_BOTTOM or ALIGN_BASELINE.
I had the same problem as you and turned to relative layout. Its easier to me.
He is an example of my TextView, which goes off the right side of the screen. I tried setting paddings and stuff, but nothing seemed to work. Any ideas? Here is my hierarchy,
ScrollView,TableLayout
<TableRow>
<TextView
android:layout_column="1"
android:id="#+id/text_price"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textCapCharacters"
android:padding="2dip"
android:text="#string/game_price"
/>
<EditText
android:id="#+id/gameprice"
android:inputType="textCapCharacters"
android:gravity="right"
android:minWidth="120dip"
/>
</TableRow>
Try setting the width of the textview to wrap_content, remove the layout_column=1 (not necessary, afaik), and set the height and width of the edittext to wrap_content.
Anyway it's weird to have a textview filling the screen and an edittext to its right with a width of at least 120dip. If you stick to a TableLayout maybe you'll have to play with the weights of the elements, tho I'm not too sure of how this works in a tableLayout. To fill the width of the screen, define that in the TableLayout with fill_parent.
If what you want is a TextView taking all the space left by the EditText placed to the right, a RelativeLayout would do the work
<RelativeLayout>
<EditText
android:id="#+id/gameprice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textCapCharacters"
android:gravity="right"
android:minWidth="120dip"/>
<TextView
android:id="#+id/text_price"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textCapCharacters"
android:padding="2dip"
android:text="#string/game_price"
android:layout_toLeftOf="#id/gameprice"/>
</RelativeLayout>
You'll need to play with the placement of the EditText, the TextView will stick to its left.