Skip to content

Commit 461cc83

Browse files
jonpryordellis1972
authored andcommitted
[samples/HelloWorld] Add Hello World sample. (#19)
Update README.md to describe how to build, install, and run the sample.
1 parent b4e31a0 commit 461cc83

File tree

16 files changed

+361
-4
lines changed

16 files changed

+361
-4
lines changed

README.md

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,17 +178,37 @@ For example, to generate `Mono.Android.dll` for API-19 (Android 4.4):
178178
xbuild /p:AndroidApiLevel=19 /p:AndroidFrameworkVersion=v4.4
179179
# creates bin\Debug\lib\xbuild-frameworks\MonoAndroid\v4.4\Mono.Android.dll
180180

181-
## Contributing ##
181+
# Samples
182182

183-
### Mailing Lists
183+
The [HelloWorld](samples/HelloWorld) sample may be built with the
184+
[xabuild](tools/scripts/xabuild) script:
185+
186+
$ tools/scripts/xabuild /t:SignAndroidPackage samples/HelloWorld/HelloWorld.csproj
187+
188+
`xabuild /t:SignAndroidPackage` will generate an `.apk` file, which may be
189+
installed onto an Android device with the [`adb install`][adb-commands]
190+
command:
191+
192+
[adb-commands]: http://developer.android.com/tools/help/adb.html#commandsummary
193+
194+
$ adb install samples/HelloWorld/bin/Debug/com.xamarin.android.helloworld-Signed.apk
195+
196+
**HelloWorld** may be launched manually through the Android app launcher,
197+
or via `adb shell am`:
198+
199+
$ adb shell am start com.xamarin.android.helloworld/example.MainActivity
200+
201+
# Contributing
202+
203+
## Mailing Lists
184204

185205
To discuss this project, and participate in the design, we use the [[email protected]](http://lists.xamarin.com/mailman/listinfo/android-devel) mailing list.
186206

187-
### Coding Guidelines
207+
## Coding Guidelines
188208

189209
We use [Mono's Coding Guidelines](http://www.mono-project.com/community/contributing/coding-guidelines/).
190210

191-
### Reporting Bugs
211+
## Reporting Bugs
192212

193213
We use [Bugzilla](https://bugzilla.xamarin.com/enter_bug.cgi?product=Android) to track issues.
194214

Xamarin.Android.sln

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ Project("{9344BDBB-3E7F-41FC-A0DD-8665D75EE146}") = "monodroid", "src\monodroid\
5353
EndProject
5454
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "api-xml-adjuster", "tools\api-xml-adjuster\api-xml-adjuster.csproj", "{8A6CB07C-E493-4A4F-AB94-038645A27118}"
5555
EndProject
56+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Samples", "Samples", "{99CBCF85-E4EB-41C8-9089-1A2B740681F0}"
57+
EndProject
58+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HelloWorld", "samples\HelloWorld\HelloWorld.csproj", "{2305B00D-DE81-4744-B0DA-357835CAFE5A}"
59+
EndProject
5660
Global
5761
GlobalSection(SolutionConfigurationPlatforms) = preSolution
5862
Debug|AnyCPU = Debug|AnyCPU
@@ -194,6 +198,18 @@ Global
194198
{E8492EFB-D14A-4F32-AA28-88848322ECEA}.XAIntegrationDebug|Any CPU.Build.0 = Debug|Any CPU
195199
{E8492EFB-D14A-4F32-AA28-88848322ECEA}.XAIntegrationRelease|Any CPU.ActiveCfg = Debug|Any CPU
196200
{E8492EFB-D14A-4F32-AA28-88848322ECEA}.XAIntegrationRelease|Any CPU.Build.0 = Debug|Any CPU
201+
{2305B00D-DE81-4744-B0DA-357835CAFE5A}.Debug|AnyCPU.ActiveCfg = Debug|Any CPU
202+
{2305B00D-DE81-4744-B0DA-357835CAFE5A}.Debug|AnyCPU.Build.0 = Debug|Any CPU
203+
{2305B00D-DE81-4744-B0DA-357835CAFE5A}.Release|AnyCPU.ActiveCfg = Release|Any CPU
204+
{2305B00D-DE81-4744-B0DA-357835CAFE5A}.Release|AnyCPU.Build.0 = Release|Any CPU
205+
{2305B00D-DE81-4744-B0DA-357835CAFE5A}.XAIntegrationDebug|Any CPU.ActiveCfg = Debug|Any CPU
206+
{2305B00D-DE81-4744-B0DA-357835CAFE5A}.XAIntegrationDebug|Any CPU.Build.0 = Debug|Any CPU
207+
{2305B00D-DE81-4744-B0DA-357835CAFE5A}.XAIntegrationRelease|Any CPU.ActiveCfg = Debug|Any CPU
208+
{2305B00D-DE81-4744-B0DA-357835CAFE5A}.XAIntegrationRelease|Any CPU.Build.0 = Debug|Any CPU
209+
{2305B00D-DE81-4744-B0DA-357835CAFE5A}.XAIntegrationDebug|AnyCPU.ActiveCfg = Debug|Any CPU
210+
{2305B00D-DE81-4744-B0DA-357835CAFE5A}.XAIntegrationDebug|AnyCPU.Build.0 = Debug|Any CPU
211+
{2305B00D-DE81-4744-B0DA-357835CAFE5A}.XAIntegrationRelease|AnyCPU.ActiveCfg = Debug|Any CPU
212+
{2305B00D-DE81-4744-B0DA-357835CAFE5A}.XAIntegrationRelease|AnyCPU.Build.0 = Debug|Any CPU
197213
EndGlobalSection
198214
GlobalSection(NestedProjects) = preSolution
199215
{8FF78EB6-6FC8-46A7-8A15-EBBA9045C5FA} = {E351F97D-EA4F-4E7F-AAA0-8EBB1F2A4A62}
@@ -219,6 +235,7 @@ Global
219235
{D48EE8D0-0A0A-4493-AEF5-DAF5F8CF86AD} = {864062D3-A415-4A6F-9324-5820237BA058}
220236
{53EE4C57-1C03-405A-8243-8DA539546C88} = {04E3E11E-B47D-4599-8AFC-50515A95E715}
221237
{8A6CB07C-E493-4A4F-AB94-038645A27118} = {864062D3-A415-4A6F-9324-5820237BA058}
238+
{2305B00D-DE81-4744-B0DA-357835CAFE5A} = {99CBCF85-E4EB-41C8-9089-1A2B740681F0}
222239
EndGlobalSection
223240
GlobalSection(MonoDevelopProperties) = preSolution
224241
Policies = $0
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Any raw assets you want to be deployed with your application can be placed in
2+
this directory (and child directories) and given a Build Action of "AndroidAsset".
3+
4+
These files will be deployed with your package and will be accessible using Android's
5+
AssetManager, like this:
6+
7+
public class ReadAsset : Activity
8+
{
9+
protected override void OnCreate (Bundle bundle)
10+
{
11+
base.OnCreate (bundle);
12+
13+
InputStream input = Assets.Open ("my_asset.txt");
14+
}
15+
}
16+
17+
Additionally, some Android functions will automatically load asset files:
18+
19+
Typeface tf = Typeface.CreateFromAsset (Context.Assets, "fonts/samplefont.ttf");

samples/HelloWorld/HelloWorld.csproj

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
6+
<ProjectGuid>{2305B00D-DE81-4744-B0DA-357835CAFE5A}</ProjectGuid>
7+
<ProjectTypeGuids>{EFBA0AD7-5A72-4C68-AF49-83D382785DCF};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
8+
<OutputType>Library</OutputType>
9+
<RootNamespace>HelloWorld</RootNamespace>
10+
<AssemblyName>HelloWorld</AssemblyName>
11+
<TargetFrameworkVersion>v5.0</TargetFrameworkVersion>
12+
<AndroidApplication>True</AndroidApplication>
13+
<AndroidResgenFile>Resources\Resource.designer.cs</AndroidResgenFile>
14+
<AndroidResgenClass>Resource</AndroidResgenClass>
15+
<AndroidManifest>Properties\AndroidManifest.xml</AndroidManifest>
16+
<MonoAndroidResourcePrefix>Resources</MonoAndroidResourcePrefix>
17+
<MonoAndroidAssetsPrefix>Assets</MonoAndroidAssetsPrefix>
18+
<AndroidUseLatestPlatformSdk>true</AndroidUseLatestPlatformSdk>
19+
</PropertyGroup>
20+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
21+
<DebugSymbols>true</DebugSymbols>
22+
<DebugType>full</DebugType>
23+
<Optimize>false</Optimize>
24+
<OutputPath>bin\Debug</OutputPath>
25+
<DefineConstants>DEBUG;</DefineConstants>
26+
<ErrorReport>prompt</ErrorReport>
27+
<WarningLevel>4</WarningLevel>
28+
<ConsolePause>false</ConsolePause>
29+
<AndroidLinkMode>None</AndroidLinkMode>
30+
</PropertyGroup>
31+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
32+
<Optimize>true</Optimize>
33+
<OutputPath>bin\Release</OutputPath>
34+
<ErrorReport>prompt</ErrorReport>
35+
<WarningLevel>4</WarningLevel>
36+
<ConsolePause>false</ConsolePause>
37+
<AndroidUseSharedRuntime>false</AndroidUseSharedRuntime>
38+
</PropertyGroup>
39+
<ItemGroup>
40+
<Reference Include="System" />
41+
<Reference Include="System.Xml" />
42+
<Reference Include="System.Core" />
43+
<Reference Include="Mono.Android" />
44+
</ItemGroup>
45+
<ItemGroup>
46+
<Compile Include="MainActivity.cs" />
47+
<Compile Include="Resources\Resource.designer.cs" />
48+
<Compile Include="Properties\AssemblyInfo.cs" />
49+
</ItemGroup>
50+
<ItemGroup>
51+
<None Include="Resources\AboutResources.txt" />
52+
<None Include="Properties\AndroidManifest.xml" />
53+
<None Include="Assets\AboutAssets.txt" />
54+
</ItemGroup>
55+
<ItemGroup>
56+
<AndroidResource Include="Resources\layout\Main.axml" />
57+
<AndroidResource Include="Resources\values\Strings.xml" />
58+
<AndroidResource Include="Resources\mipmap-hdpi\Icon.png" />
59+
<AndroidResource Include="Resources\mipmap-mdpi\Icon.png" />
60+
<AndroidResource Include="Resources\mipmap-xhdpi\Icon.png" />
61+
<AndroidResource Include="Resources\mipmap-xxhdpi\Icon.png" />
62+
<AndroidResource Include="Resources\mipmap-xxxhdpi\Icon.png" />
63+
</ItemGroup>
64+
<ItemGroup>
65+
<Folder Include="Resources\drawable\" />
66+
</ItemGroup>
67+
<Import Project="$(MSBuildExtensionsPath)\Xamarin\Android\Xamarin.Android.CSharp.targets" />
68+
</Project>

samples/HelloWorld/MainActivity.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using Android.App;
2+
using Android.Widget;
3+
using Android.OS;
4+
5+
namespace HelloWorld
6+
{
7+
[Activity (
8+
Icon = "@mipmap/icon",
9+
Label = "HelloWorld",
10+
MainLauncher = true,
11+
Name = "example.MainActivity")]
12+
public class MainActivity : Activity
13+
{
14+
int count = 1;
15+
16+
protected override void OnCreate (Bundle savedInstanceState)
17+
{
18+
base.OnCreate (savedInstanceState);
19+
20+
// Set our view from the "main" layout resource
21+
SetContentView (Resource.Layout.Main);
22+
23+
// Get our button from the layout resource,
24+
// and attach an event to it
25+
Button button = FindViewById<Button> (Resource.Id.myButton);
26+
27+
button.Click += delegate {
28+
button.Text = string.Format ("{0} clicks!", count++);
29+
};
30+
}
31+
}
32+
}
33+
34+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.xamarin.android.helloworld">
3+
<uses-sdk android:minSdkVersion="21" />
4+
<application android:allowBackup="true" android:icon="@mipmap/icon" android:label="@string/app_name">
5+
</application>
6+
</manifest>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using Android.App;
4+
5+
// Information about this assembly is defined by the following attributes.
6+
// Change them to the values specific to your project.
7+
8+
[assembly: AssemblyTitle ("HelloWorld")]
9+
[assembly: AssemblyDescription ("")]
10+
[assembly: AssemblyConfiguration ("")]
11+
[assembly: AssemblyCompany ("Xamarin Inc.")]
12+
[assembly: AssemblyProduct ("")]
13+
[assembly: AssemblyCopyright ("Xamarin Inc.")]
14+
[assembly: AssemblyTrademark ("Xamarin")]
15+
[assembly: AssemblyCulture ("")]
16+
17+
// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
18+
// The form "{Major}.{Minor}.*" will automatically update the build and revision,
19+
// and "{Major}.{Minor}.{Build}.*" will update just the revision.
20+
21+
[assembly: AssemblyVersion ("1.0.0")]
22+
23+
// The following attributes are used to specify the signing key for the assembly,
24+
// if desired. See the Mono documentation for more information about signing.
25+
26+
//[assembly: AssemblyDelaySign(false)]
27+
//[assembly: AssemblyKeyFile("")]
28+
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
Images, layout descriptions, binary blobs and string dictionaries can be included
2+
in your application as resource files. Various Android APIs are designed to
3+
operate on the resource IDs instead of dealing with images, strings or binary blobs
4+
directly.
5+
6+
For example, a sample Android app that contains a user interface layout (main.axml),
7+
an internationalization string table (strings.xml) and some icons (drawable-XXX/icon.png)
8+
would keep its resources in the "Resources" directory of the application:
9+
10+
Resources/
11+
drawable/
12+
icon.png
13+
14+
layout/
15+
main.axml
16+
17+
values/
18+
strings.xml
19+
20+
In order to get the build system to recognize Android resources, set the build action to
21+
"AndroidResource". The native Android APIs do not operate directly with filenames, but
22+
instead operate on resource IDs. When you compile an Android application that uses resources,
23+
the build system will package the resources for distribution and generate a class called "R"
24+
(this is an Android convention) that contains the tokens for each one of the resources
25+
included. For example, for the above Resources layout, this is what the R class would expose:
26+
27+
public class R {
28+
public class drawable {
29+
public const int icon = 0x123;
30+
}
31+
32+
public class layout {
33+
public const int main = 0x456;
34+
}
35+
36+
public class strings {
37+
public const int first_string = 0xabc;
38+
public const int second_string = 0xbcd;
39+
}
40+
}
41+
42+
You would then use R.drawable.icon to reference the drawable/icon.png file, or R.layout.main
43+
to reference the layout/main.axml file, or R.strings.first_string to reference the first
44+
string in the dictionary file values/strings.xml.

samples/HelloWorld/Resources/Resource.designer.cs

Lines changed: 112 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)