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
17package com.example.android.supportv7.view;
18
19import static android.text.InputType.TYPE_CLASS_TEXT;
20import static android.text.InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS;
21import static android.text.InputType.TYPE_TEXT_VARIATION_PASSWORD;
22
23import static androidx.gridlayout.widget.GridLayout.ALIGN_BOUNDS;
24import static androidx.gridlayout.widget.GridLayout.BASELINE;
25import static androidx.gridlayout.widget.GridLayout.CENTER;
26import static androidx.gridlayout.widget.GridLayout.FILL;
27import static androidx.gridlayout.widget.GridLayout.LEFT;
28import static androidx.gridlayout.widget.GridLayout.RIGHT;
29import static androidx.gridlayout.widget.GridLayout.spec;
30
31import android.app.Activity;
32import android.content.Context;
33import android.content.res.Configuration;
34import android.os.Bundle;
35import android.view.View;
36import android.widget.Button;
37import android.widget.EditText;
38import android.widget.TextView;
39
40import androidx.gridlayout.widget.GridLayout;
41import androidx.gridlayout.widget.GridLayout.LayoutParams;
42import androidx.gridlayout.widget.GridLayout.Spec;
43
44/**
45 * A form, showing use of the GridLayout API. Here we demonstrate use of the row/column order
46 * preserved property which allows rows and or columns to pass over each other when needed.
47 * The two buttons in the bottom right corner need to be separated from the other UI elements.
48 * This can either be done by separating rows or separating columns - but we don't need
49 * to do both and may only have enough space to do one or the other.
50 */
51public class GridLayout3 extends Activity {
52    public static View create(Context context) {
53        GridLayout p = new GridLayout(context);
54        p.setUseDefaultMargins(true);
55        p.setAlignmentMode(ALIGN_BOUNDS);
56        Configuration configuration = context.getResources().getConfiguration();
57        if ((configuration.orientation == Configuration.ORIENTATION_PORTRAIT)) {
58            p.setColumnOrderPreserved(false);
59        } else {
60            p.setRowOrderPreserved(false);
61        }
62
63        Spec titleRow              = spec(0);
64        Spec introRow              = spec(1);
65        Spec emailRow              = spec(2, BASELINE);
66        Spec passwordRow           = spec(3, BASELINE);
67        Spec button1Row            = spec(5);
68        Spec button2Row            = spec(6);
69
70        Spec centerInAllColumns    = spec(0, 4, CENTER);
71        Spec leftAlignInAllColumns = spec(0, 4, LEFT);
72        Spec labelColumn           = spec(0, RIGHT);
73        Spec fieldColumn           = spec(1, LEFT);
74        Spec defineLastColumn      = spec(3);
75        Spec fillLastColumn        = spec(3, FILL);
76
77        {
78            TextView c = new TextView(context);
79            c.setTextSize(32);
80            c.setText("Email setup");
81            p.addView(c, new LayoutParams(titleRow, centerInAllColumns));
82        }
83        {
84            TextView c = new TextView(context);
85            c.setTextSize(16);
86            c.setText("You can configure email in a few simple steps:");
87            p.addView(c, new LayoutParams(introRow, leftAlignInAllColumns));
88        }
89        {
90            TextView c = new TextView(context);
91            c.setText("Email address:");
92            p.addView(c, new LayoutParams(emailRow, labelColumn));
93        }
94        {
95            EditText c = new EditText(context);
96            c.setEms(10);
97            c.setInputType(TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
98            p.addView(c, new LayoutParams(emailRow, fieldColumn));
99        }
100        {
101            TextView c = new TextView(context);
102            c.setText("Password:");
103            p.addView(c, new LayoutParams(passwordRow, labelColumn));
104        }
105        {
106            EditText c = new EditText(context);
107            c.setEms(8);
108            c.setInputType(TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_PASSWORD);
109            p.addView(c, new LayoutParams(passwordRow, fieldColumn));
110        }
111        {
112            Button c = new Button(context);
113            c.setText("Manual setup");
114            p.addView(c, new LayoutParams(button1Row, defineLastColumn));
115        }
116        {
117            Button c = new Button(context);
118            c.setText("Next");
119            p.addView(c, new LayoutParams(button2Row, fillLastColumn));
120        }
121
122        return p;
123    }
124
125    @Override
126    protected void onCreate(Bundle savedInstanceState) {
127        super.onCreate(savedInstanceState);
128        setContentView(create(this));
129    }
130
131}