I have a TextView that I want centered in my layout, so I have the following:
<TextView
android:id="#+id/workoutText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="This is dynamic"/>
My problem is that I don't want the centering placement to be re-evaluated every time the text changes. When the text changes to "This is longer dynamic text", the center of the text will be re-evaluated thus pushing the text over to the left on the screen. I only want the center to be evaluated based on my initial static text I put in the layout. Is there a way to do this?
I have succeeded in accomplishing my end goal by placing another TextView below with:
<TextView
android:id="#+id/workoutText2"
android:layout_width="wrap_content"
android:layout_height="0px"
android:layout_centerInParent="true"
android:text="This is longer dynamic text"
android:alpha="0"/>
But this feels a bit hacky and would fail to work if the text dynamically changed to something longer than "This is longer dynamic text".
you should add a gravity proprety to the TextView like this
<TextView
android:id="#+id/workoutText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:text="This is dynamic"/>
Related
How do you set a text leading on a TextView? Or how do you shift a text position relative to a view that it is in?
I want the height of the textview to be less than what wrap_content would set at default without cutting the text from the bottom.
I've tried using the layout_height tag on its xml layout but it would clip the text from the bottom just like the image I've provided on the link below, and I also have tried using the lineSpacingExtra tag but it seems to only work for the distance/leading between two lines.
Here is my xml file:
<TextView
android:layout_width="0dp"
android:layout_height="30dp"
android:text="clipped text"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:lineSpacingExtra="-18dp"
android:text="a text longer than usual to demonstrate line spacing"/>
Image for clarity: https://i.stack.imgur.com/QGV4B.png
Why you can use wrap_content? Or can you share what are you trying to achieve?
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="clipped text"/>
Did you try to do it using padding or paddingTop ? Try it with different values to get your desired result.
<TextView
android:layout_width="0dp"
android:layout_height="30dp"
android:paddingTop="0dp"
android:text="clipped text"/>
I need to have an Android layout with two views. The first view is a TextView while the second is an ImageView. The ImageView should always be aligned to the right of the TextView. The TextView should be able to expand to fill any remaining space depending on the size of the text. The ImageView should never be hidden by the TextView if the text is too big. The text should be tail truncated in this case.
Here's a visual of what I'm trying to accomplish:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="bottom | center_vertical">
<TextView
android:id="#+id/myText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/black"
android:ellipsize="end"
android:singleLine="true"/>
<ImageView
android:id="#+id/myImage"
android:layout_width="12dp"
android:layout_height="12dp"
android:src="#drawable/myImageDrawable"
android:layout_toRightOf="#id/myText"/>
</RelativeLayout>
The above XML does not work because TextView hides the ImageView when its text is too big. How can I fix this code? I'm willing to use a different layout as well. Note that the TextView must be a single line.
The below xml is enough to achieve what you want. Just set singleLine to true and use drawableEnd to set your image. Also, replace the text in my code with yours. That's all.
<TextView
android:singleLine="true"
android:text=" HehhsasasashasgghgahgshagshgahsghagshaghsgahsghaHehhsasasashasgghgahgshagshgahsghagshaghsgahsgha shgasagsasghag shahsghag"
android:layout_width="wrap_content"
android:drawableEnd="#drawable/myImageDrawable"
android:layout_height="wrap_content"/>
I'm trying to make an app that involves writing some poems in Arabic language, however I'm having difficulties in making the poem look justified to look nice when reading them.
I'm using TableLayout to fill in the poem:
<TableLayout
android:id="#+id/whole_line_table"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_vertical"
android:stretchColumns="1"
>
<TableRow
android:padding="6dp">
<TextView
android:layout_width="0dp"
android:id="#+id/poem2_textView"
android:layout_weight="1"
android:gravity="right"
android:textSize="15sp"
tools:text="Some text here"
/>
<TextView
android:id="#+id/poem1_textView"
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="right"
android:textSize="15sp"
tools:text="Some text here"
android:maxLines="1"/>
</TableRow>
</TableLayout>
Example of how I want the poem to appear in the image below
verse example
You can see how each verse in the poem start and end together and notice the space between them.
Screen shot of what I have in layout so far shown below
enter image description here
I want the right part to reach to end not half the textView only.
I'm totally new to developing apps so please excuse me for any bad coding look
set text width to fill the parent and set margin to element.
use gravity center.
<TextView
android:id="#+id/poem1_textView"
android:layout_width="match_parent"
android:layout_weight="1"
android:gravity="center"
android:textSize="15sp"
android:margin="18dp"
tools:text="Some text here"
/>
that could help you to simulate an justified text, if you want a better fix or if you are using android 8.0. it supports justification on texts.
Look at this answer and try those libraries.
Android TextView Justify Text
I Hope it helps.
The situation is the same no matter if I use LinearLayout or RelativeLayout. I think this is a old Android XMl bug, but I have no idea why it still exists.
Namely, in a layout where an ImageView is on the right side of a TextView can disappear from the screen when text becomes too long. It simply pushes it off the screen. I must NOT make TextView single line.
Here is the XML.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="#+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="some long text blahblahblahblahblah"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="17sp"
android:layout_marginRight="10dp"
android:layout_centerVertical="true"
/>
<ImageView
android:layout_toRightOf="#+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/key"
android:layout_centerVertical="true"
/>
</RelativeLayout>
I cannot use layout_weight attribute as image will then stick to the right side of the screen. It HAS to be on the right side of the text.
Anyone has any ideas how to solve this bug?
Check images when the text is short and long. On the 2nd image the text is long and the image is being pushed away from the screen.
You can achieve your task, if you use the layout_weight properly. Please refer the code below:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<TextView
android:id="#+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="The quick brown fox jumps over the lazy dog. As you can see it easily handles the long length of the text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="17sp"
android:layout_marginRight="10dp"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"
/>
</LinearLayout>
The output for long text:
The output for short text:
EDIT:
This works because of the layout whose width is set as wrap_content. If it were match_parent then in all cases, all the extra space would have been given to TextView because of it's layout_weight but since the parent is wrap_content, there is no extra space for the TextView to fill when the text is small. But when the text is large, the weight property is applied to the TextView but since there is no layout_weight for the ImageView hence, it takes only the amount of space it has to.
Set
android:layout_marginRight="30dp"
for the TextView
and
android:layout_marginLeft="-20dp"
for the ImageView
You can change the values depending on your needs.
But, if the image can have different sizes, you will have to set margins programmatically.
there is 2 solutions for these
either set the image as a drawableRight to the textview
<TextView
android:id="#+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="The quick brown fox jumps over the lazy dog. As you can see it easily handles the long length of the text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="17sp"
android:layout_marginRight="10dp"
android:drawableRight="#drawable/ic_launcher"
/>
or
alignt the image to right and make textview toleft of the image
I have two text views(TV_1,TV_2).I want to set the TV_2 is immediate right of the TV_1.
It means
suppose TV1 occupy the one entire line in the screen then TV2 will set below the TV1.
Suppose Tv1 occupy the one and half line then tv2 will occupy the rest of the space. How to implement this? i tried but i am not getting. It is similar to one paragraph.
<RelativeLayout
android:id="#+id/xrays_dicom_alert_TnC"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_marginTop="20dp">
<CheckBox
android:id="#+id/xrays_dicom_alert_cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"/>
<TextView
android:id="#+id/xrays_dicom_alert_TnC_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/xrays_dicom_alert_cb"
android:text="I have read I agree to the following"
android:textColor="#color/black"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/xrays_dicom_alert_statement"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/xrays_dicom_alert_cb"
android:layout_below="#id/xrays_dicom_alert_TnC_text"
android:layout_alignRight="#id/xrays_dicom_alert_TnC_text"
android:text="Statement"
android:textColor="#color/black"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
Thats impossible act in Android.
A View occupies a rectangular area on the screen.
http://www.androidadb.com/class/android/view/View.java.html
Or you have to do something like this
To make clickable some text in TextView one can go with Spannable concept.
http://developer.android.com/reference/android/text/Spannable.html
use URLSpan http://www.androidadb.com/class/ur/URLSpan.html
I think it's not possible in the pattern what you have asked.