AndroidView v1.0 - Creating a Layout in Java Class

This section provides a tutorial example on how to create a LinearLayout object with 2 Button objects in the activity Java class. Layout resource XML file is not used in this example.

To help playing with Android views, I want to start another new Android application called AndroidView using the "android create project" command:

C:\herong>\local\android-sdk-windows\tools\android create project \
   --package com.herongyang.view --activity AndroidView --target 2 \
   --path .\AndroidView

Created project directory: C:\herong\AndroidView
...
Added file C:\herong\AndroidView\build.xml

In the first version of AndroidView, I want to create a LinearLayout with two buttons:

/* AndroidView.java
 * Version 1.0 - Starting with 1 LinearLayout and 2 buttons
 * 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;
public class AndroidView extends Activity {
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      // Create the layout
      LinearLayout l = new LinearLayout(this);

      // Create the left button
      Button bl = new Button(this);
      bl.setText("Yes");
      l.addView(bl);

      // Create the right button
      Button br = new Button(this);
      br.setText("No");
      l.addView(br);

      // Set the layout as the activity content
      setContentView(l);
   }
}

After building and installing the project, what I see when running AndroidView on the emulator is similar to this:

AndroidView - Using Java Objects
AndroidView - Using Java Objects

The output looks good to me. 1 LinearLayout holding 2 Buttons horizontally.

Notice that in this version of AndroidView, I created the LinearLayout object from the Java program without using the layout resource file. The next tutorial will show you how to create layout objects from the layout resource files.

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

 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

 Archived Tutorials

 References

 Full Version in PDF/EPUB