How to add space between two textviews - android

I am new to android development and I was just wondering how can I add space between two TextViews? Any help would be appreciated.
The code I have written so far
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/lbl_group_coworkers"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Coworkers" />
<TextView
android:id="#id/lbl_group_"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Family"/>
</LinearLayout>

You can use android:layout_marginTop="value"
like this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/lbl_group_coworkers"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Coworkers" />
<TextView
android:id="#id/lbl_group_"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Family"/>
</LinearLayout>

you can have margin between two textview.
add top margin to the second textview
like this
<TextView
android:id="#id/lbl_group_"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Family"/>

Just replace <LinearLayout> </LinearLayout>
with <RelativeLayout> </RelativeLayout>
then go in graphical layout and adjust space as you like.

add margin left right top bottom
android:layout_marginLeft="10dp"

try adding margin
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/lbl_group_coworkers"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Coworkers"
android:layout_margin="10dp"/>
<TextView
android:id="#+id/lbl_group"
android:layout_margin="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Family"/>
</LinearLayout>

You can use
android:layout_margin="5dp"
or
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
But before you ask a lot more such questions, I'd suggest to read through the android dev guides ( http://developer.android.com/guide/components/fundamentals.html )
Good Luck and have fun developing...

You can use either padding or margin depending on what you need, here is a link of a guy who gives a good explanation between the two that will help you decide on which one you'd like to use: android margin vs padding

add android:layout_marginRight="..." to the first textView

set margin property in TextView...
android:layout_marginTop="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="20dp"
android:layout_marginLeft="20dp"
and if all side set space then...
android:layout_margin="20dp"

Use GridLayout instead of LinearLayout
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A"
android:layout_gravity="left" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B"
android:layout_gravity="right" />
</GridLayout>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/custom_border"
>
<TextView
android:text="#string/textView_reference_number"
style="#style/CustomTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView_refernce_number_label"
/>
<TextView
android:text="Reference Number Value"
style="#style/CustomTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView_refernce_number"
android:layout_marginTop="10dp"/>
</LinearLayout>
android:layout_marginTop XML attribute is used to specify extra space on the top side of this view. Thus android:layout_marginTop="10dp" on 2nd textView is specifying 10 dp of space between it and above view. #UDI Please refer below link for more details over the same https://developer.android.com/reference/android/view/ViewGroup.MarginLayoutParams.html

Related

Android TextView Text wont wrap on Listview

hello i am trying to wrap a textview that has a long text. but when i test it, it just goes beyond the screen. What im trying to do is it will go to the next line if the text is long and it automatically adjust the layout. I am using this in a listview.
i am referring to this old question Android TextView Text not getting wrapped, it works on him so i tried to implement it but it wont effect on mine. thanks for answering.
here's my XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:background="#e6e6e6"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<LinearLayout
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical" android:padding="4dip"
android:layout_weight="1">
<TextView
android:id="#+id/tv_reviewer_name"
android:textSize="16sp"
android:textAppearance="?android:textAppearanceLarge"
android:text="Reviewer Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_marginTop="5dp"
android:id="#+id/tv_review_details"
android:textSize="16sp"
android:textAppearance="?android:textAppearanceSmall"
android:text="Review"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:width="0dip"/>
</LinearLayout>
</LinearLayout>
UPDATE
here's my listview:
<ListView
android:divider="#android:color/transparent"
android:dividerHeight="2.0sp"
android:id="#+id/lv_reviews"
android:background="#f2f2f2"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
You can use the inputType to auto manage a multiline text.
<TextView
android:layout_marginTop="5dp"
android:id="#+id/tv_review_details"
android:textSize="16sp"
android:textAppearance="?android:textAppearanceSmall"
android:text="Review"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:width="0dip"
android:inputType="textMultiLine"
/>
if i use listView or recyclerView, i well put the layout width match parent as mentioned below:
<LinearLayout
<!--change to match_parent-->
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" android:padding="4dip"
android:layout_weight="1">
try it once, hope this will help you.
problem solved. it turns out android studio didnt update my changes when i ran my app. i uninstalled the app and then run it again and it works perfectly.
When you are using Gravity in vertical orientation, you have to use layout_height=0dp & when you are using Gravity in Horizontal orientation then you have to use layout_width=0dp for every child of Linear Layout so that it can resize the layout by itself
<LinearLayout
android:background="#e6e6e6"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<LinearLayout
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical" android:padding="4dip"
android:layout_weight="1">
<TextView
android:id="#+id/tv_reviewer_name"
android:textSize="16sp"
android:textAppearance="?android:textAppearanceLarge"
android:text="Reviewer Name"
android:layout_width="wrap_content"
android:layout_height="0dp" />
<TextView
android:layout_marginTop="5dp"
android:id="#+id/tv_review_details"
android:textSize="16sp"
android:textAppearance="?android:textAppearanceSmall"
android:text="Review"
android:layout_height="0dp"
android:layout_width="match_parent"/>
</LinearLayout>
</LinearLayout>

Display a TextView 5dp from the bottom

Android Studio 0.3.7
Hello,
I have the following TextView that is on a Linearlayout (vertical).
I want to display the TextView just about 5dp from the bottom of the layout. However, I can't seem to do that.
I have played around with the different properties of the android:layout_marginBottom and some other properties as well.
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Sign Up with StarBucks"
android:id="#+id/tv_signup"
android:clickable="true"
android:textColor="#380aff"
android:enabled="true"
android:textStyle="bold|italic"
android:gravity="center_horizontal"
android:layout_marginBottom="5dp"/>
Can anyone tell me what is the correct property to set to get the Textview positioned from the bottom of the layout.
Many thanks for any suggestions,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center" >
//other controls define here
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:gravity="center"
android:text="TextView" />
</LinearLayout>
Use this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:gravity="center"
android:text="TextView" />
</LinearLayout>
The textView have to be child of the RelativeLayout then You can use alignParentBottom="true" and set marignBottom
Set android:layout_gravity to bottom
try this...
<TextView
android:padding_bottom="5dp"
android:gravity="bottom|center_horizontal"
........ />
Try setting android:layout_gravity="bottom"
or else if you want your textview to be horizontally center then you may set
android:layout_gravity="bottom|center_horizontal"

Layout views with dynamic width

I have a layout with two children one after another, like this:
|<view1><text1> |
<view1> may change it's width. So, I want it increases while both views stay on the screen:
|<really_wide_view1><text1> |
But when there is no more space at right, it stops:
|<reeeeeeeeeeeally_wide_vi...><text1>|
Is there easy way to do this?
Now I try to use this layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FF00aa00" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="loooooooooooooooooooooooooooooooooooong text" />
</LinearLayout>
<TextView
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#FFaa0000"
android:minWidth="30dp"
android:singleLine="true"
android:text="test" />
</LinearLayout>
But result is the same:
I am able to solve your problem using the following code (a modified version of yours):
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#FF00aa00" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong text"
/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="0"
android:background="#FFaa0000"
android:text="testtest" />
</LinearLayout>
Here is a screenshot of the same:
Let me know if the above piece of code solves the problem.
Try to give both views inside the RelativeLayout and make the views layout width and as a wrap_content and give orientation as horizontal. I think it will solve your problem.
Here is solution with TableLayout, but it looks dirty.
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:shrinkColumns="0" >
<TableRow>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
</TableLayout>
It works for any length of both text views.

Button in relative layout not affected by setting layout margins

A custom Dialog box with a RelativeLayout contains a Button widget that won't change its margins, regardless of direction. Here's the xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="horizontal" android:background="#99000000">
<TextView android:id="#+id/dialogtitle" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="Some text" />
<TextView android:id="#+id/dialogtext" android:layout_width="300dp"
android:layout_height="wrap_content" android:textSize="15dp"
android:layout_alignParentLeft="true" android:layout_below="#id/dialogtitle"
android:paddingLeft="8dp" />
<Button android:id="#+id/dialogbuttoninfo" android:layout_width="80dp"
android:layout_height="wrap_content" android:text="Doesn't care about margins"
android:layout_alignParentRight="true" android:layout_marginLeft="128dp" />
</RelativeLayout>
Padding works but only moves the text inside the button. Any suggestions?
This seems crappy but have you tried putting it all in a LinearLayout instead? or perhaps removing android:orientation="horizontal". I dont think RelativeLayout cares about orientation from what I've seen. Also I think, but might be wrong, that if you do a LinearLayout then you won't need to have android:layout_alignParentLeft="true" in there either.
After cleaning it up a bit (sorry but its so hard to read when its compressed as it was in the question) you also didn't state where the last TextView , dialogbuttoninfo was supposed to be relative to everything else, I think you have to do that for Relative layouts to behave properly, I've had some squirrely things happen.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#99000000">
<TextView
android:id="#+id/dialogtitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Some text" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/dialogtext"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:textSize="15dp"
android:layout_alignParentLeft="true"
android:paddingLeft="8dp" />
<Button
android:id="#+id/dialogbuttoninfo"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="Doesn't care about margins"
android:layout_alignParentRight="true"
android:layout_marginLeft="128dp" />
</LinearLayout>
</LinearLayout>
You may need to align the left of the button against something before the margin has real meaning.

How to display a TextView below an other one in Android with XML files?

I am pretty sure that a parameter will do the trick but I can't find the one I am looking for.
I am trying to display one TextView -file_type- below the file_title TextView.
What would be the parameter that I should add to the file_type TevxtView block to come under the file_title TextView block ?
There is what I am doing :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView android:id="#+id/file_type_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="25px"
android:paddingTop="25px" />
<TextView android:id="#+id/file_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="30px"
android:textSize="22sp"
android:background="#FF0000"
android:textColor="#FFFFFF" />
<TextView android:id="#+id/file_type"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="30px"
android:paddingTop="10px"
android:layout_gravity="bottom"
android:textSize="22sp"
android:background="#FF0000"
android:textColor="#FFFFFF" />
</LinearLayout>
Thank you ,
By default linearlayout wraps things horizontally. If you want the imageview to be at the left of the both textviews (that are wrapped vertically), use following:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView .../>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:orientation="vertical">
<TextView1..../>
<TextView2..../>
</LinearLayout>
</LinearLayout>
Or you can just pass the parameter android:orientation="vertical" to the top level LinearLayout. Have a look at RelativeLayout definition as well.

Categories

Resources