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