Sunday, October 23, 2011

Creating Activities for Tablets and Phones

Most apps start with the launcher icon. Activities that are specified with the following intent-filter will have a launcher icon for them.

    
        
        
    
There's a lot of Android hardware out there. The most notable form factors are phones and tablets. In Android 3.0 Fragments were introduced as a solution to compartmentalize logic so that it can be easier to share between phones and tablets. Fragments are great but having 1 Activity to work with phones and tablets is difficult to maintain.


The cleanest solution is to have separate activities for phones and tablets and use bool resources to tell Android which activity to use. Notice that the booleans are true based where they are stored, we assume phones but xlarge displays usually indicate tablets. Then in the AndroidManifest.xml we have the 2 activities declared where the android:enabled is based on which mode the app is going to run in.


This solution isn't limited to Tablets vs Phones. You can basically create conditionals as the resource folder qualifiers allow.


res/values/bools_hardware.xml

    true
    false
res/values-xlarge/bools_hardware.xml

    false
    true

AndroidManifest.xml

    
        
        
    


    
        
        
    

1 comment:

  1. can't make it work on ICS 4.0.3. it is strange, because android:label is read from valid values file (xlarge), but android:enabled seems to be initialized only with entries from default values.xml.

    ReplyDelete