• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.example.android.apis.content;
18 
19 // Need the following import to get access to the app resources, since this
20 // class is in a sub-package.
21 import com.example.android.apis.R;
22 
23 import android.app.Activity;
24 import android.content.Context;
25 import android.content.res.Resources;
26 import android.os.Bundle;
27 import android.widget.TextView;
28 
29 
30 /**
31  * Demonstration of loading resources.
32  *
33  * <p>
34  * Each context has a resources object that you can access.  Additionally,
35  * the Context class (an Activity is a Context) has a getString convenience
36  * method getString() that looks up a string resource.
37  *
38  * @see StyledText for more depth about using styled text, both with getString()
39  *                 and in the layout xml files.
40  */
41 public class ResourcesSample extends Activity {
42     @Override
onCreate(Bundle savedInstanceState)43 	protected void onCreate(Bundle savedInstanceState) {
44         super.onCreate(savedInstanceState);
45 
46         // See res/any/layout/resources.xml for this view layout definition.
47         setContentView(R.layout.resources);
48 
49         TextView tv;
50         CharSequence cs;
51         String str;
52 
53         // ====== Using the Context.getString() convenience method ===========
54 
55         // Using the getString() convenience method, retrieve a string
56         // resource that happens to have style information.  Note the use of
57         // CharSequence instead of String so we don't lose the style info.
58         cs = getText(R.string.styled_text);
59         tv = (TextView)findViewById(R.id.styled_text);
60         tv.setText(cs);
61 
62         // Use the same resource, but convert it to a string, which causes it
63         // to lose the style information.
64         str = getString(R.string.styled_text);
65         tv = (TextView)findViewById(R.id.plain_text);
66         tv.setText(str);
67 
68         // ====== Using the Resources object =================================
69 
70         // You might need to do this if your code is not in an activity.
71         // For example View has a protected mContext field you can use.
72         // In this case it's just 'this' since Activity is a context.
73         Context context = this;
74 
75         // Get the Resources object from our context
76         Resources res = context.getResources();
77 
78         // Get the string resource, like above.
79         cs = res.getText(R.string.styled_text);
80         tv = (TextView)findViewById(R.id.res1);
81         tv.setText(cs);
82 
83         // Note that the Resources class has methods like getColor(),
84         // getDimen(), getDrawable() because themes are stored in resources.
85         // You can use them, but you might want to take a look at the view
86         // examples to see how to make custom widgets.
87 
88     }
89 }
90 
91