Sabtu, 08 Februari 2014

Example of using TextUtils.commaEllipsize()

TextUtils.commaEllipsize() converts a CharSequence of the comma-separated form "Andy, Bob, Charles, David" that is too wide to fit into the specified width into one like "Andy, Bob, 2 more".

Here is a example:
TextUtils.commaEllipsize() example
TextUtils.commaEllipsize() example
  • When updateEllipsizedText() called in onCreate() to update myText1, because the views have not been shown here, getMeasuredWidth() inside updateEllipsizedText() return 0, so nothing shown on myText1.
  • To update myText2 after views layout, implement OnGlobalLayoutListener() to call updateEllipsizedText().

package com.example.androidtextutils;

import android.os.Bundle;
import android.app.Activity;
import android.text.TextPaint;
import android.text.TextUtils;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.TextView;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView myText1 = (TextView) findViewById(R.id.mytext1);
final TextView myText2 = (TextView) findViewById(R.id.mytext2);

final String days = "Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday";

//views not yet layouted, getMeasuredWidth() inside updateEllipsizedText() return 0
//nothing display on myText1!
updateEllipsizedText(myText1, days);

//update myText2 with TextUtils.commaEllipsize() String after layouted
ViewTreeObserver viewTreeObserver = myText2.getViewTreeObserver();
if (viewTreeObserver.isAlive()) {
viewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
//removeGlobalOnLayoutListener() was deprecated in API level 16.
//Use removeOnGlobalLayoutListener() instead
myText2.getViewTreeObserver().removeGlobalOnLayoutListener(this);
//for API 16
//myText.getViewTreeObserver().removeOnGlobalLayoutListener(this);
updateEllipsizedText(myText2, days);
}
});
}
}

private void updateEllipsizedText(TextView tv, String text) {
TextPaint p = tv.getPaint();
tv.setText(text);
float avail = tv.getMeasuredWidth();
String oneMore = "one more";
String more = "%d more";
CharSequence ellipsizedText = TextUtils.commaEllipsize(text, p, avail,
oneMore, more);
tv.setText(ellipsizedText);
}

}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="android-coding.blogspot.com" />

<TextView
android:id="@+id/mytext1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold" />
<TextView
android:id="@+id/mytext2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold|italic" />

</LinearLayout>

0 komentar:

Posting Komentar

Copyright © 2012 Codding News All Right Reserved