UIByProgramExample In Android Development

This example will show you how to design UI through code.

Algorithm:

1.) Create a new project by File-> New -> Android Project name it UIByProgramExample.

2.) Write following into main.xml:

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>

 

3.) Run for output.

Steps:

1.) Create a project named UIByProgramExample and set the information as stated in the image.

Build Target: Android 4.2
Application Name: UIByProgramExample
Package Name: com. UIByProgramExample
Activity Name: UIByProgramExample
Min SDK Version: 4.2

2.) Open UIByProgramExample.java file and write following code there:

 

package com.example.uibyprogramexample;

import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;

public class UIByProgramExample extends Activity {

LinearLayout lLayout;
TextView tView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

lLayout = new LinearLayout(this);
lLayout.setOrientation(LinearLayout.VERTICAL);
lLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
tView = new TextView(this);
tView.setText("Hello, This is a view created programmatically! " +
"You CANNOT change it easily :-)");
tView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
lLayout.addView(tView);
setContentView(lLayout);
}
}

 

 

3.) Compile and build the project.

Leave a Comment:

1 comment
Tanner Glover says May 24, 2013

Android is open source so you don’t have to pay any license fee for its development. Similarly there is no restriction to imposed.so its nice thing to running this type of courses.

Reply
Add Your Reply