1 /* 2 * Copyright (C) 2018 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.providers.media; 18 19 import android.content.Context; 20 import android.text.TextUtils; 21 import android.util.AttributeSet; 22 23 public class DialogTitleTextView extends androidx.appcompat.widget.AppCompatTextView { 24 private static final int MAX_LINES = 3; // Maximum lines allowed 25 DialogTitleTextView(Context context)26 public DialogTitleTextView(Context context) { 27 super(context); 28 init(); 29 } 30 DialogTitleTextView(Context context, AttributeSet attrs)31 public DialogTitleTextView(Context context, AttributeSet attrs) { 32 super(context, attrs); 33 init(); 34 } 35 DialogTitleTextView(Context context, AttributeSet attrs, int defStyleAttr)36 public DialogTitleTextView(Context context, AttributeSet attrs, int defStyleAttr) { 37 super(context, attrs, defStyleAttr); 38 init(); 39 } 40 init()41 private void init() { 42 setMaxLines(MAX_LINES); 43 44 setEllipsize(TextUtils.TruncateAt.END); // Add ellipsis if text is too long 45 } 46 47 @Override onMeasure(int widthMeasureSpec, int heightMeasureSpec)48 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 49 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 50 51 // Check if the number of lines exceeds the limit 52 if (getLineCount() > MAX_LINES) { 53 // If exceeding, measure again with unlimited height to wrap the text 54 super.onMeasure(widthMeasureSpec, 55 MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); 56 } 57 } 58 } 59