Using custom view in AXML

ThomasNasbyThomasNasby USMember ✭✭
edited November 2 in Xamarin.Android

I'm trying to create a custom keyboard, and I'm using this GitHub project to help me. I'm getting stuck on the part where I reference the custom keyboard.

This line in MainActivity is returning null:
mKeyboardView = (CustomKeyboardView)FindViewById(Resource.Id.keyboard_view);

It looks like I'm not referencing my CustomKeyBoardView item correctly, as I'm told it's an invalid child element. Here is the full AXML.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:layout_width="fill_parent"
        android:id="@+id/container"
        android:layout_alignParentTop="true"
        android:layout_height="fill_parent"
        android:layout_above="@+id/keyboard_view">
        <EditText
            android:layout_width="fill_parent"
            android:id="@+id/target"
            android:layout_height="wrap_content" />
    </LinearLayout>
    <todo.droid.CustomKeyboardView     ERROR: invalid child element "todo.droid.CustomKeyboardView"
        android:id="@+id/keyboard_view"
        android:visibility="gone"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" />
</RelativeLayout>

And here is my CustomKeyboardView.cs file:

using System;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Views.Animations;
using Android.InputMethodServices;
using Android.Util;

namespace Todo.Droid
{

    [Register("todo.droid.CustomKeyboardView")]
    public class CustomKeyboardView : KeyboardView
    {
        public CustomKeyboardView(Context context, IAttributeSet attrs) : base(context, attrs)
        {
        }

        public void ShowWithAnimation(Animation animation)
        {
            animation.AnimationEnd += (sender, e) => {
                Console.WriteLine("Set visibility!");
                Visibility = ViewStates.Visible;
            };

            Animation = animation;
        }
    }
}

Am I missing something? Every guide on using custom components just says I need to put the namespace in lowercase, which it currently is.

Tagged:

Answers

  • SteveVSteveV USMember ✭✭

    I'm pretty sure you need the actual fully qualified class name not the lowercase namespace version. Have you tried using <Todo.Droid.CustomKeyboardView in your layout and if so, what errors are you seeing?

    When using the lowercase namespace version does the designer show any errors when you open the axml layout? And for that matter, is the keyboard properly displayed if you change the visibility property from:

    android:visibility="gone"

    to

    android:visibility="visible"

    if the designer looks ok, does this return a view?

    var kb = FindViewById(Resource.Id.keyboard_view);

    And if so, what is the type of kb?

  • ThomasNasbyThomasNasby USMember ✭✭

    @SteveV

    Have you tried using <Todo.Droid.CustomKeyboardView in your layout and if so, what errors are you seeing?

    Same error

    is the keyboard properly displayed if you change the visibility property from gone to visible

    Nope, doesn't show if it's upper or lower case

    And if so, what is the type of kb?

    Still null

  • SteveVSteveV USMember ✭✭

    Weird. Can you instantiate an instance of CustomKeyboardView and add it to your RelativeLayout programmatically? While not a fix it might give you some insight as to whether it's a layout issue or an issue with the KeyboardView implementation.

Sign In or Register to comment.