Home | History | Annotate | Download | only in util
      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 
     17 package android.util;
     18 
     19 import android.content.Context;
     20 import android.view.Gravity;
     21 import android.view.View;
     22 import android.view.ViewGroup;
     23 import android.widget.AbsListView;
     24 import android.widget.Button;
     25 import android.widget.LinearLayout;
     26 import android.widget.TextView;
     27 
     28 /**
     29  * Reusable methods for creating more complex list items.
     30  */
     31 public class ListItemFactory {
     32 
     33     /**
     34      * Create a view with a button at the top and bottom, with filler in between.
     35      * The filler is sized to take up any space left over within desiredHeight.
     36      *
     37      * @param position      The position within the list.
     38      * @param context       The context.
     39      * @param desiredHeight The desired height of the entire view.
     40      * @return The created view.
     41      */
     42     public static View twoButtonsSeparatedByFiller(int position, Context context, int desiredHeight) {
     43         if (desiredHeight < 90) {
     44             throw new IllegalArgumentException("need at least 90 pixels of height " +
     45                     "to create the two buttons and leave 10 pixels for the filler");
     46         }
     47 
     48         final LinearLayout ll = new LinearLayout(context);
     49         ll.setOrientation(LinearLayout.VERTICAL);
     50 
     51         final LinearLayout.LayoutParams buttonLp =
     52                 new LinearLayout.LayoutParams(
     53                         ViewGroup.LayoutParams.MATCH_PARENT,
     54                         50);
     55 
     56         final Button topButton = new Button(context);
     57         topButton.setLayoutParams(
     58                 buttonLp);
     59         topButton.setText("top (position " + position + ")");
     60         ll.addView(topButton);
     61 
     62         final TextView middleFiller = new TextView(context);
     63         middleFiller.setLayoutParams(new LinearLayout.LayoutParams(
     64                 ViewGroup.LayoutParams.MATCH_PARENT,
     65                 desiredHeight - 100));
     66         middleFiller.setText("filler");
     67         ll.addView(middleFiller);
     68 
     69         final Button bottomButton = new Button(context);
     70         bottomButton.setLayoutParams(buttonLp);
     71         bottomButton.setText("bottom (position " + position + ")");
     72         ll.addView(bottomButton);
     73         ll.setTag("twoButtons");
     74         return ll;
     75     }
     76 
     77     public enum Slot {
     78         Left,
     79         Middle,
     80         Right
     81     }
     82 
     83     /**
     84      * Create a horizontal linear layout divided into thirds (with some margins
     85      * separating the thirds), filled with buttons into some slots.
     86      * @param context The context.
     87      * @param desiredHeight The height of the LL.
     88      * @param slots Which slots to fill with buttons.
     89      * @return The linear layout.
     90      */
     91     public static View horizontalButtonSlots(Context context, int desiredHeight, Slot... slots) {
     92 
     93         final LinearLayout ll = new LinearLayout(context);
     94         ll.setOrientation(LinearLayout.HORIZONTAL);
     95 
     96         final LinearLayout.LayoutParams lp
     97                 = new LinearLayout.LayoutParams(0, desiredHeight);
     98         lp.setMargins(10, 0, 10, 0);
     99         lp.weight = 0.33f;
    100 
    101         boolean left = false;
    102         boolean middle = false;
    103         boolean right = false;
    104         for (Slot slot : slots) {
    105             switch (slot) {
    106                 case Left:
    107                     left = true;
    108                     break;
    109                 case Middle:
    110                     middle = true;
    111                     break;
    112                 case Right:
    113                     right = true;
    114                     break;
    115             }
    116         }
    117 
    118         if (left) {
    119             final Button button = new Button(context);
    120             button.setText("left");
    121             ll.addView(button, lp);
    122         } else {
    123            ll.addView(new View(context), lp);
    124         }
    125 
    126         if (middle) {
    127             final Button button = new Button(context);
    128             button.setText("center");
    129             ll.addView(button, lp);
    130         } else {
    131            ll.addView(new View(context), lp);
    132         }
    133 
    134         if (right) {
    135             final Button button = new Button(context);
    136             button.setText("right");
    137             ll.addView(button, lp);
    138         } else {
    139            ll.addView(new View(context), lp);
    140         }
    141 
    142         return ll;
    143     }
    144 
    145 
    146     /**
    147      * Create a button ready to be a list item.
    148      *
    149      * @param position      The position within the list.
    150      * @param context       The context.
    151      * @param text          The text of the button
    152      * @param desiredHeight The desired height of the button
    153      * @return The created view.
    154      */
    155     public static View button(int position, Context context, String text, int desiredHeight) {
    156         TextView result = new Button(context);
    157         result.setHeight(desiredHeight);
    158         result.setText(text);
    159         final ViewGroup.LayoutParams lp = new AbsListView.LayoutParams(
    160                 ViewGroup.LayoutParams.MATCH_PARENT,
    161                 ViewGroup.LayoutParams.WRAP_CONTENT);
    162         result.setLayoutParams(lp);
    163         result.setId(position);
    164         result.setTag("button");
    165         return result;
    166     }
    167 
    168     /**
    169      * Convert an existing button view to display the data at a new position.
    170      *
    171      * @param convertView Non-null Button created by {@link #button}
    172      * @param text The text of the button
    173      * @param position The position withion the list
    174      * @return The converted view
    175      */
    176     public static View convertButton(View convertView, String text, int position) {
    177         if (((String) convertView.getTag()).equals("button")) {
    178             ((Button) convertView).setText(text);
    179             convertView.setId(position);
    180             return convertView;
    181         } else {
    182             return null;
    183         }
    184     }
    185 
    186     /**
    187      * Create a text view ready to be a list item.
    188      *
    189      * @param position      The position within the list.
    190      * @param context       The context.
    191      * @param text          The text to display
    192      * @param desiredHeight The desired height of the text view
    193      * @return The created view.
    194      */
    195     public static View text(int position, Context context, String text, int desiredHeight) {
    196         TextView result = new TextView(context);
    197         result.setHeight(desiredHeight);
    198         result.setText(text);
    199         final ViewGroup.LayoutParams lp = new AbsListView.LayoutParams(
    200                 ViewGroup.LayoutParams.MATCH_PARENT,
    201                 ViewGroup.LayoutParams.WRAP_CONTENT);
    202         result.setLayoutParams(lp);
    203         result.setId(position);
    204         result.setTag("text");
    205         return result;
    206     }
    207 
    208     /**
    209      * Convert an existing text view to display the data at a new position.
    210      *
    211      * @param convertView Non-null TextView created by {@link #text}
    212      * @param text The text to display
    213      * @param position The position withion the list
    214      * @return The converted view
    215      */
    216     public static View convertText(View convertView, String text, int position) {
    217         if(convertView.getTag() != null && ((String) convertView.getTag()).equals("text")) {
    218             ((TextView) convertView).setText(text);
    219             convertView.setId(position);
    220             return convertView;
    221 
    222         } else {
    223             return null;
    224         }
    225     }
    226 
    227     /**
    228      * Create a text view ready to be a list item.
    229      *
    230      * @param position      The position within the list.
    231      * @param context       The context.
    232      * @param text          The text of the button
    233      * @param desiredHeight The desired height of the button
    234      * @return The created view.
    235      */
    236     public static View doubleText(int position, Context context, String text, int desiredHeight) {
    237         final LinearLayout ll = new LinearLayout(context);
    238         ll.setOrientation(LinearLayout.HORIZONTAL);
    239 
    240         final AbsListView.LayoutParams lp =
    241                 new AbsListView.LayoutParams(
    242                         ViewGroup.LayoutParams.MATCH_PARENT,
    243                         desiredHeight);
    244         ll.setLayoutParams(lp);
    245         ll.setId(position);
    246 
    247         TextView t1 = new TextView(context);
    248         t1.setHeight(desiredHeight);
    249         t1.setText(text);
    250         t1.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
    251         final ViewGroup.LayoutParams lp1 = new LinearLayout.LayoutParams(
    252                 0,
    253                 ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f);
    254         ll.addView(t1, lp1);
    255 
    256         TextView t2 = new TextView(context);
    257         t2.setHeight(desiredHeight);
    258         t2.setText(text);
    259         t2.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
    260         final ViewGroup.LayoutParams lp2 = new LinearLayout.LayoutParams(
    261                 0,
    262                 ViewGroup.LayoutParams.WRAP_CONTENT,
    263                 1.0f);
    264 
    265         ll.addView(t2, lp2);
    266         ll.setTag("double");
    267         return ll;
    268     }
    269 
    270 
    271     /**
    272      * Convert an existing button view to display the data at a new position.
    273      *
    274      * @param convertView Non-null view created by {@link #doubleText}
    275      * @param text The text of the button
    276      * @param position The position withion the list
    277      * @return The converted view
    278      */
    279     public static View convertDoubleText(View convertView, String text, int position) {
    280         if (((String) convertView.getTag()).equals("double")) {
    281             TextView t1 = (TextView) ((LinearLayout) convertView).getChildAt(0);
    282             TextView t2 = (TextView) ((LinearLayout) convertView).getChildAt(1);
    283             t1.setText(text);
    284             t2.setText(text);
    285             convertView.setId(position);
    286             return convertView;
    287         } else {
    288             return null;
    289         }
    290     }
    291 }
    292