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