Implementing HTML book-like pagination - android

I found a solution to this somewhat at HTML book-like pagination I am not being able to implement the same on the android using eclipse 3.6. The code I am using is as follows:
mWebView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
// Column Count is just the number of 'screens' of text. Add one for partial 'screens'
int columnCount = Math.floor(view.getHeight() / view.getWidth())+1;
// Must be expressed as a percentage. If not set then the WebView will not stretch to give the desired effect.
int columnWidth = columnCount * 100;
String js = "var d = document.getElementsByTagName('body')[0];" + "d.style.WebkitColumnCount=" + columnCount + ";" + "d.style.WebkitColumnWidth='" + columnWidth + "%';";
mWebView.loadUrl("javascript:(function(){" + js + "})()");
}
});
mWebView.loadUrl("file:///android_asset/chapter.xml");
The LogCat shows that the width and height displayed to me are 0. Am I putting it at the wrong place? Any help would be appreciated.
Thanks

i found this solution and it worked for me. Try Nacho L solution on this HTML book-like pagination?

Related

How to Swipe and Scroll to the Last in Appium

I am getting some Images from the API and i don't know the number and now i want to test the UI in Android through Appium and i want to scroll down to the Last image. How can I do this and also I don't know the what the title from the API is coming so that I can ScrollTo("Title") and also i am not able to swipe to the last. Is there anyway?
There is no way to know for sure whether you've scrolled to the last using appium because there is no UI reporting for the edges of a scrollview.
One way to know that you've reached the end of a scroll view without relying on devs is to compare the list of all children of the scrollview every time you swipe. If all children are exactly the same, you've reached the end. An example xpath for this would look like //android.widget.View[#content-desc="Your scrollview]//*, which would grab all children and descendants. Once you have a list to compare, check the content of all children nodes. This will only work if there's something unique in these items. If all items are completely general, there will be nothing to compare and this won't be reliable. Ask devs to add content descriptions or accessibility data to the items if possible.
Another option would be to have the devs embed a uniquely id'd invisible view at the top and bottom of the scrollview. That way, if it can be found by the driver, you know that you've reached the very edge of the view. If there are already unique elements at the edges of your scrollview, you can use those.
In the end, the devs of the application can really help out the process of scrolling, but hopefully the trick of comparing the current scrollview's children can help you.
You can use the screen dimensions to scroll down:
public void scrollDown() {
Dimension size = driver.manage().window().getSize();
int x = size.getWidth() / 2;
int starty = (int) (size.getHeight() * 0.60);
int endy = (int) (size.getHeight() * 0.10);
driver.swipe(x, starty, x, endy, 2000);
}
You can store all the images into a list using its available locator. Then use driver.scrollToExact(list.get(list.size()).getAttribute("name"));
Example:
List<mobileElement> images = driver.findElementsByClass("<locator>");
driver.scrollToExact(images.get(images.size()).getAttribute("name"));
or
driver.scrollToExact(images.get(images.size()).getText());
#Test
public void testScroll()throws Exception
{
for(int i=0;i<4;i++)
{
Thread.sleep(2000);
if (driver.findElement(By.name("end_item")).isDisplayed())
{
driver.findElement(By.name("end_item")).click();
break;
}
else
{
horizontalScroll();
}
}
}
public void verticalScroll()
{
size=driver.manage().window().getSize();
int y_start=(int)(size.height*0.60);
int y_end=(int)(size.height*0.30);
int x=size.width/2;
driver.swipe(x,y_start,x,y_end,4000);
}
The above example works with vertical scroll and it is based on the example given at this blog for horizontal scroll
http://qaautomated.blogspot.in/2016/02/how-to-do-horizontal-scroll-in-appium.html
I hope this works for you.
To do this you must know resource id or cont-desc of scrollable element. You also need to know className of your scrollable element.
If you have cont-desc in scrollable list
try {
String scrollableList="your con-desc of scrollable List";
String elementClassName="android.something.something";
String anyText="any text";
driver.findElement(MobileBy.AndroidUIAutomator(
"new UiScrollable(new UiSelector().description(\"" + scrollableList + "\")).getChildByText("
+ "new UiSelector().className(\"" + elementClassName + "\"), \"" + anytext + "\")"));
}catch (Exception e){
System.out.println("Cannot scroll further");
}
If you have resource-id in scrollable list
try {
String scrollableList="your con-desc of scrollable List";
String elementClassName="android.something.something";
String anyText="any text";
driver.findElement(MobileBy.AndroidUIAutomator(
"new UiScrollable(new UiSelector().resourceId(\"" + scrollableList + "\")).getChildByText("
+ "new UiSelector().className(\"" + elementClassName + "\"), \"" + anytext + "\")"));
}catch (Exception e){
System.out.println("Cannot scroll further");
}
If the screen cannot be scroll further it will throw error which will be catched by catch block.

How to display Html table in textview?

I want to display text inside textview using this code:
Html.fromHtml("<html><body><table style=width:100%><tr><td><B>No</td><td><B>Product Name</td><td><B>Qty</td><td><B>Amount</td></tr></body></html>");
But result is not in correct format result look like this:
NoPRoductNameQtyAmount
please suggest what i am doing wrong in this code.
fromHtml() does not support <table> and related tags. Your choices are:
Reformat your text to avoid tables
Use WebView to render your HTML table
Use native widgets and containers (e.g., TableLayout) for your table
Instead of using html table inside a TextView I've solved formatting normal text into a table like text, adding white spaces into the text to have a tidy structure.
This is the code:
String lines[] = getItem(position).toString().split("\n");
String print = "";
String parts[] = null;
String tmp = "";
int weight = 0;
for (int i=0; i < lines.length; i++) {
if (!lines[i].equalsIgnoreCase("")) {
parts = lines[i].split(":", 2);
tmp = "";
Paint textPaint = text.getPaint();
float width = textPaint.measureText(parts[0]);
float wslength = textPaint.measureText(" ");
for (int j = 0; j < (220 - Math.round(width))/Math.round(wslength); j++) {
tmp = tmp + " ";
}
if (print.equalsIgnoreCase("")) {
print = print + parts[0] + tmp + Html.fromHtml(""+parts[1]+"");
} else {
print = print + "\n" + parts[0] + tmp + parts[1];
}
}
}
You can find more explanations here:
http://blog.blupixelit.eu/convert-text-to-table-in-android-sdk/
Hope this helps.
How about change the way to just render table by webview, the other tags render by textview ? Recently i finish a demo to overcome this. So we first need separate<table> with others supported tags, which will like change "<p>**<p> blabala <table>balabla </table> blabla <p>**<p>" to three separated strings
<p>**<p> blabala
<table>balabla </table>
blabla <p>**<p>
Then only <table> included tags render by webview, the others by textview
And the result in android will be like:
<ScrollView>
<LinearLayout>
<TextView>
<WebView> --- let the webview ATMOST measured
<TextView>
So every thing goes fine as i think. check this commit for detail
HtmlTextView recently added basic support for HTML tables. It's limited, but will do the trick if all you have to worry about is <table>, <td>, <tr>, and <th>.

android webview p tag to have 100% width

I have a webview which has a p tag inside and the webview itself is has 90% of the device width by default, but I want that p tag to have a 100% width to fill the whole webview. I tried giving it via style:
<p style="width:100%"></p>
but this throws UnknowFormatConversionException. Here is my code to fill the webview with content:
String head = "<head><style>#font-face {font-family: 'byekan';src: url('fonts/BYekan.ttf');}body {font-family: 'Tahoma';}</style></head>";
String text = "<html>" + head + "<body style=\"background:#e4322f;min-height:" + minHeight + "\"><p style=\"width:100%;background:blue;direction:" + dir + ";font-family:byekan;color:#fff;text-align:justify;float:" + flt + "\"> %s </p></body></Html>";
wv.loadDataWithBaseURL("file:///android_asset/",String.format(text, _content), "text/html", "utf-8", null);
it works fine if I remove that "width:100%", but the p tag will takes the text width but I want it to fill the webview width, how can I achieve that? thanks in advance.

Add a number and a Text Input value with adobe flex

I am trying to add a number and a text input value to display in a label. here is my code thus far.
'lblAnswer.text = bloodglucose + 100;'
Please tell me what I am doing wrong.
Please try following answer -
bloodglucose += 100;
lblAnswer.text = String(bloodglucose);
Hope this will work :)
Sunil is correct - when doing mixed type addition, the UI input first needs to be coerced to either int or Number. IE: Number(bloodglucose) + 100; This assumes bloodglucose is actually a getter to the input text reference. If it's not, then you need to coerce the property and not the id of the component.
Getter: public function get bloodglucose():Number { return Number(myInput.text); }
In method: lblAnswer.text = bloodglucose + 100;
or (bloodglucose is a UIComponent):
In method: lblAnswer.text = Number(bloodglucose.text) + 100;
You should use String(int i)
lblAnswer.text = String(bloodglucose + 100);
Update: What about something like this:
var i:int = bloodglucose + 100;
var s:String = String(i);
lblAnswer.text = s;
** Update ,
I am changing the code from the update that was previously posted. I initially found that because I was including the string value inside of the equation this is what was prompting an error. You have to wrap the converted components to Number inside of the string all together. Basically convert the components to a number, then convert the answer received into a string.
Below is an example of the wrong code.
txtAnswer = (String(Number(bloodglucose)+100)) / 36)).toFixed(2)
Below this line is the fixed code.
txtAnswer.text = String( (Number(bloodglucose.text) + (Number(100))/ (Number(36))).toFixed(2) ;
The .toFixed Property signifies how many decimal places I want the returned value to display.

Using size HTML attribute in TextView

I have the following:
textView.setText(Html.fromHtml("<font color=\"red\" size=\"24\">Hello</font>"));
The string 'Hello' does turn red but the size does not change.
It is as if the size attribute is just ignored, does anyone know why this is? Am I doing something wrong?
Size attribute seems not working.
You can use <small> or <big> (multiple times to increase the effect)
You can also use <h1> to <h6> (Header only, i.e. add a new line)
Its old-fashion, but it works well !
Yes, size attribute just ignored. Only "color" and "face" attributes are taken into account.
From Html class sources:
private void handleStartTag(String tag, Attributes attributes) {
if (tag.equalsIgnoreCase("br")) {
// We don't need to handle this. TagSoup will ensure that there's a </br> for each <br>
// so we can safely emite the linebreaks when we handle the close tag.
}
...
else if (tag.equalsIgnoreCase("font")) {
startFont(mSpannableStringBuilder, attributes);
}
...
}
private static void startFont(SpannableStringBuilder text,
Attributes attributes) {
String color = attributes.getValue("", "color");
String face = attributes.getValue("", "face");
int len = text.length();
text.setSpan(new Font(color, face), len, len, Spannable.SPAN_MARK_MARK);
}
Try this one,Its working for me,use small,big key words
TextView mBox = (TextView) findViewById(R.id.txt);
mBox.setText(Html.fromHtml("<font color=#cc0029>" + "<b>"
+ "Hiiiiiiiiii" + "</b>" + "<br />" + "<small>" + "description"
+ "</small>" + "<br />" + "<small>" + "DateAdded" + "</small>"));
Sergey Gotov is right. The only way to change text size it to use h1 - h6 tags.
EDIT: You can also implement TagHandler and use your own tags.
Look at Formatting and Styling on the android developpers site:
http://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling
Or, on this old post of StackOverflow :
Highlighting Text Color using Html.fromHtml() in Android?

Categories

Resources