1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 5 * except in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the 10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 * KIND, either express or implied. See the License for the specific language governing 12 * permissions and limitations under the License. 13 */ 14 15 package com.android.settings.widget; 16 17 import android.content.Context; 18 import android.content.res.TypedArray; 19 import android.support.v7.preference.Preference; 20 import android.support.v7.preference.PreferenceViewHolder; 21 import android.text.TextUtils.TruncateAt; 22 import android.util.AttributeSet; 23 import android.widget.TextView; 24 import com.android.settings.R; 25 26 /** 27 * A preference whose summary text will only span one single line. 28 */ 29 public class FixedLineSummaryPreference extends Preference { 30 31 private int mSummaryLineCount; 32 FixedLineSummaryPreference(Context context, AttributeSet attrs)33 public FixedLineSummaryPreference(Context context, AttributeSet attrs) { 34 super(context, attrs); 35 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FixedLineSummaryPreference, 36 0, 0); 37 if (a.hasValue(R.styleable.FixedLineSummaryPreference_summaryLineCount)) { 38 mSummaryLineCount = a.getInteger( 39 R.styleable.FixedLineSummaryPreference_summaryLineCount, 1); 40 } else { 41 mSummaryLineCount = 1; 42 } 43 a.recycle(); 44 } 45 setSummaryLineCount(int count)46 public void setSummaryLineCount(int count) { 47 mSummaryLineCount = count; 48 } 49 50 @Override onBindViewHolder(PreferenceViewHolder holder)51 public void onBindViewHolder(PreferenceViewHolder holder) { 52 super.onBindViewHolder(holder); 53 TextView summary = (TextView) holder.findViewById(android.R.id.summary); 54 if (summary != null) { 55 summary.setMinLines(mSummaryLineCount); 56 summary.setMaxLines(mSummaryLineCount); 57 summary.setEllipsize(TruncateAt.END); 58 } 59 } 60 } 61