1/*
2 * Copyright (C) 2007 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 android.widget.focus;
18
19import android.app.Activity;
20import android.content.Context;
21import android.os.Bundle;
22import android.view.View;
23import android.view.ViewGroup;
24import android.widget.*;
25import com.google.android.collect.Lists;
26
27import java.util.List;
28
29public class ListOfEditTexts extends Activity {
30
31    private int mLinesPerEditText = 12;
32
33    private ListView mListView;
34    private LinearLayout mLinearLayout;
35
36    public ListView getListView() {
37        return mListView;
38    }
39
40    @Override
41    protected void onCreate(Bundle icicle) {
42        super.onCreate(icicle);
43
44        // create linear layout
45        mLinearLayout = new LinearLayout(this);
46        mLinearLayout.setOrientation(LinearLayout.VERTICAL);
47        mLinearLayout.setLayoutParams(new ViewGroup.LayoutParams(
48                ViewGroup.LayoutParams.MATCH_PARENT,
49                ViewGroup.LayoutParams.MATCH_PARENT));
50
51        // add a button above
52        Button buttonAbove = new Button(this);
53        buttonAbove.setLayoutParams(
54                new LinearLayout.LayoutParams(
55                        ViewGroup.LayoutParams.MATCH_PARENT,
56                        ViewGroup.LayoutParams.WRAP_CONTENT));
57        buttonAbove.setText("button above list");
58        mLinearLayout.addView(buttonAbove);
59
60        // add a list view to it
61        mListView = new ListView(this);
62        mListView.setLayoutParams(new ViewGroup.LayoutParams(
63                ViewGroup.LayoutParams.MATCH_PARENT,
64                ViewGroup.LayoutParams.MATCH_PARENT));
65        mListView.setDrawSelectorOnTop(false);
66        mListView.setItemsCanFocus(true);
67        mListView.setLayoutParams((new LinearLayout.LayoutParams(
68                ViewGroup.LayoutParams.MATCH_PARENT,
69                0,
70                1f)));
71
72        List<String> bodies = Lists.newArrayList(
73                getBody("zero hello, my name is android"),
74                getBody("one i'm a paranoid android"),
75                getBody("two i robot.  huh huh."),
76                getBody("three not the g-phone!"));
77
78        mListView.setAdapter(new MyAdapter(this, bodies));
79        mLinearLayout.addView(mListView);
80
81        // add button below
82        Button buttonBelow = new Button(this);
83        buttonBelow.setLayoutParams(
84                new LinearLayout.LayoutParams(
85                        ViewGroup.LayoutParams.MATCH_PARENT,
86                        ViewGroup.LayoutParams.WRAP_CONTENT));
87        buttonBelow.setText("button below list");
88        mLinearLayout.addView(buttonBelow);
89
90        setContentView(mLinearLayout);
91    }
92
93    String getBody(String line) {
94        StringBuilder sb = new StringBuilder((line.length() + 5) * mLinesPerEditText);
95        for (int i = 0; i < mLinesPerEditText; i++) {
96            sb.append(i + 1).append(' ').append(line);
97            if (i < mLinesPerEditText - 1) {
98                sb.append('\n'); // all but last line
99            }
100        }
101        return sb.toString();
102    }
103
104
105    private static class MyAdapter extends ArrayAdapter<String> {
106
107        public MyAdapter(Context context, List<String> bodies) {
108            super(context, 0, bodies);
109        }
110
111        @Override
112        public View getView(int position, View convertView, ViewGroup parent) {
113            String body = getItem(position);
114
115            if (convertView != null) {
116                ((EditText) convertView).setText(body);
117                return convertView;
118            }
119
120            EditText editText = new EditText(getContext());
121            editText.setText(body);
122            return editText;
123        }
124    }
125}
126