1 /*
2  * Copyright (C) 2015 Google Inc.
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.support.v7.widget;
18 
19 import android.content.Context;
20 import android.content.res.TypedArray;
21 import android.support.v7.appcompat.R;
22 import android.text.Layout;
23 import android.util.AttributeSet;
24 import android.util.TypedValue;
25 import android.widget.TextView;
26 
27 /**
28  * Used by dialogs to change the font size and number of lines to try to fit
29  * the text to the available space.
30  *
31  * @hide
32  */
33 public class DialogTitle extends TextView {
34 
DialogTitle(Context context, AttributeSet attrs, int defStyleAttr)35     public DialogTitle(Context context, AttributeSet attrs, int defStyleAttr) {
36         super(context, attrs, defStyleAttr);
37     }
38 
DialogTitle(Context context, AttributeSet attrs)39     public DialogTitle(Context context, AttributeSet attrs) {
40         super(context, attrs);
41     }
42 
DialogTitle(Context context)43     public DialogTitle(Context context) {
44         super(context);
45     }
46 
47     @Override
onMeasure(int widthMeasureSpec, int heightMeasureSpec)48     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
49         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
50 
51         final Layout layout = getLayout();
52         if (layout != null) {
53             final int lineCount = layout.getLineCount();
54             if (lineCount > 0) {
55                 final int ellipsisCount = layout.getEllipsisCount(lineCount - 1);
56                 if (ellipsisCount > 0) {
57                     setSingleLine(false);
58                     setMaxLines(2);
59 
60                     final TypedArray a = getContext().obtainStyledAttributes(null,
61                             R.styleable.TextAppearance,
62                             android.R.attr.textAppearanceMedium,
63                             android.R.style.TextAppearance_Medium);
64                     final int textSize = a.getDimensionPixelSize(
65                             R.styleable.TextAppearance_android_textSize, 0);
66                     if (textSize != 0) {
67                         // textSize is already expressed in pixels
68                         setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
69                     }
70                     a.recycle();
71 
72                     super.onMeasure(widthMeasureSpec, heightMeasureSpec);
73                 }
74             }
75         }
76     }
77 }