Cross Platform
Android
iOS
Mac

Working with AndroidManifest.xml

AndroidManifest.xml is a powerful file in the Android platform that allows you to describe the functionality and requirements of your application to Android. However, working with it is not easy. Xamarin.Android attempts to fix this by allowing you to add custom attributes to your classes, which will then be used to automatically generate the manifest for you. Our goal is that 99% of people will never need to manually modify the manifest file.

AndroidManifest.xml is generated as part of the build process, and the XML found within Properties\AndroidManifest.xml is merged with XML generated based on custom attributes.

The merging process is trivial: it uses the custom attributes within the code to generate XML elements, and inserts those elements into AndroidManifest.xml. Elements nested within /manifest/application are not merged, and Properties\AndroidManifest.xml always overrides data provided by custom attributes.

The Basics

At compile time, assemblies are scanned for non-abstract classes that derive from Activity and have ActivityAttribute declared on them. It then uses these to build the manifest. For example, the following code:

namespace Demo
{
    public class MyActivity : Activity
    {
    }
}

Results in nothing being generated. If you want an <activity/> element to be generated, you need to use the ActivityAttribute custom attribute:

namespace Demo
{
    [Activity]
    public class MyActivity : Activity
    {
    }
}

This results in the xml fragment:

<activity android:name="demo.MyActivity" android:label="MyActivity" />

The [Activity] attribute has no effect on abstract types; abstract types are ignored.

Activity Name

By default, the type name of an activity is created from the lowercased namespace and the class name, thus the type Demo.MyActivity becomes demo.MyActivity within AndroidManifest.xml. If you wish to specify the name of your activity, use the ActivityAttribute.Name property:

[Activity (Name="awesome.demo.activity")]
public class MyActivity : Activity
{
}

Produces:

<activity android:name="awesome.demo.activity" android:label="MyActivity" />

Note: you should only use the Name property for backward compatibility reasons, as such renaming can slow down type lookup at runtime.

Activity Title Bar

By default, Android gives your application a title bar when it is run. The value used for this is /manifest/application/activity/@android:label. Most times, this will be different than your class name. For this, use the ActivityAttribute.Label property:

[Activity (Label="Awesome Demo App")]
public class MyActivity : Activity
{
}

Produces:

<activity android:name="demo.MyActivity" android:label="Awesome Demo App" />

Launchable from Application Chooser

By default, your activity will not show up in Android's application launcher screen. This is because there will likely be many activities in your application, and you don't want an icon for every one. To specify which one should be launchable from the application launcher, use the ActivityAttribute.MainLauncher property:

[Activity (Label="Awesome Demo App", MainLauncher=true)] 
public class MyActivity : Activity
{
}

Produces:

<activity android:name="demo.MyActivity" android:label="Awesome Demo App">
  <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>
</activity>

Activity Icon

By default, your activity will be given the default launcher icon provided by the system. To use a custom icon, first add your .png to Resources\drawable, set its Build Action to AndroidResource, then use the ActivityAttribute.Icon property to specify the icon to use:

[Activity (Label="Awesome Demo App", MainLauncher=true, Icon="@drawable/myicon")] 
public class MyActivity : Activity
{
}

Produces:

<activity android:name="demo.MyActivity" android:label="Awesome Demo App"
      android:icon="@drawable/myicon">
  <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>
</activity>

Advanced Features

Intent Actions and Features

The Android manifest provides a way for you to describe the capabilities of your activity. This is done via Intents and the IntentFilterAttribute custom attribute. You can specify which actions are appropriate for your activity with the IntentFilterAttribute constructor, and which categories are appropriate with the IntentFilterAttribute.Categories property. At least one activity must be provided (which is why activities are provided in the constructor). [IntentFilter] can be provided multiple times, and each use results in a separate <intent-filter/> element within the <activity/>.

[Activity (Label="Awesome Demo App", MainLauncher=true, Icon="@drawable/myicon")] 
[IntentFilter (new[]{Intent.ActionView}, 
        Categories=new[]{Intent.CategorySampleCode, "my.custom.category"})]
public class MyActivity : Activity
{
}

Produces:

<activity android:name="demo.MyActivity" android:label="Awesome Demo App"
      android:icon="@drawable/myicon">
  <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>
  <intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.SAMPLE_CODE" />
    <category android:name="my.custom.category" />
  </intent-filter>
</activity>

List of Custom Attributes