tags:

views:

47

answers:

1

I've posted a bigger chunk of the code below. You can see that initially QUOTE was procedural- coded in place. I'm trying to learn how to use declarative design so I want to do the same thing but by using resources. It seems like I need to access the string.xml thru the @R.id tag and identify QUOTE with that string value. But I don't know enough to negotiate this. Any tips? Thanks!

public class circle extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(new GraphicsView(this)); }

static public class GraphicsView extends View {

  //private static final String QUOTE = "Happy Birthday to David.";

   private final String QUOTE = getString(R.string.quote);

..... @Override protected void onDraw(Canvas canvas) { // Drawing commands go here

     canvas.drawPath(circle, cPaint);
     canvas.drawTextOnPath(QUOTE, circle, 0, 20, tPaint);
A: 

Basically I want to have the same text but as a resource in strings.xml

At minimum, you heed a file named res/values/strings.xml with the following:

<resources>
  <string name="quote">Happy Birthday to David.</string>
</resources>

You then use getString(R.string.quote) to get at the string value in your Java code. Note that you already probably have a res/values/strings.xml file, created for you when the project was created, so just add the <string> element shown above alongside the other such elements in that file.

Can someone please educate me a little and recommend a good resource for these type of questions? I'm using HelloAndroid at the moment and I also have "Beginning Android".

String resources are covered on pages 175-180 of Beginning Android. A current version of the sample project from that section can be found here.

CommonsWare
Ok, great! Thanks for the time spent answering this- I really appreciate it! I have created the string.xml file already, but using getString(R.string.quote) is where I have the problem:I get this error:Cannot make a static reference to the non-static method getString(int) from the type Contextwhen I use this code: private static final String QUOTE = getString(R.string.quote);Thats why I tried the way I pasted in O.P.So I guess that I'm instantiating a variable of the wrong type.I haven't read the pages you recommend, so thats what I'm going to do right now.Thanks again!Dave.
@user356812 - `getString()` is an instance method, so you can't use it in a static initializer.
CommonsWare
I still haven't found a solution- can anyone suggest a way of creating an instantiation of getString that can be used in a static initializer? I've still got a lot of reading on net to do, but you could save me alot of time if you have a good explanation ;)
@user356812 - It is not possible to use `getString()` in a static initializer.
CommonsWare
Ok, so I've been reading Beginning Android as suggested in the first answer. It looks like I need to use String.format() so I'm going to play with that for a while.
no, this doesn't seem like the way. I've sort of hit a wall on this one. I hate to leave it and just let it go, so any help would be appreciated
Ok, I think I've got it. I just removed the static parameter from static public class GraphicsView. N ow I can use getString(). But I wonder what consequences this could have in a more complex program? Why was the class 'static' in the first place?
@user356812 - in a `View`, use `getContext().getString()`, and you will need to call that in your constructor (after chaining to the superclass) or later. Otherwise, your `Context` will not be set up just yet. Bear in mind that creating custom `View` classes is a rather advanced technique that few developers ever use.
CommonsWare
Ok, I'll give this a go. I think that next I'm going to try and do the job of canvas.drawtext in layout.xml This may be easier/cleaner.
thankyou by-the-way for all your help!