Thursday, February 15, 2018

Layouts In Android

As discussed in the previous chapter that layouts are used to create User Interface for our Android Applications and inflated in the Activity class using setContentView() method
The basic building blocks of Android user interface is View. Each views  has attributes/properties. Attributes defines the look of the views. Some of the important and  frequently used views in Android are Layout, TextView, EditText, Button,CheckBoxes, Radio Buttons, Progress bar etc. How to use these views and how to handle the click and other events on these views will be discussed in detail in next chapter.
In this chapter we will discuss about Layout and their types.
Before going through the layouts we must understand some of the basic and important attributes of layout and views.

Attribute
Description
android:id
This is the ID which uniquely identifies the view.
android:layout_width
This is the width of the view.
android:layout_height
This is the height of the view
android:layout_marginTop
This is the extra space on the top side of the view.
android:layout_marginBottom
This is the extra space on the bottom side of the view.
android:layout_marginLeft
This is the extra space on the left side of the view.
android:layout_marginRight
This is the extra space on the right side of the view.
android:layout_gravity
This specifies how child Views are positioned.
android:layout_weight
This specifies how much of the extra space in the layout should be allocated to the View.


LinearLayout
A Linear  Layout  arranges all  its children in  vertical or horizontal direction, this direction is is given by      android:orientation  attribute.   There are two possible values for this attrinute , vertical and horizontal.
Lets discuss each of them separately.
Vertical Linear Layout
In vertical layouts children are arranged in vertical direction.
Example:
Create a new layout inside layout folder with name main.xml

<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
       android:layout_marginTop="30dp">
   

              <TextView
                     android:id="@+id/textView1"
              android:layout_gravity="center_horizontal"
              android:layout_marginTop="30dp"
              android:layout_marginBottom="25dp"
              android:textSize="25dp"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="This Is Vertical Orientation" />


              <Button
              android:id="@+id/button1"
                     android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:text="Button 1" />


              <Button
              android:id="@+id/button2"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:text="Button 2" />

              <Button
              android:id="@+id/button3"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:text="Button 3" />

              <Button
              android:id="@+id/button4"
                  android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:text="Button 4" />

</LinearLayout>



Now inflate this layout main.xml in your  Activity class

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
          
        // Inflate the layout
        setContentView(R.layout.main);
    }

}





                                                   




You can see all child views(1 textview and 4 buttons) are placed vertically in the scrren..










No comments:

Post a Comment