Android :: How to create layout for an activity?

In our previous article relating to Android Programming which was Android :: What is an activity And How To Make One? we had explained what an activity was and how you can make one. In this article we will examine how we can create a layout for an activity.

The layouts in Android are designed using XML files and same files can contain the UI code as well as the structure of the layout. There are various layouts available in android system which let you make interactive screens for your users in your application. The main layouts available in the android system are:

  • FrameLayout
  • LinearLayout
  • RelativeLayout
  • TableLayout
  • AbsoluteLayout

We will cover each and every layout in detail in our future articles for now we will concentrate on the topic of the article i.e. how to make the layout for an activity. To make a layout the first thing you need to create is an xml file which will hold the layout.

Click on File->New->AndroidXML File. You will see a window like below:

 

Add New Layout Android

Add New Layout Android

In the above dialog you will see various options under the Root Element As explained above there are various layouts available in the android system. You can check in above dialog that all these layouts are available. So choose LinearLayout in the list and name the file as main_activity_layout.xml.

You will see the following code in main_acitivity_layout.xml:

 

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:orientation=”vertical” >

</LinearLayout>

In the above file you can add your user elements inside the <LinearLayout> and </LinearLayout> tags. As an example we will add a text view in the same.

To add a text view we will use the following code in the xml file:

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:orientation=”vertical” >

<TextView
android:id=”@+id/editText1″
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:ems=”10″ />

</LinearLayout>

This will add a Text View in the application’s layout. In the same manner you can add as many controls on the layout as needed by your applications. There is also a graphical tool available in eclipse which can be used to arrange the UI elements on the screen.

Finally, we need to attach the XML file to the Activity for which the layout has been created to do the same we will make use of setContentView() function in the activities onCreate function.

The following code will attach the layout’s xml file to the activity.

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity_layout);

}

The layout is now attached to the activity and can be changed as needed by you for your application.

We will keep updating our Android Programming Tips section every 4 days, please subscribe to our feed and also share our articles on Facebook and Google Plus so other’s can benefit from our insight. Please feel free to send us your comments below, on how we can improve.

No comments yet.

Leave a Reply