How to Align the Text Properly in TextView? - android

I have a Textview in which i need to show large information in proper format.How can i achieve it?
I want to display like this:
Title : Some Title.
Date : Date
More Info about the title.
......
......
Contact : Contact Details.

Well you have to do it by calculating the space between the options.
The better way
Use a Webview make an html string with contents aligned properly using html formattings and load webview and display it.

If using a TextView is not essential you can use TableLayout:
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow>
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:text="Title"
android:layout_weight="1"/>
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:text="Some Title"
android:layout_weight="2"/>
</TableRow>
<TableRow>
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:text="Date"
android:layout_weight="1"/>
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:text="Some Date"
android:layout_weight="2"/>
</TableRow>
</TableLayout>

I think there are three options.
If font doesn't matter use android:typeface="monospace" in your textview and align through spaces
If font does matter design it in html and use a WebView
If you don't want to work with html, you need to make the layout through different TextView's which is imo always better.

As suggested by others, WebView is the option for you. See
How to programmatically set / edit content of webview
However, it is possible to do it in a TextView if the text doesn't need special formatting (ex. different color, font sizes) and the only formatting required is indentation.
In layout:
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="false" />
In Activity:
TextView tv = (TextView) findViewById(R.id.text);
tv.setText("Title : Some Title.\nDate : Date\n\nMore Info about the title.\n......\n......\n\nContact : Contact Details.");

Related

justifing a text (that is, make it fit in a textview and cover all space)

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.

Button with left icon and text perfectly aligned

Good evening,
I am developing an Android app and I am currently doing the Login interface in XML.
I am trying to create buttons with icon and text, like in the picture below :
http://i.imgur.com/J5Cj1w4.png
And here is my actual result :
http://i.imgur.com/VPALdDD.png
With this code :
<Button
style="?android:textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/sign_up_facebook"
android:text="#string/signup_with_facebook"
android:textColor="#color/primaryTextLight"
android:drawableLeft="#drawable/ic_facebook_box_white_24dp"
android:layout_weight="0"
android:background="#drawable/button_shape_login"/>
<Button
style="?android:textAppearanceSmall"
android:layout_marginTop="20sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/sign_up_google"
android:text="#string/signup_with_google"
android:textColor="#color/primaryTextLight"
android:drawableLeft="#drawable/ic_google_plus_box_white_24dp"
android:layout_weight="0"
android:background="#drawable/button_shape_login"/>
I am stuck at this step.
How can I get the final needed result with XML code ?
Thank you!
You can give padding to drawable like this : android:drawablePadding="50dip"
Also this question answered in this post.
Use android:drawablePadding attribute
You only need to specify android:paddingLeft attribute.
Try specifying a value of 36dp for example
The better option is to actually make the button a Relative/Linear layout with the layout set inside, the drawablePadding will not work so well with different lengths of text and buttons.
Essentially, RelativeLayout which is your button with a nested ImageView and TextView and you'll have good control of the layout, with consistent paddings around images and text within the button.
I haven't tested the following, this is essentially what you need
<LinearLayout
android:id="#+id/facebookButton"
android:layout_width="250dp"
android:layout_height="80dp"
android:orientation="horizontal">
<ImageView
android:src="#drawable/your_drawable"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="50dp"/>
<TextView
android:text="Sign up with Facebook"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"/>
</LinearLayout>
android:gravity="center_vertical"
//use in both textView
<Button
android:paddingLeft="50dp"
android:textAlignment="textStart"
android:drawableLeft="#drawable/"
/>

TextView inside a TextView

I have something like "There are 200€" but I want to style the value (ex. textColor Red)
Is there any way of having two textViews to seem like an entire one?
A textView inside a textView or a textViewContainer or something.
I can achieve it on the run like this answer: Change text color of one word in a TextView
But I want to do from the layout file. Any chance?
Thanks
EDIT
<TextView
android:id="#+id/lastIncome"
android:text="Tu último ingreso fueron"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="#dimen/dash_font_size"/>
<TextView
android:id="#+id/lastIncomeValue"
android:text="200€"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="#dimen/dash_value_font_size"
android:textColor="#color/greensea"
android:layout_gravity="right"/>
You can achieve this with a horizontal LinearLayout. The LinearLayout is the container for the two side by side TextViews. This layout can be placed in any other container (LinearLayout, RelativeLayout, etc.) in your XML.
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="There are " />
<TextView
android:id="#+id/amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="200€"
android:textColor="#ff0000"/>
</LinearLayout>
You can use a easy approach to do this within one textview using SpanableString Builder Click here to get this approach

android - How to arrange the two textviews side by side

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.

Android: how to align two text views based on length of text

I have a layout in which two TextViews are to be displayed on the same line such that
If TextView1 is a short text, TextView2 should be immediately right to TextView1(IMAGE1) and if the TextView1 is a long text, TextView2 should be at right corner of the Layout on the same line(IMAGE2)
How can I achieve this ?
i use simple horizontal LinearLayout with android:layout_weight attribute and it worked like you want.
Example:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/text1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="teeeeeeeext1"/>
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text2"/>
</LinearLayout>
Use a layout like this..
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/editText2"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="38dp"
android:layout_toRightOf="#+id/editText1" />
</RelativeLayout>
edittext 1 will push edittext2 to its right...depending on text in it..
You can set android:maxWidth property for first text view. So your layout would look like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxWidth="200dp"
android:text="sdfksdofsdfsdfsdfsadfgwagrswargweqrgeqrgqwergeqrgeqrg"
/>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/textView1"
android:text="text2"
/>
Edit:
I apparently misread (or did not read fully) your question. Just don't mind my answer - I vote for mxy's :)
The last time I had the same problem, I wasn't able to find or hack away a straightforward solution. Counting pixels was not an option (at least not for me). But I found a workaround that eventually led to the same display concept, and that was to use SpannableStringBuilder.
As I'm assuming you want two different TextViews because you want them to have different colors (or size, or style, or typeface).
The idea is to use a single TextView for the whole thing, and prepare a SpannableString before doing setText on your view.
By using combinations of ForegroundColorSpan, TextAppearanceSpan, etc, you can make your single TextView look like different TextViews with different styles, sitting side by side, wrapping to the next line as necessary.
Links:
Setting font color at runtime
TextAppearanceSpan sample

Categories

Resources