I am automating a product using robotium. I need to do some dat validation kind of task there.
Scenario is like:
We click on a list, select some items in the list and do some operation.
I want to put the names of the items selected into an array. Such that i can compare it later.
I used the following code:
for(i=0; i<=n;i++)
{
solo.clickInList(i);
Array1[i]=solo.getText(i).toString();
}
But sadly, this statement is not extracting the text of the textView selected but the id of the textView.
Please help me by giving an example of how to get the text of the TextView selected.
At a fix!!
if you have only listview with texts, it should work for you (I didn't test it):
ListView listView = solo.getView(ListView.class, 0);
String text = listView.getItemAtPosition(position));
Another way will be like this:
ArrayList<TextView> result = solo.clickInList(line);
String text = "";
for (int i = 0; i < result.size(); i++) {
text += result.get(i).getText().toString() + " ";
}
if (text.length() > 0) {
text = text.substring(0, text.length() - 1); // remove last space
}
Related
I am using Edit text as a view of android Recycler view but, when a user does not enter any value in Edit text then I am not able to show an Error message or auto scroll on that empty Edit text. I am storing some data into the database using recycler view edit text, I searched very much but I didn't found any helping material. I want to submit that recycler view data before validating all fields (Edit text).
Solved.
int counter = 0;
for(int i=0;i<stockData.length;i++) {
for (int j = 0; j < stockData[i].length; j++) {
if((stockData[i][0]==null || stockData[i][0].matches("")) ||
((stockData[i][1]==null || stockData[i][1].matches("")) ||
(stockData[i][4]==null || stockData[i][4].matches("Stock"))))
{
counter++;
//Log.d(Constants.TAG,"sale is empty and index is "+i);
mPromoter_recyclerView.scrollToPosition(i);
break;
}
}
if(counter>0)
break;
}
I have a string, and I can detect if it has an # inside it.
if(title.contains("#")){
SpannableString WordtoSpan = new SpannableString(title);
int idx = title.indexOf("#");
if (idx >= 0) {
int wordEnd = title.indexOf(" ", idx);
if (wordEnd < 0) {
wordEnd = title.length();
}
WordtoSpan.setSpan(new ForegroundColorSpan(Color.RED),
idx,
wordEnd,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
holder.txtTitle.setText(WordtoSpan);
} else {
holder.txtTitle.setText(title);
}
So now, if the string has an # it will color it till the end of the word after it with red. This works perfectly. But the problem is, when a string has more than one # it will color only the first # with it word, not the next or the third etc ..
ex. Now:
Notice it ill color only burger with red
I love chicken #burger , cuz they are #delicious.
I want: Notice bother burger and delicious are colored.
I love chicken #burger , cuz they are #delicious .
use title.split("#") to get a array of strings which contains #.
Something like:
String parts = title.split("#");
for(int i = 0; i<parts.length(); i++){
//TODO: do something with the string part
//parts[i], this is a part of the string.
}
You can use the Pattern to find and match words that contain hashtags in a given string.
String text = "I love chicken #burger, because they are #delicious!";
Pattern HASHTAG_PATTERN = Pattern.compile("#(\\w+|\\W+)");
Matcher mat = HASHTAG_PATTERN.matcher(text);
while (mat.find()) {
String tag = mat.group(0);
//String tag will contain the hashtag
//do operations with the hashtag
}
I am trying to colour text for my android app depending on a value entered into the SQLite database. I have set up 3 textviews and have different text colours for all of these.
The code looks like this
String arr[] = data.split("..\n\n");
for(int i = 0; i < arr.length; i++)
{
System.out.println("arr["+i+"] = " + arr[i].trim());
if(arr[i].contains("High Severity"))
{
// String highArr = arr[i];
textView.setVisibility(View.VISIBLE);
textView.setText(highArr+"\n");
textView.setTextColor(Color.RED);
}
else if(arr[i].contains("Low Severity"))
{
textView3.setVisibility(View.VISIBLE);
textView3.setText(arr[i]+"\n");
textView3.setTextColor(Color.GREEN);
}
else if(arr[i].contains("Medium Severity"))
{
textView2.setVisibility(View.VISIBLE);
textView2.setText(arr[i]+"\n");
textView2.setTextColor(Color.rgb(255, 136, 0));
}
}
I have parsed the string which has all the values for my database table, but when I try my for loop it only prints out the latest entered values.
It looks like it could be that your arr[] is only got one entry in it. You should use the debugger to see how many times your loop actually runs and what the actual lenght of the array is.
EDIT:
I just realized that you only have three textviews. Of course the textview is displaying the last one because each time your calling the settext method its reseting the text. There are a few ways to do what you are trying but Im not sure how your app is set up. You should try dynamically creating the textview, setting the text to it, then adding that textview to a linearlayout or relativelayout that you have set up. Or you could do this inside of a listview using a custom implementation of a baseadapter or any of the other adapters depending on your needs.
I would change my code like this:
String arr[] = data.split("..\n\n");
String Hseverity = "", Mseverity = "", Lseverity = "";
for(int i = 0; i < arr.length; i++)
{
System.out.println("arr["+i+"] = " + arr[i].trim());
if(arr[i].contains("High Severity"))
{
// String highArr = arr[i];
textView.setVisibility(View.VISIBLE);
Hseverity += arr[i] +"\n";
textView.setText(Hseverity);//do this to save each High Severity entry
//same for the other severity levels
textView.setTextColor(Color.RED);
}
else if(arr[i].contains("Low Severity"))
{
textView3.setVisibility(View.VISIBLE);
Lseverity += arr[i]+"\n";
textView3.setText(Lseverity);
textView3.setTextColor(Color.GREEN);
}
else if(arr[i].contains("Medium Severity"))
{
textView2.setVisibility(View.VISIBLE);
Mseverity += arr[i] + "\n";
textView2.setText(Mseverity);
textView2.setTextColor(Color.rgb(255, 136, 0));
}
}
I am taking Spanned Text from an EditText box and converting it to a HTML tagged string using HTML.toHtml. This works fine. I have verified that the string is correct and contains a <br> in the appropriate location. However, when I got to convert the tagged string back to a spanned text to populate a TextView or EditText using HTML.fromHtml the <br> (or multiple ones if they are present) at the end of the first paragraph disappear. This means that if a users entered text with multiple line breaks and wanted to keep that formatting it gets lost.
I attached a picture to help illustrate this. The first EditText is the user input, the TextView Below it is the HTML.tohtml result of the EditText above it, the EditText below it is populated using HTML.fromHtml using the string in the TextView above it. As you can see the line breaks have disappeared and so have the extra lines. Furthermore, when the spanned text of the second edit text is run through the HTML.toHtml it now produces a different HTML tagged String.
I would like to be able to take the HTML tagged String from the first EditText and populate other TextViews or EditTexts without losing line breaks and formatting.
I also had this problem and I could not find an easy "transform" or something alike solution. Note something important, when the user presses "enter" java produces the special character \n but in HTML there is no such format for line breaking. It is the <br />.
So what I have done was to replace some specific CharSequences, from the plain text, by the alternative HTML format. In my case there was only the "enter" character so it was not that messy.
I had similar problem when I was trying to save/restore editText content to db. The problem is in Html.toHtml, it somehow skips line brakes:
String src = "<p dir=\"ltr\">First line</p><p dir=\"ltr\">Second<br/><br/><br/></p><p dir=\"ltr\">Third</p>";
EditText editText = new EditText(getContext());
// All line brakes are correct after this
editText.setText(new SpannedString(Html.fromHtml(src)));
String result = Html.toHtml(editText.getText()); // Here breaks are lost
// Output :<p dir="ltr">First line</p><p dir="ltr">Second<br></p><p dir="ltr">Third</p>
I've solved this by using custom toHtml function to serialize spanned text, and replaced all '\n' with "< br/>:
public class HtmlParser {
public static String toHtml(Spannable text) {
final SpannableStringBuilder ssBuilder = new SpannableStringBuilder(text);
int start, end;
// Replace Style spans with <b></b> or <i></i>
StyleSpan[] styleSpans = ssBuilder.getSpans(0, text.length(), StyleSpan.class);
for (int i = styleSpans.length - 1; i >= 0; i--) {
StyleSpan span = styleSpans[i];
start = ssBuilder.getSpanStart(span);
end = ssBuilder.getSpanEnd(span);
ssBuilder.removeSpan(span);
if (span.getStyle() == Typeface.BOLD) {
ssBuilder.insert(start, "<b>");
ssBuilder.insert(end + 3, "</b>");
} else if (span.getStyle() == Typeface.ITALIC) {
ssBuilder.insert(start, "<i>");
ssBuilder.insert(end + 3, "</i>");
}
}
// Replace underline spans with <u></u>
UnderlineSpan[] underSpans = ssBuilder.getSpans(0, ssBuilder.length(), UnderlineSpan.class);
for (int i = underSpans.length - 1; i >= 0; i--) {
UnderlineSpan span = underSpans[i];
start = ssBuilder.getSpanStart(span);
end = ssBuilder.getSpanEnd(span);
ssBuilder.removeSpan(span);
ssBuilder.insert(start, "<u>");
ssBuilder.insert(end + 3, "</u>");
}
replace(ssBuilder, '\n', "<br/>");
return ssBuilder.toString();
}
private static void replace(SpannableStringBuilder b, char oldChar, String newStr) {
for (int i = b.length() - 1; i >= 0; i--) {
if (b.charAt(i) == oldChar) {
b.replace(i, i + 1, newStr);
}
}
}
}
Also it turned out that this way is faster in about 4 times that default Html.toHtml(): I've made a benchmark with about 20 pages and 200 spans:
Editable ed = editText.getText(); // Here is a Tao Te Ching :)
String result = "";
DebugHelper.startMeasure("Custom");
for (int i = 0; i < 10; i++) {
result = HtmlParserHelper.toHtml(ed);
}
DebugHelper.stopMeasure("Custom"); // 19 ms
DebugHelper.startMeasure("Def");
for (int i = 0; i < 10; i++) {
result = Html.toHtml(ed);
}
DebugHelper.stopMeasure("Def"); // 85 ms
Replace /n => < br>< br>
example
< p>hi< /p>
< p>j< /p>
to:
< p>hi< /p>< br>< br>< p>j< /p>
My Android app is displaying text in a TextView.
Are there any tags or anything to put around words that I want italicized? I don't need to set the TextView as italics because the whole sentence would be that way, and I only need specific words italicized.
You need to use a Spannable: see Is there any example about Spanned and Spannable text for an example.
I agree, this isn't a database issue. If this is not a custom app you're working on, you're out of luck. If it is, save the field in the database as HTML.
This function sets a string as the text of a TextView and italicizes one or more spans within that string as long as the span or spans are marked with the tags [i] and [/i]. Of course, it can be modified for other types of styling.
private void doItalicize(TextView xTextView, String xString) {
ArrayList<Integer> IndexStart = new ArrayList<>();
ArrayList<Integer> IndexEnd = new ArrayList<>();
ArrayList<StyleSpan> SpanArray = new ArrayList<>();
int i = 0;
do {
IndexStart.add(i, xString.indexOf("[i]"));
IndexEnd.add(i, xString.indexOf("[/i]") - 3);
xString = xString.replaceFirst("\\[i\\]", "");
xString = xString.replaceFirst("\\[/i\\]", "");
xTextView.setText(xString, TextView.BufferType.SPANNABLE);
SpanArray.add(i, new StyleSpan(Typeface.ITALIC));
Log.d(LOG_TAG, "i: " + i);
i++;
} while (xString.contains("[i]"));
Spannable xSpannable = (Spannable) xTextView.getText();
for (int j = 0; j < i; j++)
xSpannable.setSpan(SpanArray.get(j), IndexStart.get(j), IndexEnd.get(j), Spanned
.SPAN_EXCLUSIVE_EXCLUSIVE);
}
The solution would seem to be to save the content in the database as HTML and then output the HTML in the textview.
See the following article: How to display HTML in TextView?