1

I'm making app with using Xamarin.forms.

My problem is there is no way to create TWO line TextView. The text will be automatically go to next line even if I set enough width.

If I want to create single line, I set singleline property as true and setMaxLine as 1.

But the Two line, If I set both of LineNumber and MaxLineNumber, it does not work. Text will makes third line. for example, if text value is "I'm a baby not robot.\nPlease give me a milk", it goes like

I'm a baby not robot.
Please give me 
a milk

How to solve this problem?

Thanks.

Bright Lee
  • 2,306
  • 2
  • 27
  • 55
  • try to view [this](http://stackoverflow.com/questions/5460256/string-resource-new-line-n-not-possible) hope it is help – Francesco Taioli Dec 12 '16 at 16:30
  • I'm sorry about it. I'm trying to make two line TextView not EditText. I edited question. – Bright Lee Dec 12 '16 at 18:20
  • There is no TextView in Xamarin Forms. Can you clarify? – Yuri S Dec 12 '16 at 22:08
  • @YuriS It's Label on XF, TextView is native textview on Android side, isn't it? – Bright Lee Dec 13 '16 at 02:36
  • so, did my answer about TextView work for you or you need multiline Label of XForms? – Yuri S Dec 13 '16 at 03:02
  • @YuriS I'll try your answer, but I don't know well about implementing xml layout for XF.Android but I used custom renderer. and I do need two line(fixed) label not multiple label. XF's label is basically multiple label. – Bright Lee Dec 13 '16 at 03:32
  • Please try what @Elvis Xia suggested and let us know if you need more help – Yuri S Dec 13 '16 at 16:47
  • @YuriS I'm sorry for late reply, his suggestion is actually what I did before posting a question. It does not work, maybe because my situation is little bit different. But actually that does not guarantee cutting text automatically. – Bright Lee Dec 14 '16 at 18:41
  • 1
    you can share complete sample solution demonstrating the problem – Yuri S Dec 14 '16 at 18:53
  • @YuriS I thought that I should. But I'm sorry about it. I guess it's layout problem like Elvis says not like how to use custom renderer. I think I should use different approach. Thanks for you guys so far. – Bright Lee Dec 16 '16 at 14:15

3 Answers3

2

By default all the EditText widgets in Android are multi-lined.

Here is some sample code:

<EditText
    android:inputType="textMultiLine" <!-- Multiline input -->
    android:lines="8" <!-- Total Lines prior display -->
    android:minLines="6" <!-- Minimum lines -->
    android:gravity="top|left" <!-- Cursor Position -->
    android:maxLines="10" <!-- Maximum Lines -->
    android:layout_height="wrap_content" <!-- Height determined by content -->
    android:layout_width="fill_parent" <!-- Fill entire width -->
    android:scrollbars="vertical" <!-- Vertical Scroll Bar -->
/>
Simon Schnell
  • 1,160
  • 14
  • 24
0

Here is what works for me

<TextView
    android:text="I'm a baby not robot.\nPlease give me a milk"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/textView1"
    android:singleLine="false"
    android:maxLines="10" />

enter image description here

Yuri S
  • 5,355
  • 1
  • 15
  • 23
0

But the Two line, If I set both of LineNumber and MaxLineNumber, it does not work. Text will makes third line.

You can create a custom label control and consume it in your Xaml:

CustomLabel:

public class CustomLabel:Label
{
}

Xaml:

<ContentPage ...
         xmlns:local="clr-namespace:LabelDemo"
         ...>
<local:CustomLabel Text="I'm a baby not robot. \nPlease give me a milk"  VerticalOptions="Center" HorizontalOptions="Center" />

Then create a custom renderer in your Droid project like below:

using Android.Widget;
using Xamarin.Forms.Platform.Android;
using Xamarin.Forms;
[assembly:ExportRenderer(typeof(LabelDemo.CustomLabel),typeof(LabelDemo.Droid.MyLabelRenderer))]
namespace LabelDemo.Droid
{
    public class MyLabelRenderer:LabelRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
        {
            base.OnElementChanged(e);
            if (Control != null)
            {
                var text = Control.Text;
                text = text.Replace("\\n", "\n");//"\n" in shared project will be automatically transformed into "\\n", so we need to change it back
                Control.SetLines(2);
                Control.SetText(text, TextView.BufferType.Normal);
            }
        }
    }
}
Elvis Xia - MSFT
  • 10,801
  • 1
  • 13
  • 24
  • I'm sorry for late reply, your suggestion is actually what I did before posting a question. It does not work, maybe because my situation is little bit different. But actually that does not guarantee cutting text automatically. My text will be shown until second line, but the problem is that the second line is not actually second line but it's from first line cut automatically. – Bright Lee Dec 14 '16 at 18:43
  • Then I guess it's probably a layout problem. On blank project, it's working correctly. Could you share some codes to show your work? – Elvis Xia - MSFT Dec 15 '16 at 01:07
  • I thought that I should. But I'm sorry about it. I guess it's layout problem like you says not like how to use custom renderer. I think I should use different approach. Thanks for you guys so far. My code is little bit complicated, I will show it if I have time. – Bright Lee Dec 16 '16 at 14:16