Overview
Below are some useful links to start programming in Android Applications.
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.
- package com.example.administrator.actionbarapp;
-
- import android.app.ActionBar;
- import android.app.Activity;
- import android.support.v7.app.ActionBarActivity;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.ViewGroup;
- import android.widget.Button;
- import android.widget.CheckBox;
- import android.widget.EditText;
- import android.widget.LinearLayout;
- import android.widget.RadioButton;
- import android.widget.RadioGroup;
- import android.widget.TextView;
- import android.widget.Toast;
-
-
- public class MainActivity extends Activity {
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
-
- super.onCreate(savedInstanceState);
-
-
-
- LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
-
- LinearLayout linearLayout = new LinearLayout(this);
- linearLayout.setOrientation(LinearLayout.VERTICAL);
-
-
- TextView textView = new TextView(this);
-
- textView.setText("This TextView is dynamically created");
- textView.setLayoutParams(params);
-
-
- EditText editText = new EditText(this);
- editText.setLayoutParams(params);
-
-
- CheckBox checkBox = new CheckBox(this);
- checkBox.setLayoutParams(params);
-
-
- RadioGroup radioGroup = new RadioGroup(this);
- radioGroup.setLayoutParams(params);
-
-
- RadioButton radioButton = new RadioButton(this);
- radioButton.setLayoutParams(params);
-
-
- Button button = new Button(this);
- button.setText("This Button is dynamically created");
- button.setLayoutParams(params);
-
-
- linearLayout.addView(textView);
- linearLayout.addView(checkBox);
- linearLayout.addView(editText);
- linearLayout.addView(radioGroup);
- linearLayout.addView(radioButton);
-
- linearLayout.addView(button);
-
-
- LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ActionBar.LayoutParams.FILL_PARENT, ActionBar.LayoutParams.WRAP_CONTENT);
- this.addContentView(linearLayout, layoutParams);
-
- }
- }
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
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.
-
- 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.
-
- LinearLayout linearLayout=new LinearLayout(this);
- linearLayout.setOrientation(LinearLayout.VERTICAL);
Now, you need to create some UI elements like EditText, TextView, Button, CheckBox, RadioGroup and RadioButton etc.
-
- TextView textView=new TextView(this);
-
- textView.setText("This TextView is dynamically created");
- textView.setLayoutParams(params);
-
-
- EditText editText=new EditText(this);
- editText.setLayoutParams(params);
-
-
- CheckBox checkBox=new CheckBox(this);
- checkBox.setLayoutParams(params);
-
-
- RadioGroup radioGroup=new RadioGroup(this);
- radioGroup.setLayoutParams(params);
-
-
- RadioButton radioButton=new RadioButton(this);
- radioButton.setLayoutParams(params);
-
-
- Button button=new Button(this);
- button.setText("This Button is dynamically created");
- button.setLayoutParams(params);
All the elements created above are added to the LinearLayout object.
-
- linearLayout.addView(textView);
- linearLayout.addView(checkBox);
- linearLayout.addView(editText);
- linearLayout.addView(radioGroup);
- linearLayout.addView(radioButton);
- linearLayout.addView(button);
Create a LayoutParams object to be used by the LinearLayout object.
-
- LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(ActionBar.LayoutParams.FILL_PARENT, ActionBar.LayoutParams.WRAP_CONTENT);
Subsequently, it is added to the activity, as shown below.
- 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.