Make a Simple Android App – Xamarin Android Tutorial

7  comments

This post contains all the code that’s been written in this YouTube video.

Main.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:text="0"
        android:textSize="50sp"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/txtNumber"
        android:layout_marginBottom="20dp"
        android:layout_marginTop="20dp" />
    <Button
        android:text="Increment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btnIncrement" />
    <Button
        android:text="Decrement"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btnDecrement" />
</LinearLayout>

 

MainActivity.cs

using Android.App;
using Android.Widget;
using Android.OS;

namespace XamarinTut
{
    [Activity(Label = "XamarinTut", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        TextView txtNumber;

        int number;

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

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

            txtNumber = FindViewById<TextView>(Resource.Id.txtNumber);

            FindViewById<Button>(Resource.Id.btnIncrement).Click += (o, e) =>
            txtNumber.Text = (++number).ToString();

            FindViewById<Button>(Resource.Id.btnDecrement).Click += (o, e) =>
            txtNumber.Text = (--number).ToString();
        }
    }
}

 

About the author 

Matt Rešetár

Matt is an app developer with a knack for teaching others. Working as a freelancer and most importantly developer educator, he is set on helping other people succeed in their Flutter app development career.

You may also like

  • Hi,

    nice tutorial. Very well explained.
    I got stuck with some error messages though.
    At the end I have copied your original source code for both files into my project. (Did a restart on VS as well)
    But I still get the following error messages:
    ‘Resources.Layout’ does nor contain a definition for ‘Main’
    ‘Resources.Id’ does not contain a definition for ‘txtNumber’
    ‘Resources.Id’ does not contain a definition for ‘btnIncrement’
    ‘Resources.Id’ does not contain a definition for ‘btnDecrement’

    Could you gice some advice what went wrong?

  • One issue I’ve had is that for no obvious reason, I get

    Severity Code Description Project File Line Suppression State
    Error CS0103 The name ‘Resource’ does not exist in the current context KeyAttemp3 P:\C#\Android\KeyAttemp3\KeyAttemp3\MainActivity.cs 21 Active

    SetContentView(Resource.Layout.activity_main);

    Why is the section called Resources but it is accessed via Resource?

  • Hi, I am a new coder, build this code but error happened, its shown “CS0117 ‘Resource’ not include ‘id’ define…”
    I check tying again n again (android:id=”@+id/txtNumber”), clean/re-build too, but still experience this error, can you let me know whats wrong?

  • {"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}
    >