Slot Machine App Tutorial Xamarin

In that tutorial, you are going to discover how to create a Slot Machine for Android with Android Studio. A Slot Machine, also known as one-armed bandit, is a casino gambling machine with three or more reels which spin when a button is pushed. Our Slot Machine will have three reels. Note that you can also enjoy this tutorial in video on YouTube.

  • Xamarin Tutorial
  • Xamarin Useful Resources
  • Selected Reading

In this chapter, we will see how to create a small Android application using Xamarin.

Hello Xamarin! Application

First of all, start a new instance of Visual Studio and go to File → New → Project.

On the Menu dialog box that appears, go to Templates → Visual C# → Android → Blank App (Android).

Xamarin

Give an appropriate name for your application. In our case, we name it “helloWorld” and save it in the default location provided. Next, click the OK button for the new “helloXamarin” project to load.

On the solution, open Resources → layout → Main.axml file. Switch from Design View and go to the Source file and type the following lines of code to build your app.

In the above code, we have created a new Android textview. Next, open the folder values and double-click Strings.xml to open it. Here, we are going to store information and values about the button created above.

Open MainActivity.cs file and replace the existing code with the following lines of code.

Save the application. Build and then run it to display the created app in an Android Emulator.

If you do not have an Android Emulator, then follow the steps given in the next section to create one.

Setting Up an Android Emulator

On your Visual Studio menu, go to Tools → Android → Android Emulator Manager. On the pop-up window that appears, click the Create button. It will display the following screen.

On the above screen, supply the AVD name you want. Select a device that is appropriate for your display, e.g., Nexus 4” display. Select your target platform. It is always advisable to test on a minimum target platform, e.g., API 10 Android 2.3 (Gingerbread) so as to ensure your App works across all Android platforms.

Fill in the rest of the fields and click the OK button. Your emulator is now ready. You can select it from the list of existing Android Virtual Devices and then click Start to launch it.

Modifying HelloXamarin App

In this section, we will modify our project and create a button which will display text upon click. Open main.axml and switch to source view. After our textview that we created, we will add a button as shown below.

After adding a button, our full code will look like this −

Next, we register our button values in the strings.xml file.

After adding our button in the strings.xml file, we will open MainActivity.cs file to add an action for our button when it is clicked, as shown in the following code.

Next, build and run your application.

Slot Machine App Tutorial Xamarin Android Studio

After clicking on the button, you will get the following output −