I have some text in a file [~100 KB] that needs to be displayed to the user in a TextView. I want to split the text into pages.
This is the idea I have in mind to implement pagination:
Determine screen width and screen height, say 320 x 480.
Calculate 75% of height, [360 px] to accommodate buttons etc.
Determine font size
Calculate number of characters [N] that can be displayed.
Read from file and display only N number of characters.
This seems like it would work, but it seems crude and error-prone. Does anyone have better ideas?
You should create your own extension of TextView and hook up into the onMeasure function which should give you the width and height (prob want to give the textview layout_weight=1)
Then you can use a paint to get the size that text will take up, not I've not tested this and you'll probably need to do something to account for splitting of newlines. But it is a good start...
Paint paint = new Paint();
Rect bounds = new Rect();
int text_height = 0;
int text_width = 0;
paint.setTypeface(Typeface.DEFAULT);
paint.setTextSize(12);// have this the same as your text size
String text = "Some random text";
paint.getTextBounds(text, 0, text.length(), bounds);
text_check_h = bounds.height(); // Will give you height textview will occupy
text_check_w = bounds.width(); // Will give you width textview will occupy
Related
I am trying to get the width of a textview setting the text. Here's the Code I am using:
public int getTextViewEffectedWidth(TextView textView, String content){
Rect bounds = new Rect();
Paint paint = textView.getPaint();
paint.getTextBounds(content, 0, content.length(), bounds);
return bounds.width();
}
But this method returns larger width. Example:
I need set in textview value "6978"
This method was calculate width = 46 (By the way returned value in dp or sp ? I set in dp, cause in px this value in not enough.)
If I set textview's width 46 dp or sp, TextView will has extra place
I need to get the width value which would be consistent with width="wrap_content"
TextView size and font have default values.
try this,you will get the value in pixel;
int width=convertToPixel(textView.getWidth());
int height=convertToPixel(textView.getHeight());
private int convertToPixel(int n) {
Resources r = getResources();
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, n, r.getDisplayMetrics());
return (int) px;
}
I know it's a little while after your request, but are you running the check in an OnGlobalLayoutListener?
If not, then the view won't be completely drawn on the screen, and you won't have the dimensions available.
Inside the onGlobalLayout() you can called measuredWidth and measuredHeight.
I hope this helps, if not you, then someone else.
I am looking to resize an EditText height, based on the height of the text size.
The reason I want to do this is because some users will set their text size to "Large" inside the Android Accessibility Options, and than some of the text will be cut off within my applications EditText views.
What can I do to dynamically adjust the height to match the Text Size? I do not need auto resizing or anything fancy.
Here is some code inside my custom EditText Class
mCurrentTextSize = getTextSize();
LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT,
DisplayUtils.pixel2dp(getContext(), mCurrentTextSize));
setLayoutParams(layoutParams);
Inside a Utility Class
public static int pixel2dp(Context context, float pixel) {
DisplayMetrics metrics = new DisplayMetrics();
float logicalDensity = metrics.density;
return (int) Math.ceil(pixel / logicalDensity);
}
Why not use layout_height=wrap_content? It will automatically re-adjust the EditText so as to contain the text.
Part of my app displays a single 5-character word in a TextView which should fill the entire screen. The questions listed below address this same issue:
How to adjust text font size to fit textview
Have TextView scale its font size to fill parent?
Text size and different android screen sizes
Auto-fit TextView for Android
However, since my case requires only one 5-character word to fill the entire screen, is there a simpler way to do this? If not, would it be practical to simply provide drawable resources (assuming that the 5-character word will always be the same)?
Thanks!
I'm going to give you some code which shows the exact opposite of what you requested. The code below shrinks a line of text until it fits within the desired area. But there is the basic idea: Get the paint of the textView and then measure it. Adjust the size and remeasure it e.g. textView.getPaint().measureText("Your text")
public static float calculateTextSizeToFit(TextView textView, String desiredText, int limitSpSize, float desiredTxtPxSize) {
Paint measurePaint = new Paint(textView.getPaint());
measurePaint.setTextSize(desiredTxtPxSize);
float pWidth = measurePaint.measureText(desiredText);
float labelWidth = textView.getWidth();
int maxLines = textView.getMaxLines();
while (labelWidth > 0 && pWidth/maxLines > labelWidth-20) {
float textSize = measurePaint.getTextSize();
measurePaint.setTextSize(textSize-1);
pWidth = measurePaint.measureText(desiredText);
if (textSize < TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_SP, limitSpSize,
textView.getContext().getResources().getDisplayMetrics())) break;
}
return measurePaint.getTextSize();
}
I'm now trying to resolve an issue with somehow overlapping text in TextView.
I've posted a question about that , but I've also tried to solve it myself. I decided to count the amount of text which causes the textview to break the line. I came up with this unpolished code, which basically should inflate the view, set it's layout params according to the displayed size and then run onmeasure and return lineCount. I plan than to use probably binary search to find exact text length which fits into the textview, but even before I've just tried to run the code and see how it behaves. It's kind of weird, because it gives me different results, than what I see on screen than. I even tried to alter the textsize according to scaled density, because I wasn't sure whether it's been taken into account.
Here is the code. It returns three,but when I render the layout to screen the text takes up only two lines.
public int getCountOfLines(Context context, int widgetDp){
LayoutInflater mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout layout = (LinearLayout) mLayoutInflater.inflate(R.layout.event_basic_large ,null, false);
TextView titleTextView = (TextView) layout.findViewById(R.id.itemTitle);
titleTextView.setText(TEST_TEXT);
float density = context.getResources().getDisplayMetrics().density;
float textDensity = context.getResources().getDisplayMetrics().scaledDensity;
titleTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleTextView.getTextSize()/density*textDensity);
layout.setLayoutParams(
new LinearLayout.LayoutParams(
(int) (widgetDp*density),
ViewGroup.LayoutParams.WRAP_CONTENT)
);
layout.measure(View.MeasureSpec.makeMeasureSpec(
(int) (widgetDp*density), View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
);
Log.d(TAG, titleTextView.getLineCount() + " lines, width "+titleTextView.getMeasuredWidth());
return titleTextView.getLineCount();
}
I ran into this a while back and after searching and trying finally got the following function to work:
public int getLineCount(String testString, float textSize,
float width) {
Rect bounds = new Rect();
Paint paint = new Paint();
paint.setTypeface(<font>);//set font that you are using for this
paint.setTextSize(textSize);
paint.getTextBounds(testString, 0, testString.length(), bounds);
return (int) Math.ceil(bounds.width() / width);
}
This function uses textSize and width to give number of lines.
This functions give number of line in a textview before it is displayed.
I'm trying to display a TextView in random positions on the screen, but the text shouldn't go outside the screen. The text will always be short, no more than 3 words. This is what I have so far:
final TextView tv = ((TextView)findViewById(R.id.text);
final Random rand = new Random();
final DisplayMetrics metrics = getResources().getDisplayMetrics();
int width = rand.nextInt(metrics.widthPixels);
int height = rand.nextInt(metrics.heightPixels);
final FrameLayout.LayoutParams flp = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
flp.setMargins(width, height, 0, 0);
tv.setLayoutParams(flp);
UPDATE:
Forgot, I had this function to get a random number within a range:
public static int Random(final int lower, final int uppper)
{
return lower + (int)(Math.random() * ((uppper - lower) + 1));
}
So I updated the code to this:
int width = Random(0, metrics.widthPixels);
int height = Random(0, metrics.heightPixels);
But it still sometimes displays outside the viewing area. I even subtracted 10 from each value, to ensure it stays in. For the most part it does, but then it seems like it shows somewhere outside the screen.
try looking up abit more on getting the random number to stay inside the max width and height,
Why cant you do this?
Instead of doing that, you can found the Max x and Max y for the device and then Based on condition you can set the View's position to perticular position.
Also see this: SO
might be helpful to you.
comment me for any query.
but it still sometimes displays outside the viewing area
Dont you have title on top of your activity? That does count in... Am not sure but I think the notification bar as well... If u work in fullscreen mode, you shouldnt have problem.
Remember that the x y coordinates are always the upper left corner of an objectview! So it still can be happen that u get half (or full in case :D) of your picture/textview/whatever outside the screen...