Search code examples
xamarin.androidxamarin.formsandroid-linearlayout

xamarin.forms android - linear layout not presented as expected


I have this code for custom keyboard.

Its xamarin.forms for android.

I want the keyboard to be presented from bottom of the page and to raise the Entry if needed.

the result is that the keyboard is presented on top of the page and covers the entry if the entry is on the top.

~

public class CustomEntryRenderer : EntryRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
    {
        base.OnElementChanged(e);

        if (Control == null)
            return;

        Android.InputMethodServices.Keyboard numericKeyboard = new Android.InputMethodServices.Keyboard(Control.Context, Resource.Xml.keyboard2);
        CustomKeyboardView numericKeyboardView = new CustomKeyboardView(Control.Context, null);
        numericKeyboardView.Id = Control.Id;
        numericKeyboardView.Keyboard = numericKeyboard;
        numericKeyboardView.Visibility = ViewStates.Gone;
        numericKeyboardView.PreviewEnabled = false;

        /////////////////////////////////////////////////
        // THIS IS THE LAYOUT CREATION
        ////////////////////////////////////////////////
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent); // maybe WrapContent on all
        lp.Gravity = GravityFlags.Bottom;
        lp.BottomMargin = 0;

        /////////////////////////////////////////////////

        Activity activity = this.Context as Activity;

        activity.AddContentView(numericKeyboardView, lp);

        Control.Touch += (sender, ex2) => 
        {
            if (numericKeyboardView.Visibility == ViewStates.Gone)
            {
                //Xamarin.Forms.Animation animation = Android.Views.Animations.AnimationUtils.LoadAnimation(
                Android.Views.Animations.Animation animation = Android.Views.Animations.AnimationUtils.LoadAnimation(
                this.Context,
                    Resource.Animation.slide_in_bottom
                );
                numericKeyboardView.ShowWithAnimation(animation);
                numericKeyboardView.Visibility = ViewStates.Visible;
            }

            ex2.Handled = true;
        };                       
    }
} 

~


Solution

  • I want the keyboard to be presented from bottom of the page and to raise the Entry if needed.

    You need to wrap your numericKeyboardView with a RelativeLayout and set the Rule of AlignParentBottom.

    In OnElementChanged method, add the following codes:

    //Create RelativeLayout for Keyboard
    Android.Widget.RelativeLayout rl = new Android.Widget.RelativeLayout(this.Context);
    //Create the LayoutParams for Keyboard
    Android.Widget.RelativeLayout.LayoutParams rlp = new Android.Widget.RelativeLayout.LayoutParams(LayoutParams.FillParent, LayoutParams.WrapContent);
    //set the AlignParentBottom rule
    rlp.AddRule(LayoutRules.AlignParentBottom);
    //set the LayoutParams to Keyboard
    numericKeyboardView.LayoutParameters = rlp;
    //add the keyboard to RelativeLayout
    rl.AddView(numericKeyboardView);
    //get current activity
    Activity activity = this.Context as Activity;
    //Add the Layout View to activity
    activity.AddContentView(rl,rlp);