This blog basis my experience of working with Scala, I can ensure the technology capabilities to implement complex use cases in concise and type-safe way. Scala supports functional programming which makes things more easy and readable.
Recently when I started implementation using Android. I could see lot of boilerplate code which was difficult to maintain. This inspired me to explore if Scala can be used in Android, as Scala also runs on JVM. Implementation using Scala is faster, portable and expressing complex algorithms is easier, thanks to its exhaustive collection libraries, and functional programming features.
Scaloid: Less painful Android development with Scala
Scaloid is a library that simplifies your Android code. It makes your code easy to understand and maintain by using Scala as a language
For example the code block shown below:-
val button = new Button(context)
button.setText(“Scaloid”)
button.setOnClickListener(new OnClickListener() {
def onClick(v: View) {
Toast.makeText(context, “Hi!”, Toast.LENGTH_SHORT).show()
}
})
layout.addView(button)
is reduced to:
SButton(“Greet”, toast(“Hello!”))
Benefits :
- Scaloid provides a concise and type-safe way of writing Android application.
- You can use both Scaloid and plain-old Java Android API
- Less verbose and functional programming
Android programming is verbose and it’s difficult to write solution for even a small problem.
Let us see an example of a typical Activity implementation:
class AndroidWay extends Activity {
TextView name;
ImageView thumbnail;
LocationManager loc;
Drawable icon;
String myName;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
name = (TextView) findViewById(R.id.name);
thumbnail = (ImageView) findViewById(R.id.thumbnail);
loc = (LocationManager)
getSystemService(Activity.LOCATION_SERVICE);
icon = getResources().getDrawable(R.drawable.icon);
myName = getString(R.string.app_name);
name.setText(myName);
}
}
Cleaner, concise and with no run-time performance degradation.
UI layout without using XML in Android??
Android SDK provides a way to build UI layout using Xml. Designing a login page using XML like this :
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:orientation=”vertical” android:layout_width=”match_parent”
android:layout_height=”wrap_content” android:padding=”20dip”>
<TextView android:layout_width=”match_parent”
android:layout_height=”wrap_content” android:text=”Sign in”
android:layout_marginBottom=”25dip” android:textSize=”24.5sp”/>
<TextView android:layout_width=”match_parent”
android:layout_height=”wrap_content” android:text=”ID”/>
<EditText android:layout_width=”match_parent”
android:layout_height=”wrap_content” android:id=”@+id/userId”/>
<TextView android:layout_width=”match_parent”
android:layout_height=”wrap_content” android:text=”Password”/>
<EditText android:layout_width=”match_parent”
android:layout_height=”wrap_content” android:id=”@+id/password”
android:inputType=”textPassword”/>
<Button android:layout_width=”match_parent”
android:layout_height=”wrap_content” android:id=”@+id/signin”
android:text=”Sign in”/>
<LinearLayout android:orientation=”horizontal”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”>
<Button android:text=”Help” android:id=”@+id/help”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”/>
<Button android:text=”Sign up” android:id=”@+id/signup”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”/>
</LinearLayout>
</LinearLayout>
And for handling the login event in java:
EditText userId = (EditText) findViewById(R.id.userid);
Button signIn = (Button) findViewById(R.id.signin);
signIn.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
signIn(userId.getText());
}
});
Using Scaloid you can combine both the things in simple lines of code like this:-
new SVerticalLayout {
STextView(“Sign in”).textSize(24.5 sp).<<.marginBottom(25 dip).>>
STextView(“ID”)
SEditText()
STextView(“Password”)
SEditText() inputType TEXT_PASSWORD
SButton(“Sign in”)
this += new SLinearLayout {
SButton(“Help”,openUri(“http://help.url”))
SButton(“Sign up”)
}
}.padding(20 dip)
Scaloid is also compatible with current XML layouts of Android
Pitfalls
- According to this benchmark, runtime performance of Scala is a little worse than that of Java but that is negligible
- The size of the apk increases by few hundred KB as the library is also added in the apk
- Even the compilation takes some time as it also compiles the Scala source code, but if you are working in multicore environment this would be a matter of few seconds.
Conclusion
You can write apps in very less time using scaloid:
- Scaloid provide a type-safe and elegant way of writing Android Applications
- Compatible with existing Android API
- Supports Implicits and functional programming
- It increases the compile time, size but it’s easy and type safe way to develop an app on it.