I want to get next result in my layout:
[left textview] [space] [right textview]
For this purpose I use it:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingBottom="10dp"
android:paddingTop="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="left"
android:id="#+id/left"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="right"
android:id="#+id/right"/>
</RelativeLayout>
It's okay. But if left TextView contains a large text, the result will be next:
[left textview] (under left textview [right textview]. So left override right. But I want that left TextView doesn't override right, and when left TextView reaches right TextView the Left TextView go down into multiline. So I want to get next result:
some-text-text-left-multiline
some-text-text-left-multiline text-in-right-text-view
some-text-text-left-multiline
Use a linear layout (horizontal) and place textview with the same android:layout_weight inside to make a 50/50 split
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/left"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="#+id/right"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
This expands the left until reaching the right using android:layout_toLeftOf
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#id/right"
android:text="this is a long text on the left which should be multiline and it is" />
<TextView
android:id="#+id/right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="some text" />
</RelativeLayout>
Do this to your layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:gravity="right"
android:minWidth="50dp"
android:maxWidth="100dp"
android:text="right" />
<TextView
android:id="#+id/left"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:gravity="left"
android:layout_toLeftOf="#+id/right"
android:text="left" />
</RelativeLayout>
Related
I want the text to be displayed to the right of the name,but if the name is too large, the text on the right doesn't fit on the screen. I used the following solution, but it causes the first textview to stretch beyond the size of the text in it.
What I have:
screenshot
What I want:
concept
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="true"
android:orientation="horizontal"
android:weightSum="1">
<TextView
android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignWithParentIfMissing="true"
android:layout_weight="1"
android:fontFamily="#font/montserrat"
android:singleLine="true"
android:text="1"
android:textColor="#color/colorText"
android:textSize="20sp" />
<TextView
android:id="#+id/readed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center_vertical"
android:layout_marginStart="10dp"
android:background="#drawable/back_new"
android:fontFamily="#font/montserrat"
android:gravity="center"
android:keepScreenOn="true"
android:paddingHorizontal="5dp"
android:paddingVertical="3dp"
android:singleLine="true"
android:text="554"
android:textColor="#color/colorWhite"
android:textSize="18sp" />
</LinearLayout>
just remove android:weighSum="1" and android:layout_weight attribute in two textViews
enter code here<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="true"
android:orientation="horizontal">
<TextView
android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignWithParentIfMissing="true"
android:fontFamily="#font/montserrat"
android:singleLine="true"
android:text="3"
android:textColor="#color/colorText"
android:textSize="20sp" />
<TextView
android:id="#+id/readed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center_vertical"
android:layout_marginStart="10dp"
android:background="#drawable/back_new"
android:fontFamily="#font/montserrat"
android:gravity="center"
android:keepScreenOn="true"
android:paddingHorizontal="5dp"
android:paddingVertical="3dp"
android:singleLine="true"
android:text="554"
android:textColor="#color/colorWhite"
android:textSize="18sp" />
</LinearLayout>
I've got an issue with my TextView, I just can't center it. Here's what it should look like :
And it's actually look like that
Here's my XML code
<?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">
<TextView
android:id="#+id/magnitudeTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_margin="10dp"
android:textSize="15sp" />
<TextView
android:id="#+id/cityNameTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_margin="10dp"
android:textSize="15sp" />
<TextView
android:id="#+id/dateTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_margin="10dp"
android:textSize="15sp" />
</RelativeLayout>
Using a LinearLayout with horizontal orientation along with a weightsum of 3 for the children text view would give you a light and more reliable solution, doing it like the following:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="3"
android:orientation="horizontal">
<TextView
android:id="#+id/magnitudeTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:text="test"
android:textSize="15sp" />
<TextView
android:id="#+id/cityNameTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test"
android:gravity="center"
android:layout_weight="1"
android:layout_margin="10dp"
android:textSize="15sp" />
<TextView
android:id="#+id/dateTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="10dp"
android:textSize="15sp" />
</LinearLayout>
Now each of your text views will take exactly 1/3 of the whole screen width, if you want the center one to be bigger you can play with the layout_weight value in each of the text view to match your desired design.
Instead of RelativeLayout you can use LinearLayout in which you can set the weightsum to 3 that will give you more reliable solution. Every TextView will get the equal amount of space in a row.
The Below technique work for me.That is:
Use your first TextView as left orient and middle TextView as center oriented and last TextView as right oriented and apply weight for each TextView.
Your code it would be 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="match_parent"
orientation=vertical
weightsum=3
>
<TextView
android:id="#+id/magnitudeTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_margin="10dp"
android:textSize="15sp"
android:layout_weight=1 />
<TextView
android:id="#+id/cityNameTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:textSize="15sp"
android:layout_weight=1/>
<TextView
android:id="#+id/dateTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_margin="10dp"
android:textSize="15sp"
android:layout_weight=1/>
</LinearLayout>
`
A horizontal LinearLayout would provide a better and lighter solution for your problem. What you'd do is wrap your three textviews in it, then use layout_width=wrap_content for the ones to the left and right and layout_width=0dp
layout_weight=1
for the middle one
Generally, a linear layout with weights would suit these kinds of needs, the system will automatically divide the screen into parts depending on the weightsum you mentioned
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum = "5"
android:orientation = "horizontal"
/>
<TextView
android:id="#+id/magnitudeTV"
android:layout_width="0dp"
android:layout_weight = "1"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_margin="10dp"
android:textSize="15sp" />
<TextView
android:id="#+id/cityNameTV"
android:layout_width="0dp"
android:layout_weight = "3"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_margin="10dp"
android:textSize="15sp" />
<TextView
android:id="#+id/dateTV"
android:layout_width="0dp"
android:layout_weight = "1"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_margin="10dp"
android:textSize="15sp" />
</LinearLayout>
You can achieve this by doing this :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true">
<TextView
android:id="#+id/magnitudeTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="sdf34 "
android:textColor="#000000"
android:textSize="15sp" />
<TextView
android:id="#+id/cityNameTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/magnitudeTV"
android:layout_margin="10dp"
android:text="dasdasdsadasdds"
android:textColor="#000000"
android:textSize="15sp" />
<TextView
android:layout_toRightOf="#id/cityNameTV"
android:id="#+id/dateTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="fg25111dfg"
android:textColor="#000000"
android:textSize="15sp" />
</RelativeLayout>
but according to me it is not a good solution for this you can con-catenate all the three strings and than set the final string to one single text view as mentioned below:
String final_string = distance +" "+string_2 +" "+string_3;
textView.setText(final_string);
I'm trying to make my Textview aligned to left have tried. And android:gravity="left" but nothing happened.
This is my XML code:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:text="Email:"
android:textSize="20dp"/>
<EditText
android:id="#+id/editTextAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="#drawable/bg"
android:hint="Enter Address"/>
</LinearLayout>
Try this use code:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_margin="10dp"
android:layout_weight="1"
android:text="Email:"
android:textSize="20dp"/>
<EditText
android:id="#+id/editTextAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/textView11"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_toEndOf="#+id/textView11"
android:layout_weight="1"
android:background="#drawable/bg"
android:hint="Enter Address"/></RelativeLayout>
The gravity attribute is used to set the gravity of the content of the View.
The layout_gravity attribute is used to set the gravity of the view itself with regards to its parent.
So if you wanted to set the gravity of the text in your TextView then you'd use gravity. If you want to set the gravity of the TextView inside the LinearLayout you should use layout_gravity.
try this use RelativeLayout insted of LinearLayout and make your TextView android:layout_alignParentLeft="true"
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:layout_alignParentLeft="true"
android:text="Email:"
android:textSize="20dp"/>
<EditText
android:id="#+id/editTextAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:layout_alignParentRight="true"
android:background="#drawable/bg"
android:hint="Enter Address"/>
</RelativeLayout>
If you want to use Linear Layout then use :
<TextView
android:id="#+id/textView11"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_alignParentLeft="true"
android:text="Email:"
android:textSize="20dp"/>
I have an ImageView aligned to the parent right in a relativeLayout.
Here is my layout:
<RelativeLayout android:id="#+id/row" android:background="#null" style="#style/ListRow"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:textStyle="bold" android:textColor="#color/primary_text" android:text="Username" android:gravity="center_vertical" android:id="#+id/username" android:layout_width="wrap_content" android:layout_height="wrap_content" android:singleLine="true" android:layout_toLeftOf="#id/avatar" />
<LinearLayout android:gravity="center_vertical" android:id="#+id/time_item" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="#id/username" android:layout_alignBottom="#id/username" android:layout_alignParentLeft="true">
<TextView android:textSize="#dimen/font_size_small" android:text="12:00AM" android:textColor="#color/secondary_text" android:gravity="center_vertical" android:id="#+id/date" android:layout_width="wrap_content" android:layout_height="wrap_content" />
</LinearLayout>
<RelativeLayout android:id="#+id/avatar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5.0dip" android:layout_alignParentRight="true">
<ImageView android:id="#+id/picture" android:src="#drawable/ic_launcher" android:layout_width="48.0dip" android:layout_height="48.0dip" android:scaleType="center" android:adjustViewBounds="true" />
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" />
</RelativeLayout>
<TextView android:textSize="#dimen/font_size_small" android:text="aslkndalskndlaksndlaskndlaksnlansldnaslkndalsdnaslkdnls" android:textColor="#color/primary_text" android:id="#+id/body" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_toLeftOf="#id/avatar" android:layout_below="#id/username" android:lineSpacingMultiplier="1.2" />
</RelativeLayout>
Here is what it looks like:
When starting a new line of text I want the text to align on to the right instead of the left. I have photoshopped what I want. Here is what I want:
Anyone know how I should modify my layout to where it does this? Thanks!
Just add one more property in your TextView body.
android:gravity="right"
You are missing the android:gravity="right" element from your TextView Body. It should go like
<TextView android:gravity="right"...
That simply makes the text originate on the right side of the TextView
I have the following ImageView and TextView:
Here is the XML:
<LinearLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:id="#+id/headerLinearLay" android:orientation="horizontal">
<ImageView android:src="#drawable/icon" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="#+id/avatarImageView"></ImageView>
<TextView android:layout_height="wrap_content" android:id="#+id/usernameTextView" android:text="TextView" android:layout_width="wrap_content" android:paddingLeft="4px"></TextView>
</LinearLayout>
How can I make the image and the text be positioned at the same height? I also want the ImageView to be in the corner
Try this:
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="#+id/headerLinearLay">
<ImageView
android:id="#+id/avatarImageView"
android:src="#drawable/icon"
android:layout_height="wrap_content"
android:layout_width="wrap_content"></ImageView>
<TextView
android:id="#+id/usernameTextView"
android:text="TextView"
android:paddingLeft="4dp"
android:layout_toRightOf="#id/avatarImageView"
android:layout_centerVertical="true"
android:layout_height="wrap_content"
android:layout_width="wrap_content"></TextView>
</RelativeLayout>
Here is a simple solution that works well. Essential here is to add: android:layout_gravity="center_vertical" to TextView
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon" />
<TextView
android:id="#+id/usernameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical" />
</LinearLayout>
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_weight="0.04" />
<ImageView
android:id="#+id/imageView"
android:layout_width="204dp"
android:layout_height="200dp"
app:srcCompat="#mipmap/ic_launcher" />