In case someone is having difficulties with Android Studio due to some routine and annoying errors, using C# with Visual Studio in the meanwhile is quite good.

Just go to file, new project, C# Android application. Then click on the MainActivity.cs . You’ll need to download xamarin something if you are asked to.

If one is used with Java, you will find C# very much alike and sometimes really nice.

Here is the code for a simple app in Visual Studio. When you click the button, the text of the label is changed .

Some interesting notes :

  • a label is a TextView
  • a TextView is a view
  • a button is a view

Note that this code has not made any use of the designer.

The code is explained as much as possible at the end.

The app :

12

The code:

using Android.App;
using Android.Widget;
using Android.OS;
using System;
namespace App2
{
[Activity(Label = “App2”, MainLauncher = true, Icon = “@drawable/icon”)]
public class MainActivity : Activity
{
public object R { get; private set; }

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

//code really starts here
LinearLayout layout = new LinearLayout(this);
layout.Orientation = Orientation.Vertical;

TextView label = new TextView(this);
label.Text = “i am a textview”;

Button button = new Button(this);
button.Text = “i am a button”;

button.Click += (object sender, EventArgs e) =>
{

label.Text = “Aslm”;
button.Text = “clicked”;
};

layout.AddView(label);
layout.AddView(button);
SetContentView(layout);

}

}
}

and don’t forget to press run !

The explanations:

  • Android.App

Contains high-level classes encapsulating the overall Android application model.

  • Android.Widget
The widget package contains (mostly visual) UI elements to use on your Application screen.
  • Android.OS
Provides basic operating system services, message passing, and inter-process communication on the device.
  • System

The System namespace contains fundamental classes and base classes that define commonly-used value and reference data types, events and event handlers, interfaces, attributes and processing exceptions.

  • namespace App2

Apps2 is the name of our app

// is used for comments

else, as from //code really starts here

  1. you create a linear layout object
  2. you set the orientation to vertical
  3. you create a new text view object
  4. you specify it’s text
  5. you create a button object
  6. you specify the button’s text
  7. you specify what to do when the button is clicked i.e the what the text view’s text and the button’s text will be when changed
  8. you add the text view to the layout
  9. you add the button to the layout
  10. you add the layout to the screen

__________________

The Activity class takes care of creating a window for you in which you can place your UI with setContentView(View)

onCreate(Bundle) is where you initialize your activity. Most importantly, here you will usually call setContentView(int) with a layout resource defining your UI, and using

__________________
More info about Android.App :

An Android application is defined using one or more of Android’s four core application components. Two such application components are defined in this package: Activity and Service. The other two components are from the Android.Content package: BroadcastReceiver and ContentProvider.

An Activity is an application component that provides a screen with which users can interact in order to do something, such as dial the phone, take a photo, send an email, or view a map. An activity can start other activities, including activities that live in separate applications.

A Service is an application component that can perform long-running operations in the background without a user interface. For example, a service can handle network transactions, play music, or work with a content provider without the user being aware of the work going on.

The Fragment class is also an important part of an application’s design—especially when designing for large screen devices, such as tablets. A fragment defines a distinct part of an activity’s behavior, including the associated UI. It has its own lifecycle that is similar to that of the activity and can exist alongside other fragments that are embedded in the activity. While an activity is running, you can add and remove fragments and include each fragment in a back stack that’s managed by the activity—allowing the user to navigate backwards through the fragment states, without leaving the activity.

This package also defines application utilities, such as dialogs, notifications, and the action bar.

SOURCE of explanations :

base code credit : Johnny Manson

Advertisements