Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android How to add a custom xml file in res/values and how to register the customvalues.xml with the system

Tags:

I want to add a custom xml which having URLs to the res/values folder. I can add the file but how to read its content by getResources() ?

like image 978
Gayan Kalanamith Avatar asked Mar 01 '13 07:03

Gayan Kalanamith


People also ask

How do I create a custom XML file?

Create a new custom XML file by selecting File > New or by clicking the Create a new custom XML file link in the right-hand pane. The New Custom File dialog is displayed. 3. Save the new custom XML file by selecting File > Save As and then specifying the name and location of the file.

Where is values XML file in Android Studio?

You are changing values in file which is generated by Android Studio, so it is changed every time you build your project. All files inside build folders are generated and there is no need in changing them. Instead you should put your values in values. xml under app->src->main->res->values .

Does Android studio support XML?

Android provides a straightforward XML vocabulary that corresponds to the View classes and subclasses, such as those for widgets and layouts. You can also use Android Studio's Layout Editor to build your XML layout using a drag-and-drop interface.

What is res folder in Android?

The res/values folder is used to store the values for the resources that are used in many Android projects to include features of color, styles, dimensions etc.


2 Answers

Update: This doesn't seem to work anymore (but it used to) and you cannot create your own custom types in android. Only the standard available resource types work now.

Therefore, the only way to achieve something like this would be, have your separate file as suggested, url.xml and have all yours URLs in that file so that it doesn't get mixed up with the other Strings. It just improves readability and maintainability, AFAIK.

The URL item now looks like,

<item name="myUrl" type="string">http://myUrl.com</item> 

And you needs to be accessed the usual way:

String myurl = getResources().getString(R.string.myUrl); 

Original Answer:

Try something like this:-

url.xml

<?xml version="1.0" encoding="utf-8"?> <resources>     <item name="myUrl" type="urls">http://myUrl.com</item> </resources> 

And in your activity, get it like this:-

String s = getResources().getString(R.urls.myUrl); 

Note:- You needn't register your xml anywhere. Just make sure its available in res/values folder.

Snapshot:-

How I used

like image 152
Rahul Avatar answered Oct 10 '22 14:10

Rahul


  1. Create your custom xml file, ensure it is in res folder (I've tested this by putting it in res/values folder)
  2. Ensure all the values you place in this XML are of a standard type.
  3. You can then use the corresponding R call. For example for integer you do R.integer.YOUR_CUSTOM_XML_VALUE. Doesn't matter which xml this integer is located in, it will be registered by R as long as it is in res folder.
like image 25
Molten Ice Avatar answered Oct 10 '22 14:10

Molten Ice