Home | History | Annotate | Download | only in content
      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 {
     43     @Override
     44 	protected void onCreate(Bundle savedInstanceState)
     45     {
     46         super.onCreate(savedInstanceState);
     47 
     48         // See res/any/layout/resources.xml for this view layout definition.
     49         setContentView(R.layout.resources);
     50 
     51         TextView tv;
     52         CharSequence cs;
     53         String str;
     54 
     55         // ====== Using the Context.getString() convenience method ===========
     56 
     57         // Using the getString() conevenience method, retrieve a string
     58         // resource that hapepns to have style information.  Note the use of
     59         // CharSequence instead of String so we don't lose the style info.
     60         cs = getText(R.string.styled_text);
     61         tv = (TextView)findViewById(R.id.styled_text);
     62         tv.setText(cs);
     63 
     64         // Use the same resource, but convert it to a string, which causes it
     65         // to lose the style information.
     66         str = getString(R.string.styled_text);
     67         tv = (TextView)findViewById(R.id.plain_text);
     68         tv.setText(str);
     69 
     70         // ====== Using the Resources object =================================
     71 
     72         // You might need to do this if your code is not in an activity.
     73         // For example View has a protected mContext field you can use.
     74         // In this case it's just 'this' since Activity is a context.
     75         Context context = this;
     76 
     77         // Get the Resources object from our context
     78         Resources res = context.getResources();
     79 
     80         // Get the string resource, like above.
     81         cs = res.getText(R.string.styled_text);
     82         tv = (TextView)findViewById(R.id.res1);
     83         tv.setText(cs);
     84 
     85         // Note that the Resources class has methods like getColor(),
     86         // getDimen(), getDrawable() because themes are stored in resources.
     87         // You can use them, but you might want to take a look at the view
     88         // examples to see how to make custom widgets.
     89 
     90     }
     91 }
     92 
     93