HTML in Android seems not working - java

I want customize a string with results that will be written in a TextView but not works. I just want color some part of text using html tags but it's still all text with same color. This is what i wrote so far:
#Override
public String toString() {
return (Html.fromHtml("<font color=\"#e61624\">"+fromHour+"</font>")) + " " + from + " / " + toHour + " " + to;
}
Thanks

I wouldn't use Html.fromHtml inside the toString() method.
In toString(), simply return the corresponding text:
return "<font color=\"#e61624\">"+fromHour+"</font> " + from + " / " + toHour + " " + to;
Then call:
textView.setText(Html.fromHtml(yourObject.toString());

// Try this way,hope this will help you to solve your problem.
yourTextView.setText(Html.fromHtml("<font color=\"#e61624\">"+fromHour+"</font> " + from + " / " + toHour + " " + to));

String text = "<font color=\"#e61624\">"+"<small>" + "Hello"
+ "</small>"+"</font>";
Html.fromHtml(text);

Related

How to drag & drop in specific position in selenium java?

I am trying to drag & drop 2 elements into my workspace but they are dropped over each other, how can i specify the position of dropping the elements?
I am using dragAndDrop() function
Actions act=new Actions(driver);
act.dragAndDrop(From, To).build().perform();
enter image description here
Using dragAndDrop() method you can perform drag and drop operations on only one location at a time. it is usually used to perform operations on web element which is capable of dropping. please find the reference http://demoqa.com/droppable/ for various types of dropable elements.
To implement drag and drop operation -
Example:
WebElement From=driver.findElement(By.xpath( <<source xpath>> ));
WebElement To=driver.findElement(By.xpath( <<destination xpath>> ));
Actions actions=new Actions(driver);
actions.dragAndDrop(From, To).build().perform();
to drag and drop another element, you need to perform above all steps again.
Hope this helps :)
Try Using below code :
public static void dragAndDropViaJQueryHelper(WebDriver driver, String dragSourceJQuerySelector, String dropTargetJQuerySelector) {
String jqueryScript = "(function( jquery ) {\r\n" +
" jquery.fn.simulateDragDrop = function(options) {\r\n" +
" return this.each(function() {\r\n" +
" new jquery.simulateDragDrop(this, options);\r\n" +
" });\r\n" +
" };\r\n" +
" jquery.simulateDragDrop = function(elem, options) {\r\n" +
" this.options = options;\r\n" +
" this.simulateEvent(elem, options);\r\n" +
" };\r\n" +
" jquery.extend(jquery.simulateDragDrop.prototype, {\r\n" +
" simulateEvent: function(elem, options) {\r\n" +
" /*Simulating drag start*/\r\n" +
" var type = 'dragstart';\r\n" +
" var event = this.createEvent(type);\r\n" +
" this.dispatchEvent(elem, type, event);\r\n" +
"\r\n" +
" /*Simulating drop*/\r\n" +
" type = 'drop';\r\n" +
" var dropEvent = this.createEvent(type, {});\r\n" +
" dropEvent.dataTransfer = event.dataTransfer;\r\n" +
" this.dispatchEvent(jquery(options.dropTarget)[0], type, dropEvent);\r\n" +
"\r\n" +
" /*Simulating drag end*/\r\n" +
" type = 'dragend';\r\n" +
" var dragEndEvent = this.createEvent(type, {});\r\n" +
" dragEndEvent.dataTransfer = event.dataTransfer;\r\n" +
" this.dispatchEvent(elem, type, dragEndEvent);\r\n" +
" },\r\n" +
" createEvent: function(type) {\r\n" +
" var event = document.createEvent(\"CustomEvent\");\r\n" +
" event.initCustomEvent(type, true, true, null);\r\n" +
" event.dataTransfer = {\r\n" +
" data: {\r\n" +
" },\r\n" +
" setData: function(type, val){\r\n" +
" this.data[type] = val;\r\n" +
" },\r\n" +
" getData: function(type){\r\n" +
" return this.data[type];\r\n" +
" }\r\n" +
" };\r\n" +
" return event;\r\n" +
" },\r\n" +
" dispatchEvent: function(elem, type, event) {\r\n" +
" if(elem.dispatchEvent) {\r\n" +
" elem.dispatchEvent(event);\r\n" +
" }else if( elem.fireEvent ) {\r\n" +
" elem.fireEvent(\"on\"+type, event);\r\n" +
" }\r\n" +
" }\r\n" +
" });\r\n" +
"})(jQuery);";
((JavascriptExecutor) driver).executeScript(jqueryScript);
String dragAndDropScript = "jQuery('" + dragSourceJQuerySelector + "').simulateDragDrop({ dropTarget: '" + dropTargetJQuerySelector + "'});";
((JavascriptExecutor) driver).executeScript(dragAndDropScript);
}
Just pass css or xpath selectors in parameters.
Hope that helps you.
If that does not help you I can help you out with some other solutions also.

How to split a NAME and ADDRESS, and print them separately

I'm starting with this String:
"NAME-RAHUL KUMAR CHOUDHARY ADDRESS-RAJDHANWAR DISTRICT-GIRIDIH STATE-JHARKHAND PIN CODE-825412"
I want to split the name and address, and print it like this:
NAME:RAHUL KUMAR CHOUDHARY , DDRESS-RAJDHANWAR DISTRICT-GIRIDIH STATE-JHARKHAND PIN CODE-825412. like this
This is what I have so far:
String str_colArow3 = colArow3.getContents();
//Display the cell contents
System.out.println("Contents of cell Col A Row 3: \""+str_colArow3 + "\"");
if(str_colArow3.contains("NAME"))
{
}
else if(str_colArow3.contains("ADDRESS"))
{
}
String string = "NAME-RAHUL KUMAR CHOUDHARY ADDRESS-RAJDHANWAR DISTRICT-GIRIDIH STATE-JHARKHAND PIN CODE-825412";
String[] parts = string.split("-");
string = "Name: " + parts[1].substring(0, parts[1].length() - 7)
+ "\nAdress: " + parts[2] + " - " + parts[3]
+ "\nPin Code: " + parts[5];
Something like this. Check out the split() method for strings, your string is a bit poorly formatted to use this, though. You have to adjust for your own needs.
Edit: Better way to do this, with different string input.
String string = "RAHUL KUMAR CHOUDHARY:RAJDHANWAR:GIRIDIH:JHARKHAND:825412";
String[] parts = string.split(":");
string = "Name: " + parts[0] + "\n"
+ "Address: " + parts[1] + "\n"
+ "District: " + parts[2] + "\n"
+ "State: " + parts[3] + "\n"
+ "Pin Code: " + parts[4] + "\n";

Is there any possibility to get the currentStockLevel from this method?

I need the currentStockLevel for another void Method in java, is there any possibility to get it?
I think no, because of void right?
public void receive (int currentStock)
{
String outLine;
if (currentStockLevel > 0)
outLine = productCode;
{
outLine = ". Current Stock: " + currentStockLevel;
outLine += " Current Stock changed from " + currentStockLevel;
currentStockLevel += currentStock;
outLine += " to " + currentStockLevel;
int storeCost = wholeSalePrice * currentStockLevel;
System.out.println (productCode + ":" + " Received " + currentStockLevel + "." + " Store Cost " + "$" + storeCost + "." + " New stock level: " + currentStockLevel);
}

Put text from array list to separate lines

I have ArrayList where I put Author, Title and Value(integer). I want to put them in label and print in separate lines. But what I get is that text is printed in one line.
for (Book book : listofbooks) {
labelis.setText(labelis.getText() + "\nName: " + book.getName() + "Tile: " + book.getTitle() + "Number of books: " + book.getHowMany());
}
panel.add(labelis);
Why \n does not work?
EDIT: Solution:
for (Book book : listofbooks) {
label.setText( "<html> "
+ label.getText()
+ "<br>Name: " + book.getName()
+ "Tile: " + book.getTitle()
+ "Number of books: "
+ book.getHowMany()
);
}
label.setText(label.getText()+ "</html>");
You can use HTML tags in your JLabel. Try to seperate your Strings with that break tag: <br>
label.setText("<html> +"
+ label.getText()
+ "<br>Name: " + book.getName()
+ "Tile: " + book.getTitle()
+ "Number of books: "
+ book.getHowMany()
+ "</html>");
NOTE: Your JLabel text has to start with the opening <html> and end with the closing </html>.

How to write a carriage return into xls file in Java

I would like to insert a carriage return into a cell of a xls file.
So I have written this code
address = rs.getString(16) + " " + rs.getString(17) + "
"
+ rs.getString(18) + " " + rs.getString(19) + " (" +
rs.getString(20) + ")";
"writer.write("<ss:Cell><ss:Data ss:Type=\"String\">" + address + "</ss:Data></ss:Cell>");`
but in the Excel File the result is that the carriage return is replaced with a "square symbol". In which mode can I resolve this issue?
Thanks,
Stefano
In excel, to enter a new line in a cell, you need to insert ASCII characters 13 + 10 (constant CrLf on this page : http://msdn.microsoft.com/en-us/library/f63200h0%28v=vs.80%29.aspx).
Have you tried:
String crLf = Character.toString((char)13) + Character.toString((char)10);
address = rs.getString(16) + " " + rs.getString(17) + crLf
+ rs.getString(18) + " " + rs.getString(19) + " (" +
rs.getString(20) + ")";

Categories

Resources