Let’s get the ball rolling with the exciting stuff and develop our first HelloWorld Android app with Xamarin using C# in Visual Studio. I’ll explain the detailed concept of Android development and android apps in the future posts, while this post will only give you a flavor of Xamarin Android app development with a simple HelloWorld app.
Creating a new Xamarin.Android project with Visual Studio is similar to creating any other project. You select “File -> New Project” and then select “Android” from templates categories and select “Blank App (Android)” template. Name the project as “HelloWorld” and specify the location where you would like to save the project code.
After you click “OK”, you’ll see the Solution in Solution Explorer, just like any other .NET Solution with a Project. I’ll explain the project structure in the next post, but you can see that it looks familiar. From the Resources folder double click Main.axml under layout folder to view the layout of the main Android app screen. Once you can see the layout, drag a text view and a button on the layout.
If you see an error while displaying the layout in designer mode like Android SDK version is older, you can use Android SDK Manager to get the latest SDK.
Now open the MainActivity.cs file and edit it to write the code behind this layout. We’ll add the code to display ‘Hello World’ and ‘Good Bye’ alternatively when we click the button. The complete code in the MainActivity.cs file should look like the following.
[Activity(Label = "HelloWorld", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
protected override void OnCreate(Bundle bundle)
// Set our view from the "main" layout resource
//find button and textview from the layout using resource ids
Button button = FindViewById<Button>(Resource.Id.button1);
TextView textView = FindViewById<TextView>(Resource.Id.textView1);
//set text of button and textview
button.Text = "Click Me!";
button.Click += delegate
if (textView.Text == "Hello World!")
textView.Text = "Good Bye";
textView.Text = "Hello World!";
It’s now time to run the project and see the result of our efforts. You can simply run the project in Visual Studio as usual and the output will be shown in an Android Simulator. This is how the Android App should look like in the simulator.
Did your HelloWorld app work? 🙂 If not the first time, don’t worry, you can leave a comment and ask any query related to this topic. I’ll answer it.