Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

GridLayout Layout

GridLayout lays out views in a two-dimensional grid pattern, that is, in a series of rows and columns. The intersection of row and column is known as a grid cell, and it is the place where child views are placed. It is easier to use GridLayout when compared to TableLayout. Without specifying intermediate views, we can flexibly place the views randomly in the grid by specifying their row and column positions. More than one view can be placed in a grid cell. Besides this, views can span multiple grid cells too.

Specifying Row and Column Position

The two attributes that are used to specify the row and column position of the grid cell for inserting views are android:layout_row and android:layout_column. Together, they specify the exact location of the grid cell for placing the view. For example, the following statements place the view at the first row and column position of the grid:

android:layout_row="0"
android:layout_column="0"

When either or both of the preceding attributes are not specified, GridLayout uses the next grid cell by default for placing the view.

Spanning Rows and Columns

Views can span rows or columns if desired. The attributes used for doing so are android:layout_rowSpan and android:layout_columnSpan. For example, the following statement spans the view to two rows:

android:layout_rowSpan="2"

Similarly, the following statement spans the view to three columns:

android:layout_columnSpan="3"

Inserting Spaces in the GridLayout

For inserting spaces, a spacing view called Space is used. That is, to insert spaces, the Space view is inserted as a child view. For example, the following statements insert a space at the second row in the GridLayout. The width and height of the blank space are 50dp and 10dp:

<Space
    android:layout_row="1"
    android:layout_column="0"
    android:layout_width="50dp"
    android:layout_height="10dp" />

Similarly, the following statements insert a space at the third row in the GridLayout that spans three columns:

<Space
android:layout_row="3"

android:layout_column="0"
android:layout_columnSpan="3"
android:layout_gravity="fill" />

Let’s apply the knowledge gained so far in arranging controls in a GridLayout. The application has controls arranged in the same way as we saw in TableLayout (see Figure 3.14) but in GridLayout instead. So, let’s create a new Android project called GridLayoutLayoutApp. Make its layout file, activity_grid_layout_app.xml, appear as shown in Listing 3.14.

Listing 3.14. The Layout File activity_grid_layout_app.xml on Arranging Controls in a GridLayout Container

<GridLayout 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="horizontal"
    android:rowCount="7"
    android:columnCount="2" >
    <TextView
        android:layout_row="0"
        android:layout_column="0"
        android:text="New Product Form"
        android:typeface="serif"
        android:layout_columnSpan="2"
        android:layout_gravity="center_horizontal"
        android:textSize="20dip"  />
    <Space
        android:layout_row="1"
        android:layout_column="0"
        android:layout_width="50dp"
        android:layout_height="10dp" />
    <TextView
        android:layout_row="2"
        android:layout_column="0"
        android:text="Product Code:"  />
    <EditText
        android:id="@+id/prod_code"
        android:layout_width="100dip"  />
    <TextView
        android:text="Product Name:"   />
    <EditText
        android:layout_row="3"
        android:layout_column="1"
        android:id="@+id/prod_name"
        android:layout_width="200dip"   />
    <TextView
        android:layout_row="4"
        android:layout_column="0"
        android:text="Product Price:"   />
    <EditText
        android:layout_row="4"
        android:layout_column="1"
        android:id="@+id/prod_price"
        android:layout_width="100dip"  />
    <Space
        android:layout_row="5"
        android:layout_column="0"
        android:layout_width="50dp"
        android:layout_height="20dp" />
    <Button
        android:layout_row="6"
        android:layout_column="0"
        android:id="@+id/add_button"
        android:text="Add Product"   />
    <Button
        android:id="@+id/cancel_button"
        android:text="Cancel"   />
</GridLayout>

In the preceding code, the GridLayout is defined as consisting of seven rows and two columns. The orientation of GridLayout is set to horizontal; that is, controls are placed in rows. It means that while specifying the grid location of a view, if we don’t specify the column number, the next available column is assigned to it. As said earlier, the layout_width and layout_height attributes are not specified for any of the views laid in GridLayout because the default value wrap_content is considered for them. Remember, the row and column numbers are zero-based. In Listing 3.14, the controls are positioned in the grid as follows:

  • A TextView with the text New Product Form is set to appear at the first row and column position of the grid. The text appears in serif font and in 20dip size. The text spans two columns and appears at the center of the row.
  • A blank space is inserted at the second row and first column position. The width and height of the blank space are 50dp and 10dp, respectively.
  • A TextView with the text Product Code: is set to appear at the third row and first column position of the grid.
  • An EditText control with the ID prod_code of width 100dip is set to appear at the third row and second column position of the grid, that is, to the right of the text Product Code:. The question is even though we didn’t specify row and column position for the EditText control, how it will appear at the third row and second column position? The answer is because the orientation of the GridLayout is horizontal, the current row (if it is not full) and the next column (if available) are considered the default location for the control to be inserted.
  • A TextView with the text Product Name: is set to appear at the fourth row and first column position of the grid. Because both columns of the third row are full, the fourth row is considered the location for this view.
  • An EditText control with the ID prod_name of width 200dip is set to appear at the fourth row and second column of the grid, that is, to the right of the text Product Name:.
  • A TextView with the text Product Price: is set to appear at the fifth row and first column of the grid.
  • An EditText control with the ID prod_price of width 100dip is set to appear at the fifth row and second column position of the grid, that is, to the right of the text Product Price:.
  • A blank space is inserted at the sixth row and first column position. The width and height of the blank space are 50dp and 20dp, respectively.
  • A Button control with the caption "Add Product" is set to appear at the seventh row and first column of the grid.
  • A Button control with the caption "Cancel" is set to appear at the seventh row and second column of the grid.

There is no need to write any code in the Java activity file GridLayoutAppActivity.java. When the application is run, the controls are laid out in the grid pattern as shown in Figure 3.15.

Figure 3.15

Figure 3.15. Controls organized in the GridLayout

  • + Share This
  • 🔖 Save To Your Account

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020