2

I used the Custom render to sort the text but it does not work on some devices and throws this error:

Java.Lang.NoSuchMethodError: 'no non-static method "Landroid/widget/TextView;.setJustificationMode(I)V"'

Thanks if anyone helps.

This is my code:



[assembly: ExportRenderer(typeof(CustomLabel), typeof(CustomLabelRender))]


namespace customlabel.Droid


{


    public class CustomLabelRender : LabelRenderer


    {
        public CustomLabelRender(Context context) : base(context)
        {

        }


        protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
        {
            base.OnElementChanged(e);
            if (Control != null)
            {
                Control.JustificationMode = JustificationMode.InterWord;
            }
        }


    }


}
ho h
  • 255
  • 1
  • 11
  • Hi, what is **Target Framework** for android used in your project? https://i.stack.imgur.com/ekQZR.png – Junior Jiang Dec 03 '20 at 03:29
  • Thanks for your Answer, my Target Framework is 11. But when I try it on Android 7, my application crashes! – ho h Dec 05 '20 at 11:36
  • Okey, if so, pinedax's answer should works for you. Could you explain more why that not works for you? – Junior Jiang Dec 07 '20 at 01:48
  • I do not know, when I use pinedax's code, my app crashes again. – ho h Dec 07 '20 at 13:22
  • Hi, I have tried with pinedax's code, it works for me on Android 7, and build with Target Framework 11. If have time, you could share a new created sample project link here. I will check where problem is. – Junior Jiang Dec 08 '20 at 02:50
  • Hi, Thanks for Answer ! For me, it doesn't crash in Android 7 anymore, but the end of the text is messy again. I am using a content page that has an image and a label. Do I have to change the page type? Where do you think the problem comes from? – ho h Dec 08 '20 at 12:01
  • Glad it works! You could share the code of Xaml, I will check the layout in my local site. And not forget to add the sample text of Label which can reproduce this problem. – Junior Jiang Dec 09 '20 at 01:17
  • Because this case has been solved, you could create a new question in SO. And share the case link here let me know. – Junior Jiang Dec 09 '20 at 01:31
  • As I said, only my app did not crash on Android 7. But the problem was not completely solved for me, because the ends of my texts in Android 7 and belower are still disordered.@ Junior Jiang - MSFT – ho h Dec 09 '20 at 09:10
  • Okey, but it's not easy to achieve that below andorid 8.0. You will need to read android source code to understand the word inside textview how to typeset. Then you will calculate the width of each word then can set teh spcace between them. If you are insist on doing this, you could have a look at [this discussion](https://stackoverflow.com/a/17807121/10504796). – Junior Jiang Dec 10 '20 at 03:16
  • 1
    Thanks for your tips! @ Junior Jiang - MSFT – ho h Dec 10 '20 at 09:06

2 Answers2

1

The error is because this method was first introduced on Android on API Version 26 so that means that on devices with older versions of Android it will crash.

More info here: https://developer.android.com/reference/android/widget/TextView#setJustificationMode(int)

You can still use it but you will need to validate the OS Version before calling it. If you are already using Xamarin.Essentials (I guess you are) you can do something like this:

//Version 8.0 is API 26 (https://source.android.com/setup/start/build-numbers)

if (Xamarin.Essentials.DeviceInfo.Version.Major >= 8)
{
    if (Control != null)
    {
        Control.JustificationMode = JustificationMode.InterWord;
    }
}

Note: The validation will only prevent the crash but that means you will not be supporting devices with <8.0 (26) OS.

Hope this helps.-

pinedax
  • 9,246
  • 2
  • 23
  • 30
  • It did not work for me. Is there another way I can organize my texts to work on all versions? – ho h Dec 02 '20 at 09:36
1

I have tried with following code and it works in my local site:

public class CustomLabelRenderer: LabelRenderer
{
    public CustomLabelRenderer(Context context) : base(context)
    {

    }


    protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
    {
        base.OnElementChanged(e);

        if (Xamarin.Essentials.DeviceInfo.Version.Major >= 8)
        {
            if (Control != null)
            {
                Control.SetBackgroundColor(Android.Graphics.Color.AliceBlue);
                Control.JustificationMode = JustificationMode.InterWord;
            }
        }
        else
        {
            if (Control != null)
            {
                Control.SetBackgroundColor(Android.Graphics.Color.Red);
                Control.TextAlignment = Android.Views.TextAlignment.Center;
            }
        }
    }
}

The effect on Android 9.0 device:

enter image description here

The effect on Android 7.0 device:

enter image description here

Junior Jiang
  • 12,430
  • 1
  • 10
  • 30