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:

 * Version 4.0 - Inserting views into the parent layout
 * Copyright (c) 2012,, All Rights Reserved.
package com.herongyang.view;

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 {
   public void onCreate(Bundle savedInstanceState) {

      // Set the top layout as the activity content

      // Build detail fields
      LinearLayout l;
      l = (LinearLayout) findViewById(;
      l = (LinearLayout) findViewById(;
      l = (LinearLayout) findViewById(;

      // Insert a new child to the parent
      LinearLayout p = (LinearLayout) l.getParent();
   public void addDatePicker(LinearLayout l) {
      DatePicker v = new DatePicker(this);
   public void buildContact(LinearLayout l) {
      TextView v = new TextView(this);
      v.setText("First name: Herong");

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

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

      b = new Button(this);

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.

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

 AboutAndroid - Application to Retrieve System Information 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

 Archived Tutorials


 Full Version in PDF/EPUB