reading values for the layout from JSON file - android

I want to parse a JSON file generated by my RESTful application and use values from that file in order to change some variables of the layout/style.xml like the color of the background. I really need some help with it.

If you want to convert your JSON file to java objects you can use gson (http://code.google.com/p/google-gson/). On the other hand the layout/style.xml file cannot be modified in run-time. If you want to change the background color or any other UI property in run-time you have to do it programatically. For example you can define all the necessary styles in your layout/style.xml and when you converted your JSON file into java objects with gson, you can apply the corresponding style depending on the JSON response.

Related

json map style parsing failed in android

I want to create a JSON file to style my google map but I get this message: E/Google Maps Android API: Map style parsing failed: org.json.JSONException: Value res of type java.lang.String cannot be converted to JSONArray
E/MapsActivityRaw: Style parsing failed. Any help?
Here is my code example where I have the method with JSON creation and on left side you see the raw package where the JSON file
Here is the error that I get
I used Google platform mapstyle.withgoogle.com. But the thing that I'm not sure about, that I created a Text Document then changed the ending to .json, copied the JSON code from google platform and put it there and then copied the file and put it in android studio. Because I couldn't know how to create a JSON file in android studio. I don't know if it sounds the right way to do that?
in the previous comment that I left you, it was because I did not have the solution, but in reviewing the google documentation well I realized that there are two types of codes to implement the styles, one by JSON files and others by means of string
For the code that you show in the image it has to have a style_gray string resource, for this case I would suppose that you would have an xml file or that variable in the string.xml, you could use that same code like this:
googleMap.setMapStyle(new MapStyleOptions(getResources().getString(R.raw.map_style_aubergine)));
For that error to be solved, you would have to implement this other code, which if it is compatible with JSON files in the format generated by https://mapstyle.withgoogle.com/, the code would be the following:
googleMap.setMapStyle(MapStyleOptions.loadRawResourceStyle(this,R.raw.map_style_aubergine));
In my case the map_style_aubergine is the JSON file and a variable in the string.xml you can use this as an example and make the respective corrections in your code

Generating fast JSON by EditableText Fields

I would like to create something similar to Inputs of HTML form. Each input has a name and value attribute which is nicely playing together.
However so far I have just found android:tag which might be utilized as name attribute of inputs.
<EditText android:id="#+id/passwordEdit"
android:tag="name"
android:inputType="textPassword"/>
Simply what I would like to create is building a JSON Object by only EditText's as this:
jsonObject.put(editableField.getXXX, editableField.getText())
Please note that the point is here just using layout files in order to build a JSON Object.
Are there any convention for such kind of usage or another appropriate attribute for storing dummy values?
Thanks

How to add R.drawable resources to an array for android?

I would like to have sequence of set R.drawable been called in integer array so that it can be accessed later with choice.But I'll like to load to the R.drawable dynamically which corresponds to different names.It names needs to be had as per the external value input not hardcoded.I tried this in making many but like use list,set,array conversion etc.Kindly guide me with a snippet or example on this regard.Thank you.
I suppose, that best way to achieve this funcionality is by using ArrayAdapter of Integer.
You will simply add drawble resources by id.
If you need to get resource id, but you will get string input from user, you can use this code:
Android, getting resource ID from string?

Android and XML maps

Can you create an XML object resource with key-value pairs (basically a hashmap) using an XML file analogous to arrays.xml? This would be an analogue of NSDictionary from Obj-C.
i.e.
<key>foo</key>
<value>bar</value>
I'd like for the value to be an array of strings, and I'd like to be able to copy and paste from XML files in my Obj-C projects.
Thanks.
A good alternative is to create JSON objects, and access these using GSON. If you already have a PList resource from an iPhone project, one way to convert to JSON is to create an object from the PList and write it to a JSON string file.

Creating java.util.Map in Android

I want to create a java.util.map in android from a resource. I want to do this because I have a lot of entries to populate into the java.util.map and I want to store the values in the res folder of the project in xml format.
Is there an effecient way to do this in android? My map will have around 2500 entries so I want to do this as effeciently as possible and I don't want to hard code them...
Thanks,
Gaz
I think the Xml format is "too much" to simply store key-value pairs. A text file where a line is a key-value pair is more adequat (e.g. with comma separator), also the parsing will be easier.
foo1,bar1
foo2,bar2
...
Save your text file in res/raw directory
Open it via context.getResources.openRawResource(R.raw.fileName)
Loop on each lines and split the line to retrieve the key and the value.
Put them in your map
That's all

Categories

Resources