Is an implementation of VerticalTextAlignment for a Entry possible?

HkncoreHkncore ATMember ✭✭
edited August 2018 in Xamarin.Forms

I know that you can set the HorizontalTextAlignment for Entries, but how come there are no VerticalTextAlignment Options?

I need this:
Entry.VerticalTextAlignment = TextAlignment.Center;

This is possible, but it has no function for my application:
Entry.VerticalAlignment = LayoutOptions.Center;

Best Answers

  • HkncoreHkncore AT ✭✭
    Accepted Answer

    @ClintStLaurent said:

    Thank you for the Info, but unfortunately I am not even able to use VerticalTextAlignment

    I get that. The point I'm making is: It wouldn't do you any good even if you could because its not reliable.

    You need to either make a new control, or a custom renderer.

    Im already using a custom renderer.

    Is it possible to get the height of the entry and get the middlepoint of it. After that give that value to the text inside the entry?

Answers

  • SzbestaSzbesta Member ✭✭✭

    You could achieve this by making a CustomRendere for your Entries.
    https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/custom-renderer/entry

  • HkncoreHkncore ATMember ✭✭

    @Szbesta said:
    You could achieve this by making a CustomRendere for your Entries.
    https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/custom-renderer/entry

    I already did. The CustomRenderer has the following Code:

            if(this.Control != null) {
                Control.Gravity = GravityFlags.CenterVertical;
                TextAlignment = Android.Views.TextAlignment.Center;
                Control.TextAlignment = Android.Views.TextAlignment.Center;
            }
    

    But none of these actually fix my problem. I have to center the Text when the Entry is empty

  • SzbestaSzbesta Member ✭✭✭

    Try it with overriding the GetCellCore Method

    protected override Android.Views.View GetCellCore(Cell item, Android.Views.View convertView, ViewGroup parent, Context context)
            {
                var cell = base.GetCellCore(item, convertView, parent, context) as EntryCellView;
    
                if (cell != null)
                {
                    var textField = cell.EditText as TextView;
                    textField.TextAlignment = Android.Views.TextAlignment.Center;
                    textField.SetTextColor(Color.FromHex("#000000").ToAndroid());
                }
    
                return cell;
            }
    

    Wenns nicht läuft dann bin ich mit meinem Latein am Ende sry :/

  • SzbestaSzbesta Member ✭✭✭

    One more alternative:

    protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
    {
        base.OnElementChanged(e);
    
        if(Control != null)
        {
            Control.Gravity = GravityFlags.CenterHorizontal;
        }
    }
    
  • HkncoreHkncore ATMember ✭✭

    @Szbesta said:
    One more alternative:

    protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
    {
        base.OnElementChanged(e);
    
        if(Control != null)
        {
            Control.Gravity = GravityFlags.CenterHorizontal;
        }
    }
    

    Almost hahahahaha

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    @Hkncore said:
    I know that you can set the HorizontalTextAlignment for Entries, but how come there are no VerticalTextAlignment Options?

    Just be aware... HoriztonalTextAlignment isn't reliable. Its based on the same feature in Label. (probably a label nested inside the control but that's a guess). And Label HorizontalTextAlignmentt and VerticleTextAlignment are buggy. If you navigate off a page then back again they often times revert to Start.

  • HkncoreHkncore ATMember ✭✭

    @ClintStLaurent said:

    @Hkncore said:
    I know that you can set the HorizontalTextAlignment for Entries, but how come there are no VerticalTextAlignment Options?

    Just be aware... HoriztonalTextAlignment isn't reliable. Its based on the same feature in Label. (probably a label nested inside the control but that's a guess). And Label HorizontalTextAlignmentt and VerticleTextAlignment are buggy. If you navigate off a page then back again they often times revert to Start.

    Thank you for the Info, but unfortunately I am not even able to use VerticalTextAlignment

  • SzbestaSzbesta Member ✭✭✭

    @Hkncore said:

    @Szbesta said:
    One more alternative:

    protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
    {
        base.OnElementChanged(e);
    
        if(Control != null)
        {
            Control.Gravity = GravityFlags.CenterHorizontal;
        }
    }
    

    Almost hahahahaha

    Hahahaha whoops....
    Sorry then I'm out of usable options to go for :(

  • HkncoreHkncore ATMember ✭✭
    Accepted Answer

    @ClintStLaurent said:

    Thank you for the Info, but unfortunately I am not even able to use VerticalTextAlignment

    I get that. The point I'm making is: It wouldn't do you any good even if you could because its not reliable.

    You need to either make a new control, or a custom renderer.

    Im already using a custom renderer.

    Is it possible to get the height of the entry and get the middlepoint of it. After that give that value to the text inside the entry?

  • HkncoreHkncore ATMember ✭✭

    @Szbesta said:

    @Hkncore said:

    @Szbesta said:
    One more alternative:

    protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
    {
        base.OnElementChanged(e);
    
        if(Control != null)
        {
            Control.Gravity = GravityFlags.CenterHorizontal;
        }
    }
    

    Almost hahahahaha

    Hahahaha whoops....
    Sorry then I'm out of usable options to go for :(

    Its okay.
    Danke ^^

Sign In or Register to comment.