Need some help to understand paint object in android. I have a string object named myString. I load that with some content in a txt file which I have under assets.
In my code when I do myString.length() on that I get a vlue of 16708.
And when I use paint object as below to get the length, I get a value of 211050
myTextView = (TextView)findViewById(R.id.my_text_view);
Paint paint = myTextView.getPaint();
float length = paint.measureText(myString, 0, myString.length());
I get a value of length as 211050.
Now I am trying to understand that what are these values here. 16708 is obviously the total length of my string in characters. And what is 211050 here? Is it the string width in pixels?
yes, the length on the screen your text will take in terms of pixel.
Editted:
Yes, paint.breakText() helps in breaking the string according to the max width provided in the parameter.
But one must also use, setSubpixelText (true) to account for the strings containing i or l(small L) or w, as breaktext somehow fails to determine the exact length of the broken string using breakText().
Atleast I faced this problem, and got resolved using setSubpixelText() .
Hope this helps.
Related
If I have a string like this
String myString = "This is some text.";
Will I get the same lengths if I measure the entire string as when I take the sum of measuring each character?
At first thought, I assumed the answer to be yes, but then I was reading this answer, which said measureText adds an AdvanceX value before and after each measurement. If that is true, then it would add a subtle error into measuring summed lengths (because there would be double padding between each character).
I also know that there is kerning with some fonts that changes how close letters are placed together depending on the surrounding text. This is making me think more and more that a sum of the parts will be different than the whole.
The image comes from Wikipedia.
This is important because I am making my own custom text view that needs to handle line wrapping. If a single word is too long for the line then I measure each character until I discover where I need to break. However, I assumed that summing the measure of the individual character widths would be the same as measuring the whole string.
I have set up this question so that I can answer it below after I am finished with my tests.
The whole is not always equal to the sum of the parts.
This appears to be a result of kerning and not related to any AdvanceX double padding that you mentioned.
Test code
Here is the test code:
String myString = "This is some text.";
Paint myPaint = new Paint();
myPaint.setAntiAlias(true);
myPaint.setTextSize(100);
float theWhole = myPaint.measureText(myString);
float sumOfTheParts = 0;
for (int i = 0; i < myString.length(); i++) {
sumOfTheParts += myPaint.measureText(myString, i, i + 1);
}
Results
In my first test, the measures are the same:
String myString = "This is some text.";
theWhole // 787.0
sumOfTheParts // 787.0
However, when there are letters that need kerning, the measures are different. This is the test again with a different string:
String myString = "AV Wa";
theWhole // 291.0
sumOfTheParts // 297.0
Implications
You cannot assume that measuring all the characters of a string and then summing the total will give you the actual length of the string. Thus, when doing text wrapping you will need to measure entire string ranges.
I am getting an integer value in my android application.I want to convert it into floating point number which is in this format
"0.xyF"
.I tried lot of methods.I know its simple but i am confused.Please help.
I am passing a value from one activity to another using putExtra.So in the second activity i have to convert it to float for setting the value as verticalMargin for my dialog window.I used this line for getting the value in second activity.
int data = getIntent().getIntExtra("value", 7);
This is used for setting the vertical margin.
wlp.verticalMargin = "the converted floating point number";
If i is the integer value, then try:
float f=i;
while(f>=1.0f)
f/=10.0f;
You question still isn't clear. If you're asking how to convert an integer value that represents a percentage from 0 to 100 into a floating point value, then it would be fpVal = intVal / 100.0;
If you just want a simple conversion of an integer into a floating point number with the same exact value (e.g. 7 --> 7.0), then you can just cast it: fpVal = (float) intVal;
In your first activity store your integer value in string like this
String margin = "0."+int_value;
then pass this string to second activity.
In the second activity get that string from extra and convert it to float.
float float_value = Float.parseFloat(margin);
iam allmoust done with my app but iam stuck on this thing , i have two int's
called "positive" and "negative" and when i procces source below it shows 0.0
total = positive + negative;
float rate = positive/total;
rate*=100;
TextView analitycs = (TextView)v.findViewById(R.id.app_scores_analitycs);
analitycs.setText(String.valueOf(rate));
What Victor said is true.
Also you might want to use something different than String.valueOf(rate) to set the text of your text view, because this method can give you an ugly representation of the number.
You should probably use String.format("%.2f", rate) ad tweak that to your needs.
Are positive and total floats/doubles?
If not, then an int/int will give you an int.
The solution would be to cast either positive or negative as a float.
try the following:
float rate = ((float)(positive))/total;
I am making an android application where i got a set of strings that i load from SharedPreferences so that i can save the strings. The strings contain only numbers, but it is not an int value, its a string value. And i wounder how i can minus the numbers that's in there, becuase usually, i would have been using something like an int value = value - value; But that doesn't seem to work since it's a string and not an int value. How can i do this even though it's a string? I know i could use int values instead, but as i didn't think of this before now, when i'm almost done, it would be alot of work changing all of the code that's related to this. Please help me and thanks so much in advance!
You will have to convert your strings to ints first, then operate on them, then save the string back:
String value = preferences.getString("key:");
int intValue = Integer.valueOf(value);
intValue = intValue - 1;
preferences.edit().putString("key", Integer.toString(intValue)).commit();
Try using Integer.valueOf(string) or Integer.parseInt(string).
Learning basic programming the the concept of casting will help you tremendously. One datatype can be converted to another using the base classes, which often times deal with String. For instance look at the Documentation of Integer.
Well as you have said that it is a lot of work to change the code and save it as int, I would suggest converting the string into an integer, refer to this link for more information, someone has asked about converting strings to integers, and as Android is Java-based, this can apply to your project:
Converting a string to an integer on Android
Hope this helps.
I need calculate a thing. but my formula sentence has occur some problem.
TextView ticketP = (TextView)findViewById (R.id.ticketQ);
ticketP.setText(oneSession.getTicketOder());
String Ctotal = "";
Ctotal = jsonObject.optString("price");
String OneTotal = oneSession.getTicketOder() * Ctotal; // this part has occur the problem which is the operator * .
You'll need to convert the Strings to numeric type before performing any multiplication. Depending on the type of numeric value you are using take a look Double.parseDouble(String string) or Integer.parseInt(String string).
int oneTotal = Integer.valueOf(oneSession.getTicketOder()) * Integer.valueOf(Ctotal);
to convert it again to String use
String.valueOf(oneTotal)
Yes. Your going to have to use the parsing methods in order to convert the string to a native numerical type. You also need to be care about a few things with your code.
json.optString() can return null. opt = optional.
I would suggest using json.getInt() or json.getDouble() this will not only give you the correct type, but also throw an exception if the values aren't correct.
Secondly your going to have to convert your numerical answer back to a string if you want to display it. But this is easy enough with a .toString() or + "" if you are lazy.