Wednesday, May 8, 2013

Nested Linear Layout In Android

In the previous post  we worked on Simple Linear Layout. We saw that there can be two orientation Vertical and Horizontal in which Views/Widgets can be arranged.

In this post we will use both the Orientations Vertical and Horizontal  to nest the Layout.
By nesting the Layout we can make better and finer GUI.

We want  following Layout

Name:                EditText
Password:           EditText
Login Button    Cancel Button


The parent Layout has Vertical Orientation and Children Layouts have Horizontal Orientation.





main.xml

<!--   Parent Layout With Vertical Orientation -->
<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">
 
  <!--  1st Child Layout  (Nested In Parent Layout) with Horizontal Orientation -->

    <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="wrap_content"
    android:orientation="horizontal">

  <TextView
    android:id="@+id/textViewName"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="30dp"
    android:layout_marginBottom="25dp"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Name" />

  <EditText
      android:id="@+id/editText2"
      android:layout_weight="1"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:ems="10" />
 
   </LinearLayout>
 
    <!--  2nd Child Layout  (Nested In Parent Layout) with Horizontal Orientation -->
   <LinearLayout mlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

  <TextView
    android:id="@+id/textViewPassword"
    b
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="30dp"
    android:layout_marginBottom="25dp"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Password" />

  <EditText
      android:id="@+id/editTextPassword"
      android:layout_weight="1"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:ems="10"
      android:inputType="textPassword" />
 
    </LinearLayout>

  <!--  3rd Child Layout  (Nested In Parent Layout) with Horizontal Orientation -->
    <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="wrap_content"
        android:orientation="horizontal">
 
<Button
    android:id="@+id/button1"
    android:layout_weight="1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Login" />


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

    </LinearLayout>


</LinearLayout>


Thing to Remember here is

android:layout_weight       the attribute specifies how much weight/space  the view will have or acquire on parent.

In Above example
android:layout_weight is 1   in both button so they have acquired equal space

Now give 1 weight to first button and 2 to second button.
See the effect




 <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="wrap_content"
        android:orientation="horizontal">
   
<Button
    android:id="@+id/button1"
    android:layout_weight="1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Login" />


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

    </LinearLayout>




s

 

Advance Android Topics


                   Customizing Toast In Android 
                   Showing Toast for Longer Time
                   Customizing the Display Time of Toast
                   Using TimePickerDialog and DatePickerDialog In android
                   Animating A Button In Android
                    Populating ListView With DataBase

                    Customizing Checkboxes In Android 
                    Increasin Size of Checkboxes
                    Android ProgressBar
                    Designing For Different Screen Sizes
                    Handling Keyboard Events

More Android Topics:



  

Android : Introduction


       Eclipse Setup for Android Development

                     Configuring Eclipse for Android Development

          Begging With Android

                     Creating Your First Android Project
                     Understanding Android Manifest File of your android app


         Working With Layouts

                      Understanding Layouts in Android
                          Working with Linear Layout (With Example)
                                Nested Linear Layout (With Example)
                          Table Layout
                          Frame Layout(With Example)
                         Absolute Layout
                         Grid Layout


       Activity

                     Activity In Android
                     Activity Life Cycle
                     Starting Activity For Result
                     Sending Data from One Activity to Other in Android
                     Returning Result from Activity

     Working With Views

                     Using Buttons and EditText in Android 
                     Using CheckBoxes in Android 
                     Using AutoCompleteTextView in Android
                     Grid View

       Toast

                     Customizing Toast In Android
                     Customizing the Display Time of Toast
                     Customizing Toast At Runtime
                     Adding Image in Toast
                     Showing Toast for Longer Time

     Dialogs In Android

                     Working With Alert Dialog
                     Adding Radio Buttons In Dialog
                     Adding Check Boxes In Dialog
                     Creating Customized Dialogs in Android
                    Adding EditText in Dialog

                   Creating Dialog To Collect User Input

                 DatePicker and TimePickerDialog

                              Using TimePickerDialog and DatePickerDialog In android

    Working With SMS

                  How to Send SMS in Android
                  How To Receive SMS
                  Accessing Inbox In Android

    ListView:

               Populating ListView With DataBase

      Menus In Android

                    Creating Option Menu
                    Creating Context Menu In Android

      TelephonyManager

                    Using Telephony Manager In Android

     Working With Incoming Calls

                    How To Handle Incoming Calls in Android
                    How to Forward an Incoming Call In Android
                   CALL States In Android
 

    Miscellaneous

                   Notifications In Android
                   How To Vibrate The Android Phone
                   Sending Email In Android
                  Opening a webpage In Browser
                   How to Access PhoneBook In Android
                   Prompt User Input with an AlertDialog

   Storage:  Storing Data In Android


               Shared Prefferences  In Android

                             SharedPreferences In Android

               Files: File Handling In Android

                              Reading and Writing files to Internal Stoarage
                              Reading and Writing files to SD Card 
                           

                DataBase : Working With Database

                             Working With Database in Android
                             Creating Table In Android
                             Inserting, Deleting and Updating Records In Table in Android
                             How to Create DataBase in Android
                             Accessing Inbox In Android

     Animation In Android:

                  Animating A Button In Android





1 comment: