This tutorial is about to designing a Linear Layout. Linear Layout is a very basic and the default layout of providing the UI in Android.
Linear Layout is a View Group which supply its child Views, either HORIZONTAL or VERTICAL, depends upon the orientation property we set.
Linear Layout is a View Group which supply its child Views, either HORIZONTAL or VERTICAL, depends upon the orientation property we set.
- Create a project against your specified Build Target. (Android SDK level).
- Hence, the layout file linear.xml would be like this:
<?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"
>
</LinearLayout>
To configure a layout, we have main areas control besides the container's content:
- Orientation:
- Orientation indicates whether the LinearLayout represents a row or a column.
- Just add the android:orientation property to LinearLayout element in XML layout, set the value to be horizontal for a row or vertical for a column.
- The orientation can be modified at runtime by invoking setOrientation().
- Fill Model:
- Since, we have so many devices available in the market with various screen sizes, to match the size of the child views with various screen sizes we use Fill Model in android.
- All widgets inside a LinearLayout must supply android:layout_width and android:layout_height properties.
- These properties values have 3 flavors:
- We can provide a specific dimension, such as 200dp to indicate the widget should take up exactly 200dp (dp/dip - Density Independent Pixels).
- We can provide wrap_content, which means the widget should fill up its natural space, unless that is too big, in which case Android can use word-wrap as needed to make it fit.
- We can provide fill_parent, which means the widget should fill up all available space in its enclosing container, after all other widgets are taken care of(means the widgets added after this will not be visible).
Here, in this example I am setting the orientation as vertical. Later, added some widgets (TextViews, EditText, and a Button) to LinearLayout.
After editing the xml file would be like this:
<?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="Hello.! Everyone"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Welcome to Android.."
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="this is an example for Linear layout with vertical orientation"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Please enter your name:"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Save"
/>
</LinearLayout>
Now lets, move on to activities coding. In this example we are not doing anything on activity file.
Hence, the activity file would be like this:
public class LinearLayoutActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
After running the application output would be like this:

No comments:
Post a Comment