Programming Activities for Tablets

After you update your AndroidManifest file, the next step is to begin coding the new activities that are unique to the tablet version of your app.

Creating the ReminderListAndEditorActivity

The tablet version of your app needs a new, main activity. One that works for phones doesn’t work with tablets because you want to retain that activity so that your app continues to work on phones. Create a ReminderListAndEditorActivity.java file and add this code to it:

package com.dummies.android.taskreminder;

import android.os.Bundle;

import android.support.v4.app.FragmentActivity; →4

public class ReminderListAndEditorActivity extends FragmentActivity {

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.reminder_list_and_editor); →10

}

}

Here’s how the code works:

4 This line ensures you use the FragmentActivity from the support library rather than the one built into Android 3.x and later. Otherwise your code doesn’t work on earlier versions of Android.

10 The R.layout.reminder_list_and_editor layout, which defines the layout for this activity doesn’t exist yet, but you create it later in this chapter.

Add the new activity to the AndroidManifest.xml file beneath the ReminderEditActivity that already exists for phones:

<activity android:name=”.ReminderListAndEditorActivity” android:label=”@string/edit_reminder_title”/>

tip.eps Give the activity a title. Open strings.xml and add a string for edit_reminder_title, such as Task Reminder – Edit.

Choosing the right activity

The Task Reminder application now has two different main activities — one for phones and one for tablets. When the user launches your app, which one will Android choose?

At the moment, Android chooses the phone activity because that’s the only one associated with the android.intent.action.MAIN intent filter. Because there’s no way for the app to automatically choose the correct activity for you, you need to detect it manually when the app starts and then switch to the tablet version, if necessary.

Add the following method to your ReminderListActivity class:

private Boolean isTablet() {

int sizeMask = getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK;

boolean large = (sizeMask == Configuration.SCREENLAYOUT_SIZE_LARGE);

boolean xlarge = (sizeMask == 4);

return large || xlarge;

}

This code detects whether the device has a screen in large or extra-large format. If it does, it returns true.

technicalstuff.eps Why sizeMask == SCREENLAYOUT_SIZE_LARGE for the large Boolean, but sizeMask == 4 for the xlarge Boolean? On later versions of Android, SCREENLAYOUT_SIZE_XLARGE is equal to 4, so you might think you could replace the 4 with SCREENLAYOUT_SIZE_XLARGE. However, SCREENLAYOUT_SIZE_XLARGE did not appear in Android until API level 9, so if you try to use this constant on an older device the device will crash. Because the Task Reminder app supports devices older than API level 9, you can’t use the SCREENLAYOUT_SIZE_XLARGE constant directly. See http://d.android.com/reference/android/content/res/Configuration.html for more information about SCREENLAYOUT_SIZE_XLARGE.

technicalstuff.eps Beginning with Android 3.2, the SCREENLAYOUT_SIZE_MASK has been deprecated in favor of a new system of device size qualifiers. The system is much more reliable for choosing whether to show a phone or tablet layout, but because it isn’t available before version 3.2 (even with the support library), it’s beyond the scope of this book. Visit http://developer.android.com/guide/practices/screens_support.html for details about the new size qualifiers.

In your onCreate() method, add the code shown in bold before the call to setContentView():

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

// Switch to Tablet activity and finish this one if we’re on a tablet.

if (isTablet()) {

startActivity(new Intent(this, ReminderListAndEditorActivity.class));

finish();

return;

}

setContentView(R.layout.reminder_list);

}

The idea is that if this code runs on a tablet, the ReminderListActivity immediately quits and starts the ReminderListAndEditorActivity instead. This way, if a user opens your app on a phone, your app works optimally for the phone size. But if a user opens your app on a tablet, it runs optimally for a tablet. A slight overhead cost is involved in doing this kind of switching, but it should be unnoticeable to your users.

Creating the activity layout

After you have the activity, you need to create its layout.

At this point, you already know the basic idea of what this layout will look like: a list fragment and an edit fragment placed side by side.

Create a reminder_list_and_editor.xml file in your res/layout directory and add the following code to it:

<?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=”fill_parent”

android:orientation=”horizontal” > →5

<fragment

android:id=”@+id/list_fragment” →8

android:name=”com.dummies.android.taskreminder.ReminderListFragment” →9

android:layout_width=”0dp” →10

android:layout_height=”fill_parent” →11

android:layout_weight=”1” /> →12

<FrameLayout

android:id=”@+id/edit_container” →15

android:layout_width=”0dp”

android:layout_height=”fill_parent”

android:layout_weight=”1” /> →18

</LinearLayout>

Here’s how the code works:

5 The line wraps your two fragments in a full-screen, horizontal LinearLayout so that they appear side by side.

8–9 These lines designate the first fragment as the list fragment, called “@+id/list_fragment”, and specify its full class name as com.dummies.android.taskreminder.ReminderListFragment.

10 This line gives the layout of a width 0dp. See line 12 to find out why zero width is a benefit.

11 This line tells the list fragment to occupy half the screen horizontally and the full screen vertically.

12–18 LinearLayouts support the special “layout_weight” parameter, which can be used to flexibly split the screen between two or more views. To use it, set the widths or heights of the children of LinearLayout to 0dp, and then set a weight for each one. The LinearLayout adds all the weight values and assigns each child the proportion that the individual child’s weight represents.

Line 12 designates list_fragmentand edit_container views have a weight of 1 so that each view occupies half the screen.

You can also make the list_container have a value of 1 and the edit_container have a value of 2, in which case the list_fragment occupies a third of the screen and the edit_container takes the remaining two-thirds.

remember.eps Note that this strategy works only for LinearLayouts, though it can be quite handy.

15 This line designates a placeholder (“@+id/edit_container”)for ReminderEditFragment. A placeholder is preferable because, even though list view is always onscreen, edit view may come and go, depending on whether a user is editing an item. If an item isn’t being edited, it would be confusing to the user to see an empty edit fragment on the screen.

Run your application on both your phone and the tablet emulator. Though the new ReminderListAndEditorActivity shows up on the tablet emulator, it won’t on the phone emulator.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset