2

I would like to have another string.xml in the values folder.

I want to keep the urls in a different file so are not editable. My plan is to have a default url in those files, commit the files and add them to git ignore.

I've tried creating an url.xml file in the values folder and add the following items:

<resources>
    <item name="device_ip_address" type="string">0.0.0.0</item>
</resources>

I tried also with strings:

<string name="device_ip_address_key" translatable="false">0.0.0.0</string>

But when I try to get the string in the PreferenceScreen for example, I get a compilation error:

<PreferenceScreen
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <Preference
        app:key="@string/device_ip_address_key"
        app:title="@string/device_ip_address_title"
        app:summary="@url/device_ip_address"
        app:defaultValue="@url/device_ip_address"
        app:enabled="true"
        />
</PreferenceScreen>

or in the activity:

val myUrl = getString(R.string.device_ip_address)

Any idea how I can achieve this? Thanks

2
  • 1
    What error are you getting? Commented Jul 18, 2019 at 16:09
  • 1
    what is the error you are getting?? having that information is kind of needed to help you out because all of your code from the looks of that seems to be fine and you shouldnt have a problem Commented Jul 18, 2019 at 16:20

1 Answer 1

1

I've tried creating an url.xml file in the values folder and add the following items:

This code is perfectly fine. I just added it to a scrap project have no difficulty accessing that resource from another resource:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context=".MainActivity">

  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/device_ip_address"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

...or from code:

package com.commonsware.myapplication

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle

class MainActivity : AppCompatActivity() {

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val foo = getString(R.string.device_ip_address)
  }
}

But when I try to get the string in the PreferenceScreen for example, I get a compilation error:

There is no url resource type. Use @string/device_ip_address.

Sign up to request clarification or add additional context in comments.

3 Comments

But is the device_ip_address defined in other file than strings.xml? I want to be defined in totally separately file from string.xml.
@xarly: "But is the device_ip_address defined in other file than strings.xml?" -- yes. I created a url.xml file in res/values/.
Wow, I think I've done something wrong in my first code. Thanks a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.