blob: e65dea08fcbe525648cb8073595432d9da6bad5e [file] [log] [blame]
Chiao Cheng89437e82012-11-01 13:41:51 -07001/*
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
Gary Mai69c182a2016-12-05 13:07:03 -080017package com.android.contacts.list;
Chiao Cheng89437e82012-11-01 13:41:51 -070018
19import android.content.Context;
20import android.content.res.TypedArray;
21import android.graphics.Color;
Chiao Cheng89437e82012-11-01 13:41:51 -070022import android.util.AttributeSet;
Chiao Cheng89437e82012-11-01 13:41:51 -070023import android.view.Gravity;
24import android.view.View;
Andrew Lee020ba622014-04-25 15:26:35 -070025import android.widget.LinearLayout.LayoutParams;
Chiao Cheng89437e82012-11-01 13:41:51 -070026import android.widget.TextView;
27
Arthur Wang3f6a2442016-12-05 14:51:59 -080028import com.android.contacts.R;
Chiao Cheng89437e82012-11-01 13:41:51 -070029
30/**
31 * A custom view for the pinned section header shown at the top of the contact list.
32 */
Andrew Lee020ba622014-04-25 15:26:35 -070033public class ContactListPinnedHeaderView extends TextView {
Chiao Cheng89437e82012-11-01 13:41:51 -070034
Yorke Lee525f5132014-07-30 18:59:28 -070035 public ContactListPinnedHeaderView(Context context, AttributeSet attrs, View parent) {
Chiao Cheng89437e82012-11-01 13:41:51 -070036 super(context, attrs);
Chiao Cheng89437e82012-11-01 13:41:51 -070037
Yorke Leea675e3a2015-10-26 19:43:22 -070038 if (R.styleable.ContactListItemView == null) {
39 return;
40 }
Chiao Cheng89437e82012-11-01 13:41:51 -070041 TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.ContactListItemView);
Andrew Lee020ba622014-04-25 15:26:35 -070042 int backgroundColor = a.getColor(
43 R.styleable.ContactListItemView_list_item_background_color, Color.WHITE);
Andrew Lee815ce482014-05-30 12:28:17 -070044 int textOffsetTop = a.getDimensionPixelSize(
45 R.styleable.ContactListItemView_list_item_text_offset_top, 0);
Andrew Lee0861aad2014-06-09 14:02:51 -070046 int paddingStartOffset = a.getDimensionPixelSize(
47 R.styleable.ContactListItemView_list_item_padding_left, 0);
Brian Attwellab538e52014-09-04 20:58:15 -070048 int textWidth = getResources().getDimensionPixelSize(
49 R.dimen.contact_list_section_header_width);
50 int widthIncludingPadding = paddingStartOffset + textWidth;
Chiao Cheng89437e82012-11-01 13:41:51 -070051 a.recycle();
52
Andrew Lee020ba622014-04-25 15:26:35 -070053 setBackgroundColor(backgroundColor);
Andrew Leedd28d832014-04-28 18:38:31 -070054 setTextAppearance(getContext(), R.style.SectionHeaderStyle);
Brian Attwellab538e52014-09-04 20:58:15 -070055 setLayoutParams(new LayoutParams(widthIncludingPadding, LayoutParams.WRAP_CONTENT));
Yorke Lee525f5132014-07-30 18:59:28 -070056 setLayoutDirection(parent.getLayoutDirection());
Wenyi Wangd5403fa2016-07-20 17:16:38 -070057 setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
Andrew Lee815ce482014-05-30 12:28:17 -070058
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(
Andrew Lee0861aad2014-06-09 14:02:51 -070062 getPaddingStart() + paddingStartOffset,
Andrew Lee815ce482014-05-30 12:28:17 -070063 getPaddingTop() + (textOffsetTop * 2),
64 getPaddingEnd(),
65 getPaddingBottom());
Chiao Cheng89437e82012-11-01 13:41:51 -070066 }
67
68 /**
69 * Sets section header or makes it invisible if the title is null.
70 */
Andrew Lee020ba622014-04-25 15:26:35 -070071 public void setSectionHeaderTitle(String title) {
John Shaoda188712016-08-19 14:57:11 -070072 if (title != null) {
Andrew Lee020ba622014-04-25 15:26:35 -070073 setText(title);
74 setVisibility(View.VISIBLE);
Chiao Cheng89437e82012-11-01 13:41:51 -070075 } else {
Andrew Lee020ba622014-04-25 15:26:35 -070076 setVisibility(View.GONE);
Chiao Cheng89437e82012-11-01 13:41:51 -070077 }
78 }
Chiao Cheng89437e82012-11-01 13:41:51 -070079}