Listing 36 First portion of Review Detail showing multiple uses of the R class

public class ReviewDetail extends Activity {

private static final int MENU_CALL_REVIEW = Menu. FIRST + 2; private static final int MENU_MAP_REVIEW = Menu. FIRST + 1; private static final int MENU_WEB_REVIEW = Menu. FIRST;

private String imageLink; private String link; private TextView location;

private TextView name; O Define inflatable private TextView phone; \ View items private TextView rating; private TextView review; private ImageView reviewlmage; C Use Handler private Handler handler = new Handler () { <-1 to get image public void handleMessage(Message msg) {

if ((imageLink != null) && !imageLink.equals("")) { try {

URL url = new URL (imageLink) ; URLConnection conn = url.openConnection(); conn.connect(); BufferedlnputStream bis = new BufferedInputStream(conn.getInputStream());

Bitmap bm = BitmapFactory.decodeStream(bis); bis.close();

reviewImage.setImageBitmap(bm); } catch (IOException e) { / / log and or handle here


@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState) ;

this.setContentView(R.layout.review_detail) ;

D Set layout using <_I setContentView()

E Inflate views using findViewById() =

(TextView) findViewById(; this.rating =

(TextView) findViewById(; this.location =

(TextView) findViewById(; =

(TextView) findViewById(; =

(TextView) findViewById(; this.reviewImage =

(ImageView) findViewById(;

RestaurantFinderApplication application =

(RestaurantFinderApplication) getApplication(); Review currentReview = application.getCurrentReview(); =; this.imageLink = currentReview.imageLink;; this.rating.setText(currentReview.rating); this.location.setText(currentReview.location);;

if (( != null) && !"")) {; } else {"NA");

@Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); menu . add ( 0 , ReviewDetail. MENU_WEB_REVIEW, 0 , R.string. menu_web_review) . setIcon (

android.R.drawable.ic_menu_info_details); menu . add ( 0 , ReviewDetail. MENU_MAP_REVIEW, 1, R.string.menu_map_review).setIcon(

android.R.drawable.ic_menu_mapmode); menu . add ( 0 , ReviewDetail. MENU_CALL_REVIEW, 2, R.string.menu_call_review).setIcon(

android.R.drawable.ic_menu_call); return true;

<—A Use String and Drawable

. remainder of this class is in Chapter 4, when we discuss Intents resources

In the ReviewDetail class we are first defining View components that we will later reference from resources Q. From there we see a Handler that is used to perform a network call to populate an ImageView based on a URL. This doesn't relate to resources but is included here for completeness. Don't worry too much about the details of this here, as it will be covered more when we specifically discuss networking in chapter 5 ©. After the Handler, we set the layout and View tree using setContentView(R.layout.review_ detail) Q. This maps to an XML layout file at src/res/layout/review_detail.xml. Next we also reference some of the View objects in the layout file directly through resources and corresponding IDs ©.

Views that are defined in XML are inflated by parsing the XML and injecting the corresponding code to create the objects for you. This is handled automatically by the platform. All of the View and LayoutParams methods we have discussed previously have counterpart attributes in the XML format. This inflation approach is one of the most important aspects of View-related resources, and it makes them very convenient to use and reuse. We will examine the layout file we are referring to here and the specific views it contains more closely in the next section.

You reference resources in code, such as we are here, through the automatically generated R class. The R class is made up of static inner classes (one for each resource type) that hold references to all of your resources in the form of an int value. This value is a constant pointer to an object file through a resource table (which is contained in a special file the aapt tool creates and the R file utilizes).

The last reference to resources in listing 3.6 is for the creation of our menu items Q. For each of these we are referencing a String for text from our own local resources, and we are also assigning an icon from the android.R.drawable resources namespace. You can qualify resources in this way and reuse the platform drawables: icons, images, borders, backgrounds, and so on. You will likely want to customize much of your own applications and provide your own drawable resources, which you can do, but the platform resources are also available if you need them (and they are arguably the better choice in terms of consistency for the user, if you are calling out to well-defined actions as we are here: map, phone call, and web page).

We will cover how all the different resource types are handled and where they are placed in source in the next several sections. The first types of resources we will look at more closely are those of layouts and views.

0 0

Post a comment