I'm parsing some XML with XPath.
The XML code follows:
<?xml version="1.0" encoding="UTF-8"?>
<response>
<teldir>
<contact>
<nameDept>D'ADAMO, Piergiorgio</nameDept>
</contact>
</teldir>
</response>
Using the expression /response/teldir/contact/nameDept/text() I put the result in a Java String with Node.getNodeValue().
This string is shown in a ListView using a custom but simple layout for each item.
<TextView
android:id="#+id/contact_name_dept"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:textSize="20sp"
android:textColor="#FFFFFF"
/>
I have a unit test asserting the string is "D'ADAMO, Piergiorgio".
The problem is that only when the code runs in the emulator the item in the ListView shows "D'".
It seems that Node.getNodeValue() is truncating the string when the apostrophe occurs.
Maybe the Node DOM implementation in Android has issues?
The XPath specification requires that adjacent text nodes are concatenated, so using /text() should never give you half a text node. Unfortunately there are some careless implementations around, and if they run on a DOM that has multiple adjacent text nodes (as can often happen when entities are involved) they don't go to the trouble of merging them. It's a non-conformance and you should complain about it, but you'll be lucky to get it fixed. Meanwhile, try to get out of the habit of using /text() in your XPath expressions - it's nearly always bad practice. Instead, get the string value of the containing element using string(/response/teldir/contact/nameDept). It's very unlikely that any XPath implementation will get that one wrong (I hope!).
Related
I have the XML layout code:
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:bind="http://schemas.android.com/apk/res-auto">
<data>
<variable name="info" type="com.mycompany.orm.binders.MyBinderObject"/>
</data>
<android.support.percent.PercentRelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="15dp"
android:paddingBottom="15dp"
android:gravity="end">
...
And this works great. However, I have a property inside MyBinderObject called mygravity, and it is set to one of two strings: start or end. I am trying to figure out how to read the Data Binding property mygravity to assign the gravity to the layout.
I.e., android:gravity="#{info.mygravity}" (However, this line does not work. The compile-time databinding code fails).
Does anyone have an idea how to dynamically set the android:gravity based on a Data-bound objects property?
I have a property inside MyBinderObject called mygravity, and it is set to one of two strings: start or end.
Therein lies the problem: you're doing something reasonable and treating the layout XML like XML.
The layout XML attribute values can be of a variety of types. Sometimes, this is obvious, such as android:enabled="false" or android:layout_width="30sp". And, sometimes, the type really is a string or string resource reference (e.g., android:text="Foo"). But sometimes the attribute value looks like an English-language string, but it is really one of a set of enumerated values (e.g., gravity values).
I didn't realize that gravity was actually an enumerated attribute
A rough-cut rule of thumb: if the build would fail if you translated the value into another language, then it is an enumerated attribute (e.g., android:gravity="fin" instead of android:gravity="end", assuming that Google Translate gave me reasonable Spanish there...). If the translated value would work just fine, then it's any valid string or (usually) string resource.
I am trying to figure out how to read the Data Binding property mygravity to assign the gravity to the layout.
You will have to transmogrify the string values into equivalent Gravity constants like Gravity.START.
you can definitely use android:gravity="#{info.mygravity}" in layout file but make sure that mygravity should be int value.
you should refer this
like if you want center gravity, value of info.mygravity should be Gravity.CENTER
I have spent the last few weeks working on my first Android Application, after studying Java in school. I have managed to make a functional interface with access (limited, but we're getting there!!) to a database. I am now hoping to integrate an online API into my App, but I'm really not sure where to start. I appreciate any help you can give, even if you can simply point me in the right direction for research.
I am using the USDA National Nutrient Database API (http://ndb.nal.usda.gov/ndb/foods) and in my App I have got a TextEdit and "Search" Button which should allow the user to search the database for results. I then want these results to populate a Spinner with a choice of all results that contain the searched text. From here the user should be able to specify an AMOUNT of the food type, and see the nutritional information for eg. 100g, 1 item, 5lb etc. Finally, I want to import the Nutritional Data from the Online Database and place them in a DataBase. I have got a API KEY.
Now I feel like I should be able to do much of this on my own, but what I need help with is actually accessing and recovering the data from the site. Unfortunately I have not used an API before, and I don't know where to begin. I would really appreciate any help you can give me in this, even if you could point me in the right direction for resources and tutorials.
I am including my xml code so far for reference.
I have chosen not to include my Java for this Activity, as it lacks most functionality as I do not know how to query the database through the API. If you think it will be helpful I will append it, but it's mostly just initialisation statements, getting the values from the text boxes, and initialising buttons with no onClick methods defined yet.
This search box allows the user to type keywords which will then be searched in the database
<EditText
android:id="#+id/food_search"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/food_search_hint" />
This Search Button will send the request to query the database for the String from the food_search EditText
<Button
android:id="#+id/food_button_search"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/food_button_search" />
This Spinner will be populated with the results from the database. The line in italics is a placeholder inserted to test that the Spinner is functional.
<Spinner
android:id="#+id/food_spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
*android:entries="#array/test_food_spinner"*
android:background="#android:drawable/btn_dropdown"
android:spinnerMode="dropdown" />
A second, numerical EditText to add the amount/weight of food
<EditText
android:id="#+id/food_amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="2"
android:hint="#string/food_number_hint"
android:inputType="number"/>
This Spinner holds values such as "g", "kg", "lb", "units". I will create a method to multiply the results from the database by the values specified in the food_amount textEdit and this spinner
<Spinner
android:id="#+id/food_amount_spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:entries="#array/test_food_amount_spinner"
android:background="#android:drawable/btn_dropdown"
android:spinnerMode="dropdown" />
And finally a submit button to add the name and nutritional information from the online database to a local database
<Button
android:id="#+id/food_submit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/food_button_submit"
/>
Again thank you so much, I really appreciate any help you can give me on this. I am an eager student but I simply don't know where to turn for information on this subject. <3
This is your documentation:
http://ndb.nal.usda.gov/ndb/doc/apilist/API-LIST.md
Have a look at how you should build new customized requests.
This is sample request:
http://api.nal.usda.gov/ndb/list?format=json<=g&sort=n&api_key=xoNloOitF8uXEhuREu11T7y64Lz1tntsZGHcZwPs&location=Denver+CO
It returns you a JSON string.
You can make HTTP-requests with okhttp.
Then you can parse your results and marshall it to objects.
You can do it manually with JSONObject class or use nice json libraries like gson or jackson (my personal favourite is gson).
I'll give you a simplified example, and you shall continue on.
Open
http://api.nal.usda.gov/ndb/list?format=json<=g&sort=n&api_key=xoNloOitF8uXEhuREu11T7y64Lz1tntsZGHcZwPs&location=Denver+CO
Copy contents and paste to http://jsonviewer.stack.hu, have a look
at the structure
Use http://www.jsonschema2pojo.org/ to generate your java classes to
which you will marshall your json-response
Using gson or jackson convert you response to objects, for example,
like this:
--
Gson gson = new Gson();
FoodResponse fr = gson.fromJson(jsonResponseString, FoodResponse.class);
Show your objects in UI (all your spinners etc)
I found a library which allows an app to wrap text around an image - however once implemented it changed the size of my text - how can the text size be increase when using this library?
android:textSize= has no impact on the text size.
Neither does:
FlowTextView titleTv = (FlowTextView) findViewById(R.id.titleTv);
((FlowTextView) findViewById(R.id.titleTv)).setTextSize(20);
https://code.google.com/p/android-flowtextview/
Example:
<com.pagesuite.flowtext.FlowTextView
android:id="#+id/titleTv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:text=""
android:textSize="20sp" >
In the short term a call to invalidate will probably get it working:
FlowTextView titleTv = (FlowTextView) findViewById(R.id.titleTv);
titleTv.setTextSize(20);
titleTv.invalidate();
However, I suspect you are using the JAR file right? It is quite out of date so I would recommend checking the source code out and using it as an android library project - setTextSize() should work properly then without needing a call to invalidate() (plus various other bug fixes etc).
Also - I never added the ability to set the text size via XML - wouldn't be too hard to add this though.
I checked the code of android-flowtextview and they have the text size hardcoded (check line 131 here). You have to change their source code or use the public method setTextSize(int).
Also, this link might help you, as seems that someone already did something as you are trying to do.
https://code.google.com/p/android-flowtextview/source/browse/trunk/src/com/pagesuite/flowtext/FlowTextView.java
There's a 'setTextSize(int)' method that should do exactly what you're looking for.
If you want to set it from XML, it's a little more involved. Since FlowTextView's constructors ignore the AttributeSet that gets passed in, you'll have to code this yourself. Follow guides like this: http://kotikan.com/blog/posts/2012/09/android-attributes to figure out how to add custom attributes to your views.
Need to add it to textview, for example, standard java-code.
What do you exactly mean? You may want to change Typeface of TextView to monospace, this will make your TextView look similar to code block here at stackoverflow.
This font is monospace
Code to make it look like this:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:typeface="monospace"
android:text="This font is monospace" />
or in Java:
yourTextview.setTypeface(Typeface.MONOSPACE, Typeface.NORMAL);
Edit:
So, you need some Java code formatting library, the best would be if it returned result in HTML code, so it could be easily put into TextView.
I don't know if there is something like this for free, but here are few resources you may be interested in:
How to pretty print XML from Java?
Stand-alone Java code formatter/beautifier/pretty printer?
Edit2:
You may also use one of these JavaScript libraries:
http://alexgorbatchev.com/SyntaxHighlighter/
http://code.google.com/p/google-code-prettify/
along with custom WebView to show pretty formatted code.
I am going through the android hello world tutorial, and have been stuck when trying to create an XML UI. For some reason, even on a new program, in which I have made no changes to the default build, it gives the error java.lang.NullPointerException after every character I type. I can't figure out why it is doing this, as I am just trying to edit the text between the Text I want to set it to say something other than what is set by default. However, even with a fresh build, no changes, and I just try to change the text within the xml tags, it still gives the error. What do I need to do to allow it to let me type? I am using the eclips IDE and the android sdk. I was able to do the first part of the tutorial that doesn't involve XML.
Just guessing, but I suspect you are doing something like the following:
<TextView android:text="#string/hello" />
and you are editing it to
<TextView android:text="#string/helloWorld" />
without creating a reference in res/values/strings.xml .
If this is the case, go to strings.xml and edit the proper string there, for example
<string name="hello">Hello World!</string>
becomes
<string name="hello">Hello everybody!</string>
Ok, I finally found the answer somewhere else, it was something wrong with how the file was created by default. I have to add the element xmlns:android as follows <resources xmlns:android="http://schemas.android.com/apk/res/android" />
Interestingly enough, the file will work if I type it one character at a time, dismissing the pop up with each keystroke. However the new element eliminates the pop ups. I am not sure why the error would pop up, yet the program still compile and run correctly on my avd. Oh well, if you have this error add the element and it goes away
I think in your typing contain UTF-8 not pure ASCII. You can change in eclipse by
in Ecipse IDE Window> preference> Under General tab, select workspace.
In text file encoding choose other, in these choose UTF-8 . It will be ok