Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to let a TextView have multiple lines?

I want to show multiple line via TextView and strings.xml . I want to show first few lines until the middle of the page and other lines to show in full of the page. I want to show first few lines with same size of width page.

Left of the page is a picture and the right of the page is my sentences.

This is my code but this shows dishevel.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<ImageView 
    android:id="@+id/logoImage"
    android:src="@drawable/ic_launcher"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
<TextView
    android:id="@+id/txtIntroduce"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/txtIntroduce"
    android:textColor="@color/blue_text"
    android:background="@color/blue"/>
</LinearLayout>

strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
     <string name="intro">.................  ....</string>
<resources>

How can i show this view? Sorry for my poor english and thanks for your help.

like image 885
SensorS Avatar asked Sep 09 '13 07:09

SensorS


4 Answers

You could have a look at these three xml properties of the TextView:

<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:ellipsize="end"
    android:maxLines="5"
    android:singleLine="false" />

There you can define how many lines the TextView should have, and if there should be displayed dots ("...") when the text exceeds the TextView size.

Also, you can use return inside your strings .xml to start a new line: ("\n")

<string name="intro">This is the first line \n this is a new line.</string>
like image 190
Philipp Jahoda Avatar answered Oct 25 '22 08:10

Philipp Jahoda


You need this 4 properties:

  1. android:layout_width (set a value let's say 300dp)
  2. android:ellipsize="end"
  3. android:maxLines="number of lines u want"
  4. android:singleLine="false"

for example: enter image description here

like image 38
sidibe Avatar answered Oct 25 '22 06:10

sidibe


It sounds like you want to wrap text around a picture, like so:

--------------------
|..........| xxxxxxx
|..Picture.| xxxxxxx
|..........| xxxxxxx
------------ xxxxxxx
xxxxxxxxxxTextxxxxxx
xxxxxxxxxxxxxxxxxxxx

I think the easiest option is to a WebView. However, according to this you can also use image tags in a TextView. I've never tried it myself, but I have used the other tags like so: TextView.setText(Html.fromHtml("<b>some bold text</b> some normal text")) so maybe something similar will work in your situation.

like image 43
Merk Avatar answered Oct 25 '22 07:10

Merk


Here's my first post on stackoverflow...

I think this is the best an simple way to have a multiline textView on android. Here we go:

  1. Write your text in an external editor (ex. Microsoft Word, LibreOffice, etc) with paragraphs and multiple lines.

  2. Open the strings.xml file of your project and create a new string (ex. <string name="my_multiline_textview></string>).

  3. Copy and paste each paragraph from your text inside the tags, putting an \n at the end.

  4. As many \n you put at the end of paragraph, as many lines between them.

  5. Insert a new textView on your layout and associate it with the multiline string created on steps 2, 3 and 4 (android:text="@string/my_multiline_textview").

  6. Go back to the Graphical Layout and see the magic happening :-)

I hope this info can help yo all. See ya.

like image 25
Jose Carlos Avatar answered Oct 25 '22 06:10

Jose Carlos