Create User Interface Programmatically In Android Applications

Overview

Below are some useful links to start programming in Android Applications.

  1. Android Programming - Day One 
  2. Android Programming - Day Two
  3. How To Display A Dialog Windows In Android
  4. How To Display A Progress Dialog Window In Android
  5. Intent In Android
  6. Return Data Using Intent Object In Android Applications
  7. Passing Data Using An Intent Object In Android Applications 
  8. Fragments In Android Applications
  9. Adding Fragments Dynamically In Android Application 
  10. Fragment Life Cycle In Android Application
  11. Interaction Between Two Fragments
  12. Calling In Built Functions in Android Application
  13. Intent Object and Intent Filters in Android Application 
  14. Displaying Notifications in Android Applications
  15. User Interface in Android Applications
  16. Orientation, Anchoring, Resizing And Repositioning Of Views In Android Application 
  17. Activity Behavior When Screen Orientation Changes in Android Application
  18. Persevere with State Information during Changes in Configuration 
  19. Detect Orientation Changes and Controlling the Orientation of the Activity
  20. Utilizing the Action Bar in Android Application

Introduction

In this article, we will learn to create user interface, using code. So far, we have seen all the UIs, which you have created, using XML file but we can create the user interface programmatically. This is useful, when UI needs to be dynamically generated during runtime if you are creating an app for the air ticket reservation system and your app is supposed to display the seats for each  way's travel, using the buttons. In this case, you will have to dynamically generate the UI code, which is based on the air travel selected by the user.

Implementation

Create a new project by selecting File->New->New Project.


Now, add some elements which are used to create UI in Android, as shown below.

  1. package com.example.administrator.actionbarapp;  
  2.   
  3. import android.app.ActionBar;  
  4. import android.app.Activity;  
  5. import android.support.v7.app.ActionBarActivity;  
  6. import android.os.Bundle;  
  7. import android.view.Menu;  
  8. import android.view.MenuItem;  
  9. import android.view.ViewGroup;  
  10. import android.widget.Button;  
  11. import android.widget.CheckBox;  
  12. import android.widget.EditText;  
  13. import android.widget.LinearLayout;  
  14. import android.widget.RadioButton;  
  15. import android.widget.RadioGroup;  
  16. import android.widget.TextView;  
  17. import android.widget.Toast;  
  18.   
  19.   
  20. public class MainActivity extends Activity {  
  21.   
  22.     @Override  
  23.     protected void onCreate(Bundle savedInstanceState) {  
  24.         //getSupportActionBar().setDisplayHomeAsUpEnabled(false);  
  25.         super.onCreate(savedInstanceState);  
  26.         //setContentView(R.layout.activity_main);  
  27.   
  28.         //Create params for views---------------  
  29.         LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);  
  30.         //Create a layout---------------  
  31.         LinearLayout linearLayout = new LinearLayout(this);  
  32.         linearLayout.setOrientation(LinearLayout.VERTICAL);  
  33.   
  34.         //----Create a TextView------  
  35.         TextView textView = new TextView(this);  
  36.   
  37.         textView.setText("This TextView is dynamically created");  
  38.         textView.setLayoutParams(params);  
  39.   
  40.         //--Create A EditText------------------  
  41.         EditText editText = new EditText(this);  
  42.         editText.setLayoutParams(params);  
  43.   
  44.         //----Create a CheckBox-------------  
  45.         CheckBox checkBox = new CheckBox(this);  
  46.         checkBox.setLayoutParams(params);  
  47.   
  48.         //--- Create a RadioGroup---------------  
  49.         RadioGroup radioGroup = new RadioGroup(this);  
  50.         radioGroup.setLayoutParams(params);  
  51.   
  52.         //--------Create a RadioButton----------  
  53.         RadioButton radioButton = new RadioButton(this);  
  54.         radioButton.setLayoutParams(params);  
  55.   
  56.         //-----Create a Button--------  
  57.         Button button = new Button(this);  
  58.         button.setText("This Button is dynamically created");  
  59.         button.setLayoutParams(params);  
  60.   
  61.         //---Add all elements to the layout  
  62.         linearLayout.addView(textView);  
  63.         linearLayout.addView(checkBox);  
  64.         linearLayout.addView(editText);  
  65.         linearLayout.addView(radioGroup);  
  66.         linearLayout.addView(radioButton);  
  67.   
  68.         linearLayout.addView(button);  
  69.   
  70.         //---Create a layout param for the layout-----------------  
  71.         LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ActionBar.LayoutParams.FILL_PARENT, ActionBar.LayoutParams.WRAP_CONTENT);  
  72.         this.addContentView(linearLayout, layoutParams);  
  73.   
  74.     }  
  75. }   

Explanation

Press F11 to debug the Application on the mobile device or Android emulator. The activity created looks as shown below.

 

If you see the code carefully, we have commented on the code

  1. //setContentView(R.layout.activity_main);  
Hence, the activity will not be loaded from the main.xml file, as it is by default in any Android Application. First, we created a LayoutParams object to specify the layout parameter, which can be used by other views.  
  1. //Create params for views---------------  
  2.  LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);  
Now, create a LinearLayout object to contain all the views in the activity.
  1. //Create a layout---------------  
  2. LinearLayout linearLayout=new LinearLayout(this);  
  3. linearLayout.setOrientation(LinearLayout.VERTICAL);  
Now, you need to create some UI elements like EditText, TextView, Button, CheckBox, RadioGroup and RadioButton etc.
  1. //----Create a TextView------  
  2.  TextView textView=new TextView(this);  
  3.    
  4.  textView.setText("This TextView is dynamically created");  
  5.  textView.setLayoutParams(params);  
  6.    
  7.  //--Create A EditText------------------  
  8.  EditText editText=new EditText(this);  
  9.  editText.setLayoutParams(params);  
  10.    
  11.  //----Create a CheckBox-------------  
  12.  CheckBox checkBox=new CheckBox(this);  
  13.  checkBox.setLayoutParams(params);  
  14.    
  15.  //--- Create a RadioGroup---------------  
  16.  RadioGroup radioGroup=new RadioGroup(this);  
  17.  radioGroup.setLayoutParams(params);  
  18.    
  19.  //--------Create a RadioButton----------  
  20.  RadioButton radioButton=new RadioButton(this);  
  21.  radioButton.setLayoutParams(params);  
  22.    
  23.  //-----Create a Button--------  
  24.  Button button=new Button(this);  
  25.  button.setText("This Button is dynamically created");  
  26.  button.setLayoutParams(params);  
All the elements created above are added to the LinearLayout object.
  1. //---Add all elements to the layout  
  2.  linearLayout.addView(textView);  
  3.  linearLayout.addView(checkBox);  
  4.  linearLayout.addView(editText);  
  5.  linearLayout.addView(radioGroup);  
  6.  linearLayout.addView(radioButton);  
  7.  linearLayout.addView(button);  
Create a LayoutParams object to be used by the LinearLayout object.
  1. //---Create a layout param for the layout-----------------  
  2.  LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(ActionBar.LayoutParams.FILL_PARENT, ActionBar.LayoutParams.WRAP_CONTENT);  
Subsequently, it is added to the activity, as shown below.
  1. this.addContentView(linearLayout,layoutParams);  
Conclusion

In this article, you learned how to create UI dynamically. In the next article, I will explain about how the activities interact with the user.

Up Next
    Ebook Download
    View all
    Learn
    View all