AndroidView v4.0 - Inserting Views to Parent Layout

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.

As the last tutorial on View objects, I am going to follow the view tree to find the parent layout and insert a DatePicker view in the Java code:

/* AndroidView.java
 * Version 4.0 - Inserting views into the parent layout
 * Copyright (c) 2012, 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;
import android.widget.DatePicker;
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);

      // Insert a new child to the parent
      LinearLayout p = (LinearLayout) l.getParent();
      addDatePicker(p);
   }
   public void addDatePicker(LinearLayout l) {
      DatePicker v = new DatePicker(this);
      l.addView(v);
   }
   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);
   }
}

Rebuild, install and run the application again on the emulator. I see this on the emulator:

AndroidView - Date Picker
AndroidView - Date Picker

It works! I am able to use the getParent() method in the view tree to get hold of the parent layout and insert a DatePicker view into the layout.

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