AndroidView v3.0 - Referencing Views in Resource Files

This section provides a tutorial example on how to identify and reference layouts or views defined the layout resource file. This is needed to mix layouts and views created in Java code with those defined in the resource file.

With AndroidView version 3, I want to try the mixed approach to develop the UI layout, where major UI layouts and views are defined layout resource files and minor ones are created in Java class code.

In order to mix UI layouts and views defined in the layout resource file to those created in the Java code, I need to learn two 2 new features provided in the Android SDK:

1. Assigning a ID to the layout or view defined in the layout resource file. This can be done using the "id" attribute as shown in the following example:

<someView android:id="@+id/someID"/>

2. Obtaining the reference to the layout or view object defined in the layout resource file. This can be done using the findviewById() method as shown in the following example:

findViewById(R.id.someID);

The next question is what layouts and views should be defined in the layout resource file and what should be created in the Java code. The answer depends on the nature of the application. For my AndroidView version 3, here is what I want:

So I enhanced .\src\com\herongyang\view\AndroidView.java as follows:

/* AndroidView.java
 * Version 3.0 - Mixing View objects with Layouts from resource file
 * Copyright (c) 2015, HerongYang.com, All Rights Reserved.
 */
package com.herongyang.view;

import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.Button;
import android.widget.TextView;
public class AndroidView extends Activity {
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      // Set the top layout as the activity content      
      setContentView(R.layout.main);

      // Build detail fields
      LinearLayout l;
      l = (LinearLayout) findViewById(R.id.lContact);
      buildContact(l);
      l = (LinearLayout) findViewById(R.id.lAddress);
      buildAddress(l);
      l = (LinearLayout) findViewById(R.id.lAction);
      buildAction(l);
   }
   public void buildContact(LinearLayout l) {
      TextView v = new TextView(this);
      v.setText("First name: Herong");
      l.addView(v);

      v = new TextView(this);
      v.setText("Last name: Yang");
      l.addView(v);
   }
   public void buildAddress(LinearLayout l) {
      TextView v = new TextView(this);
      v.setText("City: Paris");
      l.addView(v);

      v = new TextView(this);
      v.setText("Country: France");
      l.addView(v);
   }
   public void buildAction(LinearLayout l) {
      Button b = new Button(this);
      b.setText("Submit");
      l.addView(b);

      b = new Button(this);
      b.setText("Cancel");
      l.addView(b);
   }
}

See next tutorials on how to create layout resource file to display the final form. The resource file will define 3 layouts identified as lContact, lAddress and lAction.

Last update: 2015.

Table of Contents

 About This Book

 Installing JDK 1.8 on Windows System

 Installation of Android SDK R24 and Emulator

 Installing Apache Ant 1.9 on Windows System

 Developing First Android Application - HelloAndroid

 Android Application Package (APK) Files

 Android Debug Bridge (adb) Tool

 Android File Systems

 Android 4.0.3 File Systems

 AboutAndroid - Application to Retrieve System Information

 android.app.Activity Class and Activity Lifecycle

View Objects and Layout Resource Files

 android.view.View Class - Base of User Interface Components

 View, ViewGroup, Layout, and Widget

 What Is Layout Resource File?

 AndroidView v1.0 - Creating a Layout in Java Class

 AndroidView v2.0 - Creating a Layout in Resource File

AndroidView v3.0 - Referencing Views in Resource Files

 AndroidView v3.1 - Layouts with Vertical Orientation

 AndroidView v3.2 - Layouts with Horizontal Orientation

 AndroidView v4.0 - Inserting Views to Parent Layout

 Using "adb logcat" Command for Debugging

 Build Process and Package File Content

 Building Your Own Web Browser

 Android Command Line Shell

 Samsung Galaxy Tab 3 Mini Tablet

 USB Debugging Applications on Samsung Tablet

 Android Tablet - LG-V905R

 USB Debugging Applications on LG-V905R Tablet

 Android Phone - LG-P925g

 USB Debugging Applications on LG-P925g Phone

 Outdated Tutorials

 References

 PDF Printing Version