Forum Xamarin.Android

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

How to full justify textview in xamarin android?

BayuBayu Member ✭✭✭

I make long text inside the textview.

<TextView
android:text="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\nadgsjhgksdhgkdhgkhgkjghjsdhkjsgdhal asghds agdhjas sdaghjak sagdl adgakjlsg alsjahgkjas ajsdghaugaj asdjgha akglajdsahg k
android:textSize="14sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:justificationMode="inter_word"
android:id="@+id/textView1" />

but I get not full jutify, like this

how to resolve it? thanks

Best Answer

Answers

  • jezhjezh Member, Xamarin Team Xamurai

    What's the api version of your android device?

    TextView in Android O offers full justification (new typographic alignment) itself.

    You just need to do this:

         if (Build.VERSION.SdkInt >= Build.VERSION_CODES.O)
            {
                textView.JustificationMode = Android.Text.JustificationMode.InterWord;
            }
    

    When I tested on Huawei mate30(android 10), the result is:

  • BayuBayu Member ✭✭✭

    Thank you @jezh , I use API 22 Android lollipop 5.1.

    where I write this code? thanks

    @jezh said:
    What's the api version of your android device?

    TextView in Android O offers full justification (new typographic alignment) itself.

    You just need to do this:

         if (Build.VERSION.SdkInt >= Build.VERSION_CODES.O)
            {
                textView.JustificationMode = Android.Text.JustificationMode.InterWord;
            }
    

    When I tested on Huawei mate30(android 10), the result is:

  • jezhjezh Member, Xamarin Team Xamurai

    You can refer to the following code in your activity:

       public class MainActivity : AppCompatActivity
        {
            TextView textView;
            protected override void OnCreate(Bundle savedInstanceState)
            {
                base.OnCreate(savedInstanceState);
                Xamarin.Essentials.Platform.Init(this, savedInstanceState);
                // Set our view from the "main" layout resource
                SetContentView(Resource.Layout.activity_main);
    
    
                textView = FindViewById<TextView>(Resource.Id.textView1);
    
                if (Build.VERSION.SdkInt >= Build.VERSION_CODES.O)
                {
                    textView.JustificationMode = Android.Text.JustificationMode.InterWord;
                }
            }
    

    Note: the textView1 is the Id of your TextView.

    android:id="@+id/textView1"
    
  • BayuBayu Member ✭✭✭

    Thank you, I apply the code and get the 3 warning like this

    when I run, get this:

    how about it? @jezh

  • jezhjezh Member, Xamarin Team Xamurai
    edited April 2020

    1.The first warning
    I couldn't see the detail of your layout, could you please post the code of your layout?

    2.The second warning
    Just add a string in file strings.xml of folder Resources ->values->strings.xml .For example:

    <string name="teststr">aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\nadgsjhgksdhgkdhgkhgkjghjsdhkjsgdhal asghds agdhjas sdaghjak sagdl adgakjlsg alsjahgkjas ajsdghaugaj asdjgha akglajdsahg k</string>
    

    And use it in layout like this:

    android:text="@string/teststr"
    

    3.Just as the tip mentioned, `Build.VERSION_CODES.O' is obsolete, we should use code

     if (Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.O){   }
    

    Not

      if (Build.VERSION.SdkInt >= Build.VERSION_CODES.O){  }
    

    Note: I added code if (Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.O){ } just to indicate that the property android:justificationMode is intended to be valid beyond android version 8.0(api 26).

  • BayuBayu Member ✭✭✭
    1. this is my full layout code

      <?xml version="1.0" encoding="utf-8"?>
      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent">
          <ScrollView
              android:minWidth="25px"
              android:minHeight="25px"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:id="@+id/scrollView1" >
              <TextView
                  android:text="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\nadgsjhgksdhgkdhgkhgkjghjsdhkjsgdhal asghds agdhjas sdaghjak sagdl adgakjlsg alsjahgkjas ajsdghaugaj asdjgha akglajdsahg k"
                  android:textSize="14sp"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_margin="10dp"
                  android:justificationMode="inter_word"
                  android:id="@+id/textView1" />
      
          </ScrollView>
      
      </RelativeLayout>
      
    2. & 3 okay thank you @jezh

  • jezhjezh Member, Xamarin Team Xamurai
    edited April 2020

    Just remove ScrollView element,so the code will like this:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
            <TextView
                android:text="@string/teststr"
                android:textSize="14sp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:justificationMode="inter_word"
                android:id="@+id/textView1" />
    </RelativeLayout>
    
  • BayuBayu Member ✭✭✭

    thank you @jezh, I apply all the code and still get

  • jezhjezh Member, Xamarin Team Xamurai

    What's the version of your andrid emulator?

  • BayuBayu Member ✭✭✭

    This is my android emulator version @jezh

  • BayuBayu Member ✭✭✭

    ow ya ya, now I see, thank you for the discussion and for all the help @jezh. Nice discussion.

  • jezhjezh Member, Xamarin Team Xamurai

    It's my pleasure to help you. Happy coding... :)

Sign In or Register to comment.