question

RubenKannemeyer-8831 avatar image
0 Votes"
RubenKannemeyer-8831 asked RubenKannemeyer-8831 commented

Xamarin.forms.Android - EntryCell Autofocus

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 EntryCell field which takes the value of a barcode. Entrycell has an option to do keyboard.hide which is perfect for my application but I need the field to set autofocus which is not an option for EntryCell. How would I set autofocus to an EntryCell?

Please explain in the simplest way possible. Many thanks!

dotnet-xamarinformsdotnet-android
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

KyleWang-MSFT avatar image
0 Votes"
KyleWang-MSFT answered RubenKannemeyer-8831 commented

Hi RubenKannemeyer-8831,

Welcome to our Microsoft Q&A platform!

We can't make the EnrtyCell focused directly. Here is a workaround that creating a custom EntryCell you can refer to.
MyEntryCell.cs

 class MyEntryCell : ViewCell
 {
     public Label label
     {
         get;
         set;
     }
    
     public Entry entry
     {
         get;
         set;
     }
    
     public MyEntryCell()
     {
         label = new Label
         {
             Text = "123",
             VerticalOptions = LayoutOptions.Center
         };
         entry = new Entry
         {
             HorizontalOptions = LayoutOptions.Fill
         };
    
         Grid grid = new Grid
         {
             ColumnDefinitions =
             {
                 new ColumnDefinition { Width = GridLength.Auto },
                 new ColumnDefinition { Width = GridLength.Star },
             }
         };
         grid.Children.Add(label, 0, 0);
         grid.Children.Add(entry, 1, 0);
    
         View = grid;
     }
 }

MainPage.xaml.cs

 public partial class MainPage : ContentPage
 {
     protected async override void OnAppearing()
     {
         base.OnAppearing();
         await Task.Delay(600);
         myEntryCell.entry.Focus();
     }
     MyEntryCell myEntryCell;
    
     public MainPage()
     {
         InitializeComponent();
    
         myEntryCell = new MyEntryCell();
         myEntryCell.label.Text = "Title";
    
         TableView tableView = new TableView
         {
             Intent = TableIntent.Form,
             Root = new TableRoot
             {
                 new TableSection
                 {
                     myEntryCell
                 }
             }
         };
         // ...
     }
 }

Regards,
Kyle


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.

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks Kyle!

Does this code "declare" the EntryCell already or do I need to do that in the xaml? As it currently stands no entrycell gets created on my page from your code.

Sorry if I'm missing something simple,

Thanks!

0 Votes 0 ·
KyleWang-MSFT avatar image KyleWang-MSFT RubenKannemeyer-8831 ·

@RubenKannemeyer-8831 You also need to set Content for the page.

 this.Content = new StackLayout
 {
     Children =
     {
         tableView,
     }
 };
1 Vote 1 ·

This works perfectly thank you!

I do have a slight problem though. By having the custom render to set autofocus, you break the EntryCell into a Label and Entry box. However Entry box does not have the ability to hidekeyboard as the EntryCell does. Is it possible to keep the property of hidekeyboard for EntryCell while still being able to set focus?

0 Votes 0 ·