xamarin / Xamarin.Forms

Xamarin.Forms is no longer supported. Migrate your apps to .NET MAUI.
https://aka.ms/xamarin-upgrade
Other
5.63k stars 1.88k forks source link

[Bug] [Android] Unable to focus custom EditText in Xamarin ListView #13455

Open MuneeshKumarG opened 3 years ago

MuneeshKumarG commented 3 years ago

Description

I have written custom view in Xamarin. And implemented the view design in renderer with EditText.

When I try to focus the EditText in ListView ItemTemplate, Keyboard not showing in android platform. In outside the ListView it works fine.

Code snippet

Mainpage.xaml.cs

public partial class MainPage : ContentPage
    {
        public string[] Items { get; set; }
        public MainPage()
        {
            InitializeComponent();

            Items = new string[] { "Item1" };
            BindingContext = this;
        }
    }

    public class CustomizedView : View
    {

    }

MainPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:CustomView"
             x:Class="CustomView.MainPage">

    <StackLayout>
        <local:CustomizedView/>

        <ListView ItemsSource="{Binding Items}" RowHeight="200" >
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Grid>
                            <local:CustomizedView/>
                        </Grid>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>

</ContentPage>

CustomRenderer.cs

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using CustomView;
using CustomView.Droid;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;

[assembly: ExportRenderer(typeof(CustomizedView), typeof(CustomViewRenderer))]

namespace CustomView.Droid
{
    public class CustomViewRenderer : ViewRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.View> e)
        {
            base.OnElementChanged(e);

            if(e.NewElement != null)
            {
                FrameLayout frameLayout = new FrameLayout(Forms.Context);

                EditText editText = new EditText(Context);
                editText.Text = "Text";
                editText.SetPadding(10, 10, 10, 10);

                frameLayout.AddView(editText);

                SetNativeControl(frameLayout);
            }
        }
    }
}
PureWeen commented 3 years ago

@MuneeshKumarG just an FYI that this issue probably won't get addressed

Because it's a custom renderer and I tested this with CollectionView and it works fine there. I'll put this one as up for grabs if someone from the community wants to take it