Let me share with you some things I have learned during my first few days in the world that is Android. In particular, in this post I want to talk about how some of the basic buildings blocks of Android relate back to Silverlight concepts such as Forms, and Controls.

In Android, the basic building block for logic is the Activity. The Activity can be roughly equated to the code-behind that one uses in a Form in Silverlight land. While the XAML in Silverlight is part of the Control or Form definition, in Android the XML layouts are defined independently in their own files not associated to any particular Activity at compile-time. At run-time, one can then apply a XML defined layout to an Activity.

So you have an Activity, an Activity is the logic that will manage one screen, two screen, or however many screens you want. The next concept I’ve discovered is that of a View. A view is an XML layout which defines how the UI should look. It is defined completely independently of any Activity. A View can be launched by a activity and is done so through via the setContentView() method within a activity.

Activity, View…got it. So far so good it appears.

[sourcecode language=”java”]
<p style="margin-left: 27pt;">@Override</p>
<p style="margin-left: 27pt;">public void onCreate(Bundle savedInstanceState) {</p>
<p style="margin-left: 54pt;">super.onCreate(savedInstanceState);</p>
<p style="margin-left: 54pt;">setContentView(R.layout.login_layout);</p>
<p style="margin-left: 54pt;">Button loginButton = (Button) findViewById(R.button.btn_Login);</p>
<p style="margin-left: 54pt;">loginButton.setOnClickListener(loginListener);</p>
<p style="margin-left: 54pt;">Button cancelButton = (Button) findViewById(R.button.btn_Cancel);</p>
<p style="margin-left: 54pt;">cancelButton.setOnClickListener(cancelListener);</p>
<p style="margin-left: 27pt;">}[/sourcecode]

This code snippet outlines the creation and launching of a LoginActivity I have created. The activity is meant to back end a simple screen which prompts a user for their credentials. The onCreate() method resides in an Activity and is executed when the Activity is created (who would have thought?!)

The first thing you will notice being different than Silverlight is in the second line. The call to setContentView references a XML layout that I have created and instructs the activity to essentially skin itself with that layout. The layout format is very much like XAML, but not quite XAML.

Next thing you will notice is that unlike in Silverlight/.NET, where you are able to access controls directly defined in the XAML via their x:Name attribute, you need to find a reference to a particular view widget by using the findViewByID method. The value which is passed to it is a integer which represents a unique addressing of that widget.

Even now I am starting to see how the two worlds are different. While I wouldn’t dare call Android better/worse than Wp7 after such a short time, it does appear that the Silverlight development experience is much cleaner and simpler to work with. By simpler I mean a lot of the plumbing that needs to be done in Android is taken care of automatically in Silverlight. Using XAML, I can construct almost all program logic within the XML structures defined. Android’s XML layout story is not nearly as powerful. Now is that good or bad? That’s a different question. But for those of us who come from the .NET world, you will find out that a lot of the hand holding you get from the .NET experience isn’t present in Android.

Oh, and don’t get me started on XML handling. But I will save the lessons I have learned from that for another day.

Share with: