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 #188

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?

appamatto commented 11 years ago

This is nice. Maybe it should be something simple like LoginActivity extends Activity with Layout.Login.

appamatto commented 11 years ago

I merged this into master. There was a bug in the camel-case code that was fixed, and I changed the naming around. Please let me know what you think!

appamatto commented 11 years ago

Here is a gist illustrating the changes I made: https://gist.github.com/appamatto/6253667