Android Tips from File->New Project

Posted on 25 Jan 2013 by Eric Oestrich

I learned several Android tips the other day from an unlikely place: File->New Project. I started a new project and found out that the latest SDK gives you an option to include an activity as your base. I chose LoginActivity and found several new things.

EditTexts can have errors

Android EditText Error

This is possible by calling setError on the EditText.

LoginActivity.java - attemptLogin()
// Check for a valid password.
if (TextUtils.isEmpty(mPassword)) {
  mPasswordView.setError(getString(R.string.error_field_required));
  focusView = mPasswordView;
  cancel = true;
} else if (mPassword.length() < 4) {
  mPasswordView.setError(getString(R.string.error_invalid_password));
  focusView = mPasswordView;
  cancel = true;
}

The merge view

I’m still a little uncertain about this one, but after doing some searching it looks like you can speed up the display of your views if you use merge. More info.

activity_login.xml
<merge xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  tools:context=".LoginActivity" >

  <!-- Login progress -->

  <LinearLayout ... />
  <ScrollView ... />
</merge>

Value files can be layout specific

This one might be my favorite tip of them. I knew you could make resource files specific to versions of android, but I didn’t know you could make value files specific to a layout. With this you can stick layout specific strings in a separate file and not clutter up the main strings.xml file.

Android values and layouts folder

comments powered by Disqus
Creative Commons License
This site's content is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License unless otherwise specified. Code on this site is licensed under the MIT License unless otherwise specified.