~/writing / Technical
To Xamarin with Love
Discover why Xamarin outshines Java for Android development. Compare code snippets and learn how C# makes mobile programming simpler and more elegant.
Xamarin Evolve just concluded. And there's no better time to write this post. I'm not writing anything new or even anything related to announcements that happened there. I am writing this because recently I got a chance to work with Android using Java. And the experience was not at all good. And the reason is, not so surprisingly, Java.
I will not explain anything; instead I'll just copy-paste three code snippets. All are doing the same thing but they are all in different languages. Now, for the people who think that language doesn't make any difference—this post is for them. Here I am not including any frameworks like Reactive UI or Xamarin.Forms. If I include them, then this competition will be one-sided. (Xamarin sided)
Code Snippet in Java
public class MainActivity extends Activity {
int count = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setEvents();
}
private void setEvents() {
final Button mybutton = (Button) this.findViewById(R.id.myButton);
mybutton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mybutton.setText(String.valueOf(count));
}
});
final TextView seekbarView = (TextView) this.findViewById(R.id.seekbarTextView);
SeekBar seekbar = (SeekBar) this.findViewById(R.id.seekBar1);
seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
int progressed = 0;
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
seekbarView.setText(String.valueOf(progressed));
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
progressed = progress;
}
});
CheckBox checkbox = (CheckBox) this.findViewById(R.id.checkBox1);
checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
Log.i("info","Checkbox is " + String.valueOf(isChecked));
}
});
}
}
Code Snippet in C#
[Activity (Label = "XamarinBlog", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
int count = 1;
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
var button = this.FindViewById<Button> (Resource.Id.myButton);
button.Click += (sender, e) => {
button.Text = String.Format("{0} clicks",count);
count++;
};
var seekbarTextView = this.FindViewById<TextView> (Resource.Id.seekbarTextView);
var seekbar = this.FindViewById<SeekBar> (Resource.Id.seekBar1);
seekbar.ProgressChanged += (sender, e) => {
seekbarTextView.Text = String.Format("The value of seekbar is {0}", e.Progress);
};
var checkbox = this.FindViewById<CheckBox> (Resource.Id.checkBox1);
checkbox.CheckedChange += (sender, e) => {
Console.WriteLine("Checked changed!");
};
}
}
Code Snippet in F#
[<Activity(Label = "XamarinBlog", MainLauncher = true)>]
type MainActivity() =
inherit Activity()
let mutable count : int = 1
override this.OnCreate(bundle) =
base.OnCreate(bundle)
// Set our view from the "main" layout resource
this.SetContentView(Resource_Layout.Main)
// Get our button from the layout resource, and attach an event to it
let button = this.FindViewById<Button>(Resource_Id.myButton)
button.Click.Add(fun args ->
button.Text <- sprintf "%d clicks!" count
count <- count + 1)
let seekbarTextView = this.FindViewById<TextView>(Resource_Id.seekbarTextView)
let seekbar = this.FindViewById<SeekBar>(Resource_Id.seekBar1)
seekbar.ProgressChanged.Add
(fun args -> seekbarTextView.Text <- sprintf "The value of seekbar is %A" args.Progress)
let checkbox = this.FindViewById<CheckBox>(Resource_Id.checkBox1)
checkbox.CheckedChange.Add(fun args -> printfn "Check changed")
Hope you can see what I am trying to say here. I can become a good typist by writing code in Java but definitely not a good programmer. I don't know from which planet Java has incorporated its Object Oriented model.
Xamarin is solving many problems by supporting C# and F# for Android development.
<3<3<3 Xamarin
You can also compare code between C# and F# but don't tell anyone—right now I am bashing Java in this post. :P
Frequently Asked Questions
Xamarin is a framework that allows developers to write Android applications using C# instead of Java. The main advantage is that C# offers cleaner syntax and more concise code compared to Java, making development faster and less error-prone, as demonstrated by the code examples comparing Java and C# implementations of the same functionality.
C# uses lambda expressions and event handling syntax that is more concise than Java's anonymous inner class approach. For example, setting button click listeners in C# requires just a few lines with arrow functions, whereas Java requires defining entire anonymous classes with multiple override methods, making C# significantly more readable and maintainable.
Yes, Xamarin.Forms is a framework that can be used alongside Xamarin to create cross-platform mobile applications. The author mentions that including Xamarin.Forms would make the comparison even more favorable to Xamarin, suggesting it provides additional benefits and abstraction layers for development.
Based on the post, Java requires excessive boilerplate code, verbose syntax for event handling, and generates auto-generated code comments like "TODO Auto-generated method stub." These issues make Java development tedious and harder to maintain compared to more modern languages like C#.