2

I'm porting my app from Windows Phone 8 to Android - and I need to create a few custom UI controls. I tried to create an XML layout, creating a LinearLayout inside it as the control and then add it dynamically (After user's desire) - but that didn't work. How can I accomplish this the easiest way as possible?

This is the code i tried:

MainActivity.cs:

using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

namespace Knowledge_Organizer
{
[Activity (Label = "Knowledge_Organizer", MainLauncher = true)]
public class MainActivity : Activity
{

    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);

        // Set our view from the "main" layout resource
        SetContentView (Resource.Layout.Main);

        // Import the items from resources
        LinearLayout layout = FindViewById<LinearLayout> (Resource.Id.rootLayout);

        // Add the tile
        Resource.Layout.Tile tileRoot = FindViewById<LinearLayout> (Resource.Layout.Tile);
        var tile = (Resource.Id.projectTile);
        layout.AddView (tile);
    }
}
}

Main layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minWidth="25px"
android:minHeight="25px"
android:id="@+id/rootLayout" />

Tile:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
    android:orientation="horizontal"
    android:minWidth="25px"
    android:minHeight="25px"
    android:layout_width="290dp"
    android:layout_height="120dp"
    android:id="@+id/linearLayout1"
    android:background="#ff2e4653">
    <TextView
        android:text="Project"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:id="@+id/projectTile"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginRight="0.0dp" />
</LinearLayout>
</LinearLayout>

Thanks a lot in advance!

Kind Regards, Erik

1
  • So, how does the Tile.cs fill look like? Commented Apr 8, 2019 at 1:03

1 Answer 1

4

You need to use a LayoutInflater, e.g.

var tile = LayoutInflater.Inflate(Resource.Layout.my_tile_xml, null);
layout.AddView (tile);

To find a control within the tile:

var tile = LayoutInflater.Inflate(Resource.Layout.my_tile_xml, null);
tile.FindViewById<TextView>(Resource.Id.projectTile).Text = "whatever";
layout.AddView (tile);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, it worked - but how can I modify one specific control within the tile? Keep in mind the tile will be added dynamically after the user's desire
I've updated the answer with an example of how to set the text. If the answer worked for you, you should mark it as correct. That's how this site works.

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.