0

Another tough day with Xamarin!

So, today, i am trying to figure out how one can create and use a custom control in Xamarin.Android. I read all the docs and all were on Xamarin.Forms. I wonder why there is so less support for Xamarin.Android.

Anyways, i came across a few questions on SO and tried this :

TestControl.cs

namespace TestApp.CustomControls.Classes
{
public class TestControl : ViewGroup
{
    protected override void OnLayout(bool changed, int l, int t, int r, int b)
    {
        throw new NotImplementedException();
    }
    public TestControl(Context context, IAttributeSet attrs)
        : base(context, attrs)
    {
    }
}
}

TestControl.xml

<TestApp.CustomControls.Classes.TestControl xmlns:android="http://schemas.android.com/apk/res/android"
                                                 android:layout_height="20dp" android:layout_width="20dp"
                                                 android:background="#fff">

In my activity_main.axml file, i have the following:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" 
android:layout_height="50dp"
android:layout_weight="50dp">

<TestApp.CustomControls.Classes.TestControl android:layout_height="50dp"
android:layout_weight="50dp"/>
</RelativeLayout>

Even though intellisense is able to find TestApp.CustomControls.Classes.TestControl but i don't see anything on the screen. Maybe because i never linked the xml file with the class?(Still now sure how xamarin.android works).

So, what should be my steps to get the custom control working/visible on view?

11
  • stackoverflow.com/questions/21682010/… Commented Apr 8, 2019 at 0:46
  • It is a ViewGroup subclass, thus is a container for other Views and/or to describe layout parameters of its children, what do you expect to see when it does does not contain any children? Commented Apr 8, 2019 at 0:51
  • @SushiHangover, umm, i tried adding a <LinearLayout> to the Xml file but didn't result in anything.... Maybe i am missing a lot here. Any guidance? Commented Apr 8, 2019 at 0:55
  • @Jason, i've already seen that question. So, where is the Tile.cs ? How does it actually look like? And the way his, Tile.xml looked....well, how is he supposed to connect the control's code-behind with the xml? Commented Apr 8, 2019 at 0:57
  • @zackraiyan A LinearLayout is a ViewGroup subclass and again is a container for other controls that provides an vert/horz. layout of its children based upon its orientation property (developer.android.com/reference/android/widget/LinearLayout). What are you trying to accomplish with your "custom control"? Commented Apr 8, 2019 at 1:00

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.