1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.example.android.apis.view;
18 
19 import android.app.Activity;
20 import android.content.Context;
21 import android.content.res.Configuration;
22 import android.os.Bundle;
23 import android.view.View;
24 
25 import android.widget.*;
26 
27 import static android.text.InputType.*;
28 import static android.widget.GridLayout.*;
29 import static android.widget.GridLayout.LayoutParams;
30 
31 /**
32  * A form, showing use of the GridLayout API. Here we demonstrate use of the row/column order
33  * preserved property which allows rows and or columns to pass over each other when needed.
34  * The two buttons in the bottom right corner need to be separated from the other UI elements.
35  * This can either be done by separating rows or separating columns - but we don't need
36  * to do both and may only have enough space to do one or the other.
37  */
38 public class GridLayout3 extends Activity {
create(Context context)39     public static View create(Context context) {
40         GridLayout p = new GridLayout(context);
41         p.setUseDefaultMargins(true);
42         p.setAlignmentMode(ALIGN_BOUNDS);
43         Configuration configuration = context.getResources().getConfiguration();
44         if ((configuration.orientation == Configuration.ORIENTATION_PORTRAIT)) {
45             p.setColumnOrderPreserved(false);
46         } else {
47             p.setRowOrderPreserved(false);
48         }
49 
50         Spec titleRow              = spec(0);
51         Spec introRow              = spec(1);
52         Spec emailRow              = spec(2, BASELINE);
53         Spec passwordRow           = spec(3, BASELINE);
54         Spec button1Row            = spec(5);
55         Spec button2Row            = spec(6);
56 
57         Spec centerInAllColumns    = spec(0, 4, CENTER);
58         Spec leftAlignInAllColumns = spec(0, 4, LEFT);
59         Spec labelColumn           = spec(0, RIGHT);
60         Spec fieldColumn           = spec(1, LEFT);
61         Spec defineLastColumn      = spec(3);
62         Spec fillLastColumn        = spec(3, FILL);
63 
64         {
65             TextView c = new TextView(context);
66             c.setTextSize(32);
67             c.setText("Email setup");
68             p.addView(c, new LayoutParams(titleRow, centerInAllColumns));
69         }
70         {
71             TextView c = new TextView(context);
72             c.setTextSize(16);
73             c.setText("You can configure email in a few simple steps:");
74             p.addView(c, new LayoutParams(introRow, leftAlignInAllColumns));
75         }
76         {
77             TextView c = new TextView(context);
78             c.setText("Email address:");
79             p.addView(c, new LayoutParams(emailRow, labelColumn));
80         }
81         {
82             EditText c = new EditText(context);
83             c.setEms(10);
84             c.setInputType(TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
85             p.addView(c, new LayoutParams(emailRow, fieldColumn));
86         }
87         {
88             TextView c = new TextView(context);
89             c.setText("Password:");
90             p.addView(c, new LayoutParams(passwordRow, labelColumn));
91         }
92         {
93             TextView c = new EditText(context);
94             c.setEms(8);
95             c.setInputType(TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_PASSWORD);
96             p.addView(c, new LayoutParams(passwordRow, fieldColumn));
97         }
98         {
99             Button c = new Button(context);
100             c.setText("Manual setup");
101             p.addView(c, new LayoutParams(button1Row, defineLastColumn));
102         }
103         {
104             Button c = new Button(context);
105             c.setText("Next");
106             p.addView(c, new LayoutParams(button2Row, fillLastColumn));
107         }
108 
109         return p;
110     }
111 
onCreate(Bundle savedInstanceState)112     protected void onCreate(Bundle savedInstanceState) {
113         super.onCreate(savedInstanceState);
114         setContentView(create(this));
115     }
116 
117 }