Search code examples
androidandroid-scrollview

Keep drawable height in middle of visible textview


I am using expandable textview like below. I want to add small drawable to the right of it (some sort of arrow). It is easy to keep this arrow in vertical center, when view is collapsed. However when I expand view, which contains a lot of text (like 100 lines), my drawable is placed in the middle of content height. How can i place it in the middle of the visible height?

   <LinearLayout
            android:id="@+id/tes5"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dip"
            android:gravity="center_vertical"
            android:layout_marginRight="16dip"
            android:layout_marginTop="6dip"
            android:orientation="horizontal" >


                <bigdig.yarh.ellotv.widget.ExpandableTextView
                    android:id="@+id/tv_artist_info"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textColor="@color/gray" />


            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/more_arrow_icon" />
        </LinearLayout> 

ExpandableTextView class

/**
* User: Bazlur Rahman Rokon
* Date: 9/7/13 - 3:33 AM
*/
public class ExpandableTextView extends TextView {
   private static final int DEFAULT_TRIM_LENGTH = 200;
   private static final String ELLIPSIS = ".....";

   private CharSequence originalText;
   private CharSequence trimmedText;
   private BufferType bufferType;
   private boolean trim = true;
   private int trimLength;

   public ExpandableTextView(Context context) {
       this(context, null);
   }

   public ExpandableTextView(Context context, AttributeSet attrs) {
       super(context, attrs);

       TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ExpandableTextView);
       this.trimLength = typedArray.getInt(R.styleable.ExpandableTextView_trimLength, DEFAULT_TRIM_LENGTH);
       typedArray.recycle();

       setOnClickListener(new OnClickListener() {
           @Override
           public void onClick(View v) {
               trim = !trim;
               setText();
               requestFocusFromTouch();
           }
       });
   }

   private void setText() {
       super.setText(getDisplayableText(), bufferType);
   }

   private CharSequence getDisplayableText() {
       return trim ? trimmedText : originalText;
   }

   @Override
   public void setText(CharSequence text, BufferType type) {
       originalText = text;
       trimmedText = getTrimmedText(text);
       bufferType = type;
       setText();
   }

   private CharSequence getTrimmedText(CharSequence text) {
       if (originalText != null && originalText.length() > trimLength) {
           return new SpannableStringBuilder(originalText, 0, trimLength + 1).append(ELLIPSIS);
       } else {
           return originalText;
       }
   }

   public CharSequence getOriginalText() {
       return originalText;
   }

   public void setTrimLength(int trimLength) {
       this.trimLength = trimLength;
       trimmedText = getTrimmedText(originalText);
       setText();
   }

   public int getTrimLength() {
       return trimLength;
   }
}

Solution

  • Did not found a way to achieve it with coumpaund textview so still habe to use linear layout

     <LinearLayout
                        android:id="@+id/ll_favorite_box"
                        android:layout_width="0dip"
                        android:visibility="gone"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:gravity="center_vertical"
                        android:orientation="horizontal" >
    
                        <ImageView
                            android:id="@+id/im_favorite_indicator"
                            android:layout_width="wrap_content"
                            android:layout_height="12dip"
                            android:layout_marginLeft="8dip"
                            android:layout_marginRight="4dip"
                            android:scaleType="centerInside"
                            android:src="@drawable/star_selected_icon" />
    
                        <TextView
                            android:id="@+id/tv_like_count"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginBottom="4dip"
                            android:layout_marginTop="4dip"
                            android:text="1000,0b"
                            android:textColor="@color/black_text_60"
                            android:textSize="16sp" />
                    </LinearLayout>