Friday, April 20, 2012

Sending an Email


The following bit of code enables your app to send an email. If a user has multiple email processing apps, like an email app and Gmail, a picker will automatically be displayed for them to select the email app they want to use to send the message.




 String mMessage="Here is a very important email message that you need to read right away."; 

 String mSubject="Urgent: Read me now!"; 

 String mEmailTo="mom@you.com"; 

 String mCCTo="dad@you.com"; 

 Intent emailI = new Intent(Intent.ACTION_SEND); 

 emailI.setType("text/plain"); 

 if (mSubjectStr!=null) { 

      emailI.putExtra(Intent.EXTRA_SUBJECT, mSubject); 

 } else { 

      emailI.putExtra(Intent.EXTRA_SUBJECT, getResources().getString(R.string.emailSubjectVal)); 

 } 

 if (mCCValStr!=null) { 

      emailI.putExtra(android.content.Intent.EXTRA_CC,new String[]{ mCCTo}); 

 } 

 emailI.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] { mEmailTo }); 

 emailI.putExtra(android.content.Intent.EXTRA_TEXT, mMessage); 

 startActivity(emailI);  


Disabling the Back Button

In a few cases, my clients have wanted to disable the hardware back button on a screen or screens and put their own "exit" icon on the screen. This prevents the user from accidentally exiting a screen,say if they are holding a tablet and their thumb slips and/or gives them the ability to throw up an exit confirmation dialog when the layout exit button is clicked.  To do this for a particular activity, simply override the onBackPressed() method.


 @Override
public void onBackPressed()
{

return;
}

Tuesday, April 17, 2012

Android Source Bug Reports

A useful tool for debugging is the Android Bug report database where known issues with the Android code base are reported and tracked. This repository is particularly useful when debugging an issue that seems to be occurring on a particular brand of handset or Android build level


A great example of using the archive to debug issues revolving around a build level or handset is the GSON issues that occur with Acer devices (and HTC) which is where I ran into it. 

Sunday, April 15, 2012

Text Wrapping Expandable List Adapters

Expandable Lists allow you to have a two tiered list consisting of a list of groups and their attached children. I've used this type of adapter on a couple of projects and it's quite easy.  The Groups and children can have custom layouts just like a normal list adapter. This allows you to style both the groups and the children. The group adapter automatically displays an open and close arrow icons on the right side, although the graphic used varies from device to device or Android build level to build level.  On a recent project, the customer had a really long group name that wrapped behind the right hand side open and close icons. This was an easy fix though via adding a right side margin to my group's title text field as in the xml below.



 <?xml version="1.0" encoding="utf-8"?> 

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 

   android:layout_width="fill_parent" 

   android:layout_height="75dp" 

   android:orientation="horizontal" 

   android:background="@drawable/gridpaper_row"> 

   <ImageView 

     android:id="@+id/liImage" 

     android:tag="togglebutton" 

     android:layout_width="wrap_content" 

     android:layout_height="wrap_content" 

     android:layout_marginRight="5dp" 

     android:layout_gravity="center_vertical" 

     android:onClick="statusGroupClickHandler" 

     android:src="@drawable/checkmarkuncheckedimage58x54" /> 

   <TextView 

     android:id="@+id/liName" 

     style="@style/MediumBlackBoldText" 

     android:layout_width="fill_parent" 

     android:layout_height="wrap_content" 

     android:layout_weight=".5" 

     android:layout_marginRight="35dp" 

     android:layout_gravity="center_vertical" 

     android:gravity="left|center_vertical" /> 

 </LinearLayout>  

Saturday, April 14, 2012

Determining Target API

One of the first decisions I make when implementing an Android app is what platform version target level to set.  This impacts who can use your app. Choose to high and users may not be able to see your app in Google Play. Choose to low and you may miss out on some important features, bug fixes and performance gains of the more recent APIs. To help my clients decide, I show them a a great chart from Google of active devices running the different versions of  Android platform.  


http://developer.android.com/resources/dashboard/platform-versions.html

Use this chart to decide what build level to target. Currently (as of April 2, 2012) only about 7% of all devices are running below Android 2.2. So in my book that's the base level. Almost 85% of all devices runs 2.2 or 2.3, so bottom line? Go with 2.2 unless you're targeting a specific device or have a specific need to leverage an API only available in 3.0 or 4.0.

Friday, April 13, 2012

Testing REST APIs

When starting a project, I always ask for a list of the APIs and sample responses and requests including auth requirements, parameters and data types.  Preferably there is a document that details all of the ones required but sometimes this is provided in a wiki or back of a napkin depending on how far along the backend design is.

Once I have that documentation, I typically want to test the calls to make sure that the documentation matches the actual API. There are a bunch of tools out there that can help you test the apis, and you can google them to find a good one for your environment. I like to use the Rest Console, a Chrome plugin.  It allows me to do pretty much any time of call via Post, GET, Put or Delete, allows me to set custom headers and displays the server response.

Anytime that I have an issue with an API I plug values into the console to debug what's going on. It's faster and easier than debugging it via Eclipse.

Thursday, April 12, 2012

Limiting Edit Text to Alphanumeric

Android provides an easy way to edit text fields by modifying the layout xml and adding an android:inputType="text". This lets you easily create some basic validations like numbers, decimal, phone or emails. But there's parameter for no alphanumeric (i.e. no special characters). To do this, you need to user an input filter like below and set the fields you want to validate with that filter in code. This imput filter
      InputFilter alphaNumericFilter = new InputFilter() {   
                @Override  
       public CharSequence filter(CharSequence arg0, int arg1, int arg2, Spanned arg3, int arg4, int arg5)  
       {  
                     for (int k = arg1; k < arg2; k++) {   
           if (!Character.isLetterOrDigit(arg0.charAt(k))) {   
               return "";   
           }   
       }   
                     return null;   
       }   
          };   
     mFirstName.setFilters(new InputFilter[]{ alphaNumericFilter});   
Now how about an email address?
     InputFilter filterEmail = new InputFilter() {   
                  @Override  
         public CharSequence filter(CharSequence arg0, int arg1, int arg2, Spanned arg3, int arg4, int arg5)  
         {  
                       boolean valid=false;  
                       for (int i = arg1; i < arg2; i++) {   
             if (Character.isLetterOrDigit(arg0.charAt(i)) ) {   
                  valid=true;  
             } else if ( arg0.charAt(i)=='@') {  
                  valid=true;  
             } else if     (arg0.charAt(i)=='.') {  
                  valid=true;       
             }  
         }   
                       if (valid) {  
                            return null;  
                       } else {  
                            return "";  
                       }  
         }   
            };