0

I'm trying to change the value of a resource, but many post say you can not change, I think if you can, but do not know how yet.

In the Xml I have something like this:

   <resources xmlns:android="http://schemas.android.com/apk/res/android">
        <integer name="textSize">11</integer>
    </resources>

to obtain the value

int update_str = this.getResources().getInteger (R.integer.textSize);

but to change it?

???

I can get all the textviews and change the parameter individually, but if we have 1000 textviews on each page, I think it is a bit forced and drastic to the% cpu hardware.

John Palmer
  • 25,356
  • 3
  • 48
  • 67
Calimbo
  • 81
  • 1
  • 3
  • 13

3 Answers3

2

This is not supported, and not the way you should handle changing the value of the TextViews. Resources are final in your APK, if you want to change the text in the TextViews you can define all resources you need and pass them to the TextView in your code, retrieving them as you described via getResources().

AlexWalterbos
  • 408
  • 4
  • 12
  • Then as I can load a resource, my TextView, then change it? but only do 1 time for all, (I do not want to have to change for 1000 textviews, is heavy to the% cpu) – Calimbo Apr 19 '15 at 12:42
  • The reason it's heavy is because you have 1000 `TextViews` in one layout, try to lower that amount. Changing the text of a `TextView` is not very intensive on the cpu. – AlexWalterbos Apr 19 '15 at 12:44
  • There is intensive continually change pages back forward back forward back forward ..... .., I use this function: http://stackoverflow.com/questions/10766716/set-font-for-all-textviews-in-activity – Calimbo Apr 19 '15 at 12:48
  • Can you expand on what you are trying to do in this layout? – AlexWalterbos Apr 19 '15 at 13:14
  • I try to only change the text depending on the resolution manually (without ldpi , mdpi , hdpi ) now what I have by code, depending on the resolution and apply one or the other, but des of resources, to be not always loading – Calimbo Apr 19 '15 at 14:06
  • for example textview set resources id (@Integer/textsize/1/2/3/4.. ) – Calimbo Apr 19 '15 at 14:10
  • I'm sorry, I don't really get what you are trying to do. You can define layouts for different screen sizes, orientations and screen densities using [this](http://developer.android.com/guide/practices/screens_support.html#range) approach. Please extend your question with an explanation of what it is supposed to do, and why you choose to follow your approach instead of using methods like size/density-dependent layouts. – AlexWalterbos Apr 19 '15 at 20:55
0

The value is hardcoded in your application source files, so there is absolutely no way to change in during runtime.

If you want the value to be changed during runtime, you should save the value in SharedPreferences and get it from there every time you need it (Though that means that you can't use it in XML, only in Java)

Moonbloom
  • 7,738
  • 3
  • 26
  • 38
0

but many post say you can not change

This is because you can't! The xml values are final inside your apk by design.

if we have 1000 textviews on each page, I think it is a bit forced and drastic to the% cpu hardware

I don't think it's a good idea to have 1000 TextView on any page. But even if you have to, there are many programatic ways to achieve this other than changing final value of String.

For example you can share a single object reference with all your textviews, this object will have a NON final instance of a String, that you will be abele to manipulate. Read about MVC to understand this idea.

Ilya Gazman
  • 31,250
  • 24
  • 137
  • 216