• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 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.android.contacts.common.list;
18 
19 import android.content.Context;
20 import android.content.res.TypedArray;
21 import android.graphics.Color;
22 import android.text.TextUtils;
23 import android.util.AttributeSet;
24 import android.view.Gravity;
25 import android.view.View;
26 import android.view.ViewParent;
27 import android.widget.LinearLayout.LayoutParams;
28 import android.widget.TextView;
29 
30 import com.android.contacts.common.R;
31 import com.android.contacts.common.util.ViewUtil;
32 
33 /**
34  * A custom view for the pinned section header shown at the top of the contact list.
35  */
36 public class ContactListPinnedHeaderView extends TextView {
37 
ContactListPinnedHeaderView(Context context, AttributeSet attrs, View parent)38     public ContactListPinnedHeaderView(Context context, AttributeSet attrs, View parent) {
39         super(context, attrs);
40 
41         if (R.styleable.ContactListItemView == null) {
42             return;
43         }
44         TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.ContactListItemView);
45         int backgroundColor = a.getColor(
46                 R.styleable.ContactListItemView_list_item_background_color, Color.WHITE);
47         int textOffsetTop = a.getDimensionPixelSize(
48                 R.styleable.ContactListItemView_list_item_text_offset_top, 0);
49         int paddingStartOffset = a.getDimensionPixelSize(
50                 R.styleable.ContactListItemView_list_item_padding_left, 0);
51         int textWidth = getResources().getDimensionPixelSize(
52                 R.dimen.contact_list_section_header_width);
53         int widthIncludingPadding = paddingStartOffset + textWidth;
54         a.recycle();
55 
56         setBackgroundColor(backgroundColor);
57         setTextAppearance(getContext(), R.style.SectionHeaderStyle);
58         setLayoutParams(new LayoutParams(widthIncludingPadding, LayoutParams.WRAP_CONTENT));
59         setLayoutDirection(parent.getLayoutDirection());
60         setGravity(Gravity.CENTER_VERTICAL |
61                 (ViewUtil.isViewLayoutRtl(this) ? Gravity.RIGHT : Gravity.LEFT));
62 
63         // Apply text top offset. Multiply by two, because we are implementing this by padding for a
64         // vertically centered view, rather than adjusting the position directly via a layout.
65         setPaddingRelative(
66                 getPaddingStart() + paddingStartOffset,
67                 getPaddingTop() + (textOffsetTop * 2),
68                 getPaddingEnd(),
69                 getPaddingBottom());
70     }
71 
72     /**
73      * Sets section header or makes it invisible if the title is null.
74      */
setSectionHeaderTitle(String title)75     public void setSectionHeaderTitle(String title) {
76         if (!TextUtils.isEmpty(title)) {
77             setText(title);
78             setVisibility(View.VISIBLE);
79         } else {
80             setVisibility(View.GONE);
81         }
82     }
83 }
84