Android Spanned object - android

Suppose I have an EditText:
Editable e = editText.getEditableText(); // length == 2
// ...attach a span start=0, end=2 to e...
int index = e.nextSpanTransition(0, 2, Object.class);
According to this: http://developer.android.com/reference/android/text/Spanned.html
index should be 0, since it says
Return the first offset greater than or equal to start where a markup object of class type begins or ends
But index is 2. Is this a bug or am I missing something?
Or am I even misinterpreting the docs, since it could mean "greater than start where a markup object begins, OR, equal to start where a markup object ends"?

The documentation also says:
or limit if there are no starts or ends greater than or equal to start but less than limit
Where limit is the second parameter (2 in your case). Your span does not satisfy less than limit because it is equal to it. So it returns limit.
Here is the source code that explains it:
/**
* Return the next offset after <code>start</code> but less than or
* equal to <code>limit</code> where a span of the specified type
* begins or ends.
*/
public int nextSpanTransition(int start, int limit, Class kind) {
int count = mSpanCount;
Object[] spans = mSpans;
int[] starts = mSpanStarts;
int[] ends = mSpanEnds;
int gapstart = mGapStart;
int gaplen = mGapLength;
if (kind == null) {
kind = Object.class;
}
for (int i = 0; i < count; i++) {
int st = starts[i];
int en = ends[i];
if (st > gapstart)
st -= gaplen;
if (en > gapstart)
en -= gaplen;
if (st > start && st < limit && kind.isInstance(spans[i]))
limit = st;
if (en > start && en < limit && kind.isInstance(spans[i]))
limit = en;
}
return limit;
}
Look at the last 2 if-sentences, in your case st=0, start=0, en=2, limit=2. The first if is false, the second if is false too. At the end it returns the unchanged limit parameter.

Related

generate random choice each time in array with no repeat in android

I'm developing multiple choice question game. It has four integer choices. Options are stored in an array list. I want to display the choices with the elements in the array in random order with no repetition each time. Please give me the solution.
int ans[] = new int[] { sum, sum + 1, sum + 2, sum - 1 };
ArrayList<Integer> number = new ArrayList<Integer>();
for (int i : number) {
number.add(ans[i]);
}
Collections.shuffle(number);
but1.setText("" + number.get(0));
but2.setText("" + number.get(1));
but3.setText("" + number.get(2));
but4.setText("" + number.get(3));
makeRandomArray returns an random int array between your size input. So if you pass size = 4, it returns an random array from 0 to 3, then handle your problem with random output. Hope this help!
/**
* Make an int random array with value from 0 to max
* #param size: size = max + 1
* #return result: an int array
*/
private int[] makeRandomArray(int size) {
int result[] = new int[size];
List<Integer> ascesdingArray = new ArrayList<>();
for (int i = 0; i < size; i++) {
ascesdingArray.add(i);
}
for (int i = 0; i < size; i++) {
int randomValue = random(ascesdingArray);
result[i] = randomValue;
}
return result;
}
/**
* Get random value in input array
* #param ascesdingArray
* #return randomValue: random element from input array
*/
private int random(List<Integer> ascesdingArray) {
int max = ascesdingArray.size() - 1;
int randomValue = 0;
if (max > 0) {
// get random index and it's value
int index = new Random().nextInt(max);
randomValue = ascesdingArray.get(index);
// remove value got from array so it will not be duplicated
ascesdingArray.remove(index);
} else if (max == 0) {
randomValue = ascesdingArray.get(0);
}
return randomValue;
}
First of all, your code will produce an IndexOutOfBoundsException due to you are iterating over an empty list making the result list empty and next your are picking four items from that empty list.
Finally, answering your question for your specific situation the next code will work:
It's a simplified and corrected working version of your code (tested):
List<Integer> number = Arrays.asList(sum, sum + 1, sum + 2, sum - 1);
Collections.shuffle(number);
but1.setText("" + number.get(0));
but2.setText("" + number.get(1));
but3.setText("" + number.get(2));
but4.setText("" + number.get(3));
Hope it helps

Substituting percentage of words at random indexes in String array

I have a string like
This is a very nice sentence
I break it into separate words and store in String[] with:
String[] words = s.split(" ");
How can I take a specific percentage on the total number of words (lets say 2 of 6 words) and substitute these 2 words with something else. My code so far:
//Indexes in total
int maxIndex = words.length;
//Percentage of total indexes
double percentageOfIndexes = 0.20;
//Round the number of indexes
int NumOfIndexes = (int) Math.ceil( maxIndex * (percentageOfIndexes / 100.0));
//Get a random number from rounded indexes
int generatedIndex = random.nextInt(NumOfIndexes);`
First, calculate how many words you want to replace:
int totalWordsCount = words.length;
double percentageOfWords = 0.20;
int wordsToReplaceCount = (int) Math.ceil( totalWordsCount * percentageOfWords );
Then, knowing how many words you want to replace, get that many random indexes, and just swap words at those indexes:
for (int i=0; i<wordsToReplaceCount; i++) {
int index = random.nextInt(totalWordsCount);
//and replace
words[index] = "Other"; // <--- insert new words
}
NOTE: Just remember that the smaller the number of words, the bigger discrepancy between your percentage, and actual number of words to replace, eg. 20% from 6 words is 1.2 word, which becomes 2 after Math.ceil(), and 2 is 33.33% from 6.

find string at pixel position in textview [duplicate]

I have a TextView with an OnTouchListener. What I want is the character index the user is pointing to when I get the MotionEvent. Is there any way to get to the underlying font metrics of the TextView?
Have you tried something like this:
Layout layout = this.getLayout();
if (layout != null)
{
int line = layout.getLineForVertical(y);
int offset = layout.getOffsetForHorizontal(line, x);
// At this point, "offset" should be what you want - the character index
}
Hope this helps...
I am not aware of a simple direct way to do this but you should be able to put something together using the Paint object of the TextView via a call to TextView.getPaint()
Once you have the paint object you will have access to the underlying FontMetrices via a call to Paint.getFontMetrics() and have access to other functions like Paint.measureText() Paint.getTextBounds(), and Paint.getTextWidths() for accessing the actual size of the displayed text.
While it generally works I had a few problems with the answer from Tony Blues.
Firstly getOffsetForHorizontal returns an offset even if the x coordinate is way beyond the last character of the line.
Secondly the returned character offset sometimes belongs to the next character, not the character directly underneath the pointer. Apparently the method returns the offset of the nearest cursor position. This may be to the left or to the right of the character depending on what's closer by.
My solution uses getPrimaryHorizontal instead to determine the cursor position of a certain offset and uses binary search to find the offset underneath the pointer's x coordinate.
public static int getCharacterOffset(TextView textView, int x, int y) {
x += textView.getScrollX() - textView.getTotalPaddingLeft();
y += textView.getScrollY() - textView.getTotalPaddingTop();
final Layout layout = textView.getLayout();
final int lineCount = layout.getLineCount();
if (lineCount == 0 || y < layout.getLineTop(0) || y >= layout.getLineBottom(lineCount - 1))
return -1;
final int line = layout.getLineForVertical(y);
if (x < layout.getLineLeft(line) || x >= layout.getLineRight(line))
return -1;
int start = layout.getLineStart(line);
int end = layout.getLineEnd(line);
while (end > start + 1) {
int middle = start + (end - start) / 2;
if (x >= layout.getPrimaryHorizontal(middle)) {
start = middle;
}
else {
end = middle;
}
}
return start;
}
Edit: This updated version works better with unnatural line breaks, when a long word does not fit in a line and gets split somewhere in the middle.
Caveats: In hyphenated texts, clicking on the hyphen at the end of a line return the index of the character next to it. Also this method does not work well with RTL texts.

Using setMultiples to set indices in a given array to steadily increase

I need to configure a method using setMultiples(int[] A, int offset), that will allow me to set the remaining indices of my array (A) starting at the offset to increasing in multiples of 17. Example (17, 34, 51, etc.)
The follwoing code allows me to fill my array (A) with increasing multiples of 17:
for(int i=3; i<500; i++) { values [i] = 17*2; }
I cant figure out how to use setMultiples after looking it up.
Do like this to set the values in the multiples of 17.
int[] values = new int[500];
int offset = 3;
setMultiples(values, offset);
in setMultiples method
private void setMultiples(int[] values, int offset) {
for(int i=offset; i<500; i++) {
values[i] = 17*i;
}
}

How to find android TextView number of characters per line?

So I have a TextView in android that has the width of the whole length of the screen and a padding of dip 5. How can I calculate the number of characters that will fit a single line on the screen? I guess in other words, I'm trying to get the number of columns of a textview?
I considered manual calculation depending on textsize and width, but 1) don't know the correlation and 2) due to the padding in the units of dip, different screens will use different number of actual pixels to pad.
Overall Question: I am trying to use this to solve: if given a string how can I manually edit to string such that when the textview prints the string character by character, I will know when to start a word that won't fit on one line on the next. Note: I know that textview automatically puts words that won't fit onto the next line, however, since I'm printing character by character, like typing animation, textview doesn't know the word won't fit until it prints out the overflowing characters of that word.
Been searching everywhere for this...
Thanks!
Added solutions:
one possible solution:
public String measure2 (TextView t, String s) {
String u = "";
int start = 0;
int end = 1;
int space = 0;
boolean ellipsized = false;
float fwidth = t.getMeasuredWidth();
for(;;) {
//t.setText(s.substring(start, end));
float twidth = t.getPaint().measureText(s.substring(start, end));
if (twidth < fwidth){
if (end < s.length())
end++;
else {
if (!ellipsized)
return s;
return u + s.subSequence(start, end);
}
}
else {
ellipsized = true;
space = (u + s.substring(start, end)).lastIndexOf(" ");
if (space == -1)
space = end - 1;
u += s.subSequence(start, space) + "\n";
start = space + 1;
end = start + 1;
}
}
}
solution 2, but still uses solution1 sometimes:
public String measure3 (TextView t, String s) {
List<String> wlist = Arrays.asList(s.split(" "));
if (wlist.size() == 1)
return measure2(t, s);
String u = "";
int end = 1;
float fwidth = t.getMeasuredWidth();
for(;;) {
//t.setText(s.substring(start, end));
if (wlist.isEmpty())
return u;
String temp = listStr(wlist, end);
float twidth = t.getPaint().measureText(temp);
if (twidth < fwidth){
if (end < wlist.size())
end++;
else {
return u + temp;
}
}
else {
temp = listStr(wlist, end-1);
if (end == 1)
temp = measure2(t, temp);
if (wlist.isEmpty())
return u + temp;
else
u = u + temp + "\n";
wlist = wlist.subList(end - 1, wlist.size());
end = 1;
}
}
}
public String listStr (List<String> arr, int end) {
String s = "";
for (String e : arr.subList(0, end) ){
s = s + e + " ";
}
return s.trim();
}
I used the above code to generate off a original string s, a string u that would be printed. However, I think this approach is very inefficient. Is there another approach or a better algorithm? Note: there are some errors in measure3 that I fixed, but was too lazy to edit
Try this:
private boolean isTooLarge (TextView text, String newText) {
float textWidth = text.getPaint().measureText(newText);
return (textWidth >= text.getMeasuredWidth ());
}
Detecting how many characters fit will be impossible due to the variable width of the characters. The above function will test if a particular string will fit or not in the TextView. The content of newText should be all the characters in a particular line. If true, then start a new line (and using a new string to pass as parameter).
Answer to the comment:
because the app can be run in many systems is exactly why you need to measure it.
This is a way to solve your "overall question". What is the difference between using str.size()>numCol vs is too large? You will need to implement your animation (hint #1: insert a newline character)
as I said before when you start a new line, you start a new string (hint #2: if you extend TextView, you can implement all this in overriding setText). (hint #3: Keep track of the lines created with a static int lines; and use newString.split("\\r?\\n")[lines-1] to check for length).
You can get total line of Textview and get string for each characters by below code.Then you can set style to each line whichever you want.
I set first line bold.
private void setLayoutListner( final TextView textView ) {
textView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
textView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
final Layout layout = textView.getLayout();
// Loop over all the lines and do whatever you need with
// the width of the line
for (int i = 0; i < layout.getLineCount(); i++) {
int end = layout.getLineEnd(0);
SpannableString content = new SpannableString( textView.getText().toString() );
content.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, end, 0);
content.setSpan(new StyleSpan(android.graphics.Typeface.NORMAL), end, content.length(), 0);
textView.setText( content );
}
}
});
}
Try this way.You can apply multiple style this way.
I had the same issue and I calculated the number characters per line by 2 steps:
Step 1: Calculate the number of lines
val widthOfTvComment = widthOfScreen - marginLeft - marginRight
var bounds = Rect()
var paint = Paint()
paint.textSize = textSize
paint.getTextBounds(comment,0,comment.length,bounds)
val lines = ( bounds.width()/widthOfTvComment)
Step 2: Calculated the number characters per line
val charactersPerLine = comment.length / lines

Categories

Resources