1. Welcome to PHCorner Forums. Take a moment to Sign up and gain unlimited access and extra privileges that guests are not entitled to, such as:

    All that and more! Registration is quick, simple and absolutely free. Join our community today!

C# pahelp po sa visual studio-android(basic Calculator)

Discussion in 'Coding, Programming' started by mbonrostro, Mar 18, 2016.

  1. anyone here po nakakaalam ng basic calculator po using visual studio android ?..
     
  2. Ha? Visual Studio Android? ano ba talaga? Android Studio ba (Mobile Apps) ? Visual Studio (Desktop App) ?
     
  3. android mobile app using visual studio
     
  4. Xamarin is offered in different licenses from free to enterprise levels but for not I will be using the starter version which is the free version. It includes the Xamarin Studio which is great start for those who want to try out creating their first apps for Android, they also offer a Business license which lets you develop in Visual Studio so you can use that rich experience similar to developing Web Apps or Windows Apps, then they have this Enterprise which contains everything. For now lets see what the free stuff can offer, I did a simple project which I will be discussing below and it looks very promising, I love it.

    Please or Register to view links

    Like what I said above we will be using the free version so we will make this simple project by using Xamarin Studio. Now I was thinking what to do, I don’t want to create the boring "Hello World!" demo so what I will be developing is a simple calculator which acts like a normal desk calculator. Most of the calculator projects online regardless its C#, VB.Net developed as Windows
    forms does not act like a normal desk calculator where you can continuously perform operations and deliver results through the screen, you can’t even change operations along the way, so I decided I will make this one and I accepted my own challenge.

    Lets start! First you need to download Xamarin at Please or Register to view links, while installing it will ask you to download as well prerequisites but is you are a developer most or all of them are already set up in your machine.

    Once its downloaded you’re ready to roll!

    Fire up Xamarin Studio and select new Solution then Android Application, give it a solution name and in this case we use "Calculator".

    Please or Register to view links

    Once all ok you will see the IDE which is like a cross between Visual Studio and Eclipse, it has intellisense and fancy text colours and highlighting.

    Please or Register to view links

    You will notice if you go to the Resources\layout they the UI is in a file called Main.axml which is a "Hello World" template.

    Please or Register to view links

    At this point we are more interested on the 2 files on the solution, that layout and the MainActivity.cs where we will be doing our coding.

    Please or Register to view links

    First lets design you calculator, you need to go to that Main.axml and start coding, you will notice its like Windows form where you can drag and drop items from the Toolbox on the right, it will also have its property window on the bottom left.

    Please or Register to view links

    Were not interested with the dragging and dropping part so let’s go the code behind (you can control it more), so hit that Source tab below the UI beside the content source. First thing you will notice is that it is in XML format, if you’re so used to drag and drop or HTML coding then you need a bit of learning here specially the layouts.

    Common layouts used are Linear Layout, Relative Layout, List View and Grid View. To see how each of them would look like here is an illustration

    Please or Register to view links

    Linear Layout – you can organize objects in either horizontal or vertical rows.

    Please or Register to view links

    Relative Layout – specify the location of child objects relative to each other. For example object A is placed on the left of object B or it can be aligned to a parent.

    Please or Register to view links

    List View – Displays it in a scrolling single column list.

    Please or Register to view links

    Grid View – Displays it in a scrolling grid view of columns and rows.

    Now lets start designing, for this project we will using a mix of Linear and Relative. Copy and Paste the code below then I will give you some explanation of what it does.

    Code:

       Sorry you need to Register / Login to see this Codes!    

     
  5. Most of the objects are created similar to an HTML tags like and <EditText>. Also like HTML Tags they will have properties and the common ones we used are:

    • orientation – this sets our object orientation either vertical or horizontal.
    • layout_width – fill_parent will fill depending on the parent with, you can also specify a with by using something like 200dp. If you want to use percentage use 0dp but we will be indicating percentage on layout_weight.
    • layout_height – similar to layout_width.
    • layout_weight – this indicated the percentage you will use on the width.
    • id – identifier to the object.
    • layout_toRightOf – this indicates you will place this object to the right of what object Id.
    • layout_alignTop – this aligns your object similar to what object Id.
    • layout_below – this will place your object below the object Id.
    other elements would sound similar to .Net object properties. Now click your content tab to see how it looks. It should be something like this

    Please or Register to view links

    Now you have your UI, you need to start coding in C#, so go to your MainActivity.cs

    To display the layout you need to do this.

    SetContentView (Resource.Layout.Main);

    Then you will notice that objects are not natively available in code, you cant do something like this

    resultText.Text = "";

    you need to find it like how you find objects but ID in a grid during the ASP.Net days. To do it here is a sample for button1

    Button button1 = FindViewById<Button> (Resource.Id.button1);

    Now you have your button1, you can assign a delegate and define what it should do when you click

    button1.Click += delegate {

    //Your stuff here

    };

    Other than that you’re good to go, everything should be easy. So for the full code just copy and paste the one below, it’s an operational calculator.


    Code:

       Sorry you need to Register / Login to see this Codes!    


    Just a side note : You might notice Calculate happens on each digit press, this makes sure you have a running total even before you hit the operator. The operator buttons will just store the current operator needed for your next digit press.

    Other than that you are now all good to go, just hit run

    Please or Register to view links

    It will now ask you for a device, if you don’t have an emulator yet then you can create one, choose create emulator then hit OK.

    Please or Register to view links

    Click New

    Please or Register to view links

    Then I suggest create something similar to a device you have, so you can play around with it on your device.

    Please or Register to view links

    Once created hit start, then launch

    Please or Register to view links

    You will now see it will start, and it will also show you any errors it encounter.

    Please or Register to view links

    Once started go back to Xamarin and choose the device you want your app to run on, in this case your newly created emulator, it will then be automatically deployed on the emulator.

    Please or Register to view links

    You will then see the progress on the middle top part of the IDE.

    Please or Register to view links

    Then on your emulator

    Please or Register to view links

    License
    This article, along with any associated source code and files, is licensed under Please or Register to view links
     
  6. Grabe imba naman po yan.. thanx po sa info kahit tapos na po ung calcu na app.apache cordova po pala gamit namen wala pa po akong background sa xamarin..
     
  7. okey bossing..late na kase ako nag libot sa mga thread
     
Tags / Keywords: