Xamarin.forms.Android - EntryCell Autofocus

Ruben Kannemeyer 61 Reputation points
2021-10-01T09:39:37.64+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 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!

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,296 questions
0 comments No comments
{count} votes

Accepted answer
  1. Kyle Wang 5,531 Reputation points
    2021-10-04T09:13:51.27+00:00

    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.


0 additional answers

Sort by: Most helpful