Xamarin.forms.Android - Hide softkeyboard

Ruben Kannemeyer 61 Reputation points
2021-09-29T15:30:04.303+00:00

Hi there, I am a new to Xamarin.forms and I am struggling, please help. This is for ANDROID.

My app is used for stocktake and the device that the app is on is a normal phone that has a scanner at the back. (Sunmi L2k). The app scans a barcode and displays relevant information of product right below it.

On my app I have an Entry field to which autofocus is set as soon as the page opens (So that the user just needs to click the scan button and nothing else) and the field automatically populates with the barcode. Because of the autofocus the softkeyboard pops up which covers the product information. So the user clicks away from the keyboard to view the information but that loses focus on the Entry field and then they have to reclick on the entry field to scan the next item instead of just scanning straight away. How do I prevent the keyboard from displaying completely so that the user does not need to click on the screen anywhere.

Or alternatively, which ever option is easier, I replace the Entry field with a EntryCell option which does have the option to hide the keyboard, but does not have an autofocus function, so the user will still have to touch the screen on the field to gain focus.

Please explain in the simplest way possible. Many thanks!

Developer technologies | .NET | Xamarin
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2021-09-30T06:07:41.75+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    You can create a Custom renderer to hide the keyboard when Entry get the focus.

    Firstly, Create an class to extend the Entryin shared code.

       public class MyEntryHideKeyboard:Entry  
           {  
         
           }  
    

    Then, Implement the custom renderer in the android platform.

       using Xamarin.Forms;  
       using Xamarin.Forms.Platform.Android;  
         
       [assembly: ExportRenderer(typeof(MyEntryHideKeyboard), typeof(MyEntryRenderer))]  
       namespace App141.Droid  
       {  
           class MyEntryRenderer : EntryRenderer  
           {  
               public MyEntryRenderer(Context context) : base(context)  
               {  
               }  
         
               protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)  
               {  
                   base.OnElementChanged(e);  
         
                   if (Control != null)  
                   {  
                   
                       EditText editText= Control as EditText;  
         
                       editText.InputType = Android.Text.InputTypes.Null;  
                   }  
               }  
           }  
       }  
    

    In the end, we can use it in the xml.

       <app141:MyEntryHideKeyboard  x:Name="MyEntry"></app141:MyEntryHideKeyboard>  
    

    When you use code to get focus in the OnAppearing method. This Keybord will not show, but you focus still exist.

       public partial class MainPage : ContentPage  
           {  
               public MainPage()  
               {  
                   InitializeComponent();  
               }  
               protected override void OnAppearing()  
               {  
                   base.OnAppearing();  
                
                   MyEntry.Focus();  
               }  
           }  
    

    Best Regards,

    Leon Lu


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    2 people found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.