1 /* 2 * Copyright (C) 2011 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.calendar; 18 19 import android.content.Context; 20 import android.graphics.drawable.Drawable; 21 import android.util.AttributeSet; 22 import android.view.View; 23 import android.view.View.OnClickListener; 24 import android.widget.ImageButton; 25 import android.widget.LinearLayout; 26 import android.widget.TextView; 27 28 public class ExpandableTextView extends LinearLayout implements OnClickListener { 29 30 TextView mTv; 31 ImageButton mButton; // Button to expand/collapse 32 33 private boolean mRelayout = false; 34 private boolean mCollapsed = true; // Show short version as default. 35 private int mMaxCollapsedLines = 8; // The default number of lines; 36 private Drawable mExpandDrawable; 37 private Drawable mCollapseDrawable; 38 39 public ExpandableTextView(Context context) { 40 super(context); 41 init(); 42 } 43 44 public ExpandableTextView(Context context, AttributeSet attrs) { 45 super(context, attrs, 0); 46 init(); 47 } 48 49 public ExpandableTextView(Context context, AttributeSet attrs, int defStyle) { 50 super(context, attrs, defStyle); 51 init(); 52 } 53 54 void init() { 55 mMaxCollapsedLines = getResources().getInteger((R.integer.event_info_desc_line_num)); 56 mExpandDrawable = getResources().getDrawable(R.drawable.ic_expand_small_holo_light); 57 mCollapseDrawable = getResources().getDrawable(R.drawable.ic_collapse_small_holo_light); 58 } 59 60 @Override 61 public void onClick(View v) { 62 if (mButton.getVisibility() != View.VISIBLE) { 63 return; 64 } 65 66 mCollapsed = !mCollapsed; 67 mButton.setImageDrawable(mCollapsed ? mExpandDrawable : mCollapseDrawable); 68 mTv.setMaxLines(mCollapsed ? mMaxCollapsedLines : Integer.MAX_VALUE); 69 } 70 71 @Override 72 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 73 // If no change, measure and return 74 if (!mRelayout || getVisibility() == View.GONE) { 75 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 76 return; 77 } 78 mRelayout = false; 79 80 // Setup with optimistic case 81 // i.e. Everything fits. No button needed 82 mButton.setVisibility(View.GONE); 83 mTv.setMaxLines(Integer.MAX_VALUE); 84 85 // Measure 86 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 87 88 // If the text fits in collapsed mode, we are done. 89 if (mTv.getLineCount() <= mMaxCollapsedLines) { 90 return; 91 } 92 93 // Doesn't fit in collapsed mode. Collapse text view as needed. Show 94 // button. 95 if (mCollapsed) { 96 mTv.setMaxLines(mMaxCollapsedLines); 97 } 98 mButton.setVisibility(View.VISIBLE); 99 100 // Re-measure with new setup 101 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 102 } 103 104 private void findViews() { 105 mTv = (TextView) findViewById(R.id.expandable_text); 106 mTv.setOnClickListener(this); 107 mButton = (ImageButton) findViewById(R.id.expand_collapse); 108 mButton.setOnClickListener(this); 109 } 110 111 public void setText(String text) { 112 mRelayout = true; 113 if (mTv == null) { 114 findViews(); 115 } 116 String trimmedText = text.trim(); 117 mTv.setText(trimmedText); 118 this.setVisibility(trimmedText.length() == 0 ? View.GONE : View.VISIBLE); 119 } 120 121 public CharSequence getText() { 122 if (mTv == null) { 123 return ""; 124 } 125 return mTv.getText(); 126 } 127 } 128