jberkel / android-plugin

An sbt plugin for Android development in Scala
https://groups.google.com/forum/#!forum/scala-on-android
Other
476 stars 113 forks source link

Add another typed resources task #184

Closed taku0 closed 11 years ago

taku0 commented 11 years ago

I wrote another typed resources task. This utilize lazy val for more natural interface. For example, suppose we have the following layout: login.xml:

<LinearLayout>
  <EditText android:id="username"/>
  <EditText android:id="password" android:inputType="textPassword"/>
  <Button android:id="loginButton" android:text="Login"/>
</LinearLayout>

An activity using conventional typed resources may be like this:

class LoginActivity extends Activity {
  var usernameField: EditText = _
  var passwordField: EditText = _

  protected override def onCreate(savedInstanceState: Bundle) {
    super.onCreate(savedInstanceState)

    setContentView(R.layout.login)

    usernameField = findView(TR.layout.username)
    passwordField = findView(TR.layout.password)

    findView(TR.layout.loginButton).setOnClickListener(
      new View.OnClickListener {
        override def onClick(view: View) {
          doLogin()
        }
      }
    )
  }

  def doLogin() {
    val username = usernameField.getText
    val password = passwordField.getText

    ...
  }
}

With new typed resources, we can write it more concisely:

class LoginActivity extends typed_resource.layout.Login.Activity {
  protected override def onCreate(savedInstanceState: Bundle) {
    super.onCreate(savedInstanceState)

    views.loginButton.setOnClickListener(
      new View.OnClickListener {
        override def onClick(view: View) {
          doLogin()
        }
      }
    )
  }

  def doLogin() {
    val username = views.username.getText
    val password = views.password.getText

    ...
  }
}

Note that we do not have to call setContentView.

Views such as views.loginButton are defined with lazy val.

Since typed_resource.layout.Login.Activity is a trait rather than a class, we can use custom base activities:

class LoginActivity extends MyBaseActivity with typed_resource.layout.Login.Activity {
  ...
}

We can also define dialogs in a similar way:

class LoginDialog extends with typed_resource.layout.Login.Dialog {
  ...
}

Moreover, we also have wrappers suitable for use with LayoutInflater or SimplerAdapter.ViewBinder:

val layout = typed_resource.layout.Login(layoutInflater.inflate(R.layout.login, container))

layout.views.username.setText(username)
...

I will enhance this task to generate other value resources (e.g. strings, ints, colors).

I named it TypedResources2 provisionally, but more appropriate name is wanted. Are there any better names?