Android examples for User Interface:TextView Value
Compute the maximum number of characters per line in the given TextView .
/*// w w w .j a va 2 s .c o m * Copyright 2013 Google Inc., Pietro "Rampo" Rampini - PiKo Technologies * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import android.text.Layout; import android.util.Log; import android.widget.TextView; public class Main{ private static final String TAG = "CharsPerLineUtil"; /** * Compute the maximum number of characters per line in the given {@link TextView}. */ public static int getMaxCharsPerLine(TextView textView) { return getMaxCharsPerLine(textView.getLayout()); } /** * Compute the maximum number of characters per line in the given {@link Layout}. */ public static int getMaxCharsPerLine(Layout layout) { int maxChars = 0; int maxIndex = -1; for (int i = layout.getLineCount() - 1; i >= 0; i--) { int chars = layout.getLineEnd(i) - layout.getLineStart(i); if (chars > maxChars) { maxChars = chars; maxIndex = i; } } if (BuildConfig.DEBUG && maxIndex >= 0) { CharSequence line = layout.getText().subSequence( layout.getLineStart(maxIndex), layout.getLineEnd(maxIndex)); Log.d(TAG, "Max line: '" + line + "' (length=" + line.length() + ")"); } return maxChars; } }