Android Open Source - introToDroid4ed Resource Roundup Activity






From Project

Back to project page introToDroid4ed.

License

The source code is released under:

GNU General Public License

If you think the Android project introToDroid4ed listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.introtoandroid.resourceroundup;
//from ww w.  j  a  v a  2s . c o  m
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.xmlpull.v1.XmlPullParserException;


import android.app.Activity;
import android.content.res.XmlResourceParser;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.NinePatchDrawable;
import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;


public class ResourceRoundupActivity extends Activity {
    /** Called when the activity is first created. */
  private static final String DEBUG_TAG= "ResourceRoundup Log";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_resource_roundup);
        
        
        // SOME SIMPLE RESOURCE HANDLING (a string, a bold styled string, a color, a dimension and a simple drawable
        String myResourceString = getResources().getString(R.string.hello);  
        CharSequence myResourceStringRich = getResources().getText(R.string.boldhello);
        int myResourceColor = getResources().getColor(R.color.prettyTextColor);
        float myResourceDimension = getResources().getDimension(R.dimen.textPointSize);
        ColorDrawable myResourceDrawable = (ColorDrawable)getResources().getDrawable(R.drawable.redDrawable);
        
        Log.d(DEBUG_TAG, "myResourceString value is: " + myResourceString);
        Log.d(DEBUG_TAG, "myResourceStringRich value is: " + myResourceStringRich.toString());
        Log.d(DEBUG_TAG, "myResourceColor value is: " + myResourceColor);
        Log.d(DEBUG_TAG, "myResourceDimension value is: " + myResourceDimension);
        Log.d(DEBUG_TAG, "myResourceDrawable value is: " + myResourceDrawable.toString()); // Not very exciting, I know.

        // SOME STRING-ARRAY HANDLING
        String[] aFlavors = getResources().getStringArray(R.array.flavors);
        for(int i=0; i<aFlavors.length; i++)
        {
           Log.d(DEBUG_TAG, "aFlavors["+ i + "] value is: "+ aFlavors[i]);
        } 
        
        // SOME FORMAT STRING HANDLING
        // You can use htmlEncode to make sure your string is escaped properly. Of course, "Won" is fine as is... But if it had been "You've won!", you'd be glad you called this.
        String escapedWin = TextUtils.htmlEncode("Won");
        Log.d(DEBUG_TAG, "escapedWin value is: " + escapedWin);
        
        // First, the simple format string
        String mySimpleWinString = getResources().getString(R.string.simpleformatString);
        Log.d(DEBUG_TAG, "mySimpleWinString value is: " + mySimpleWinString);
        String resultsText1 = String.format(mySimpleWinString, 5, 5, escapedWin);
        Log.d(DEBUG_TAG, "resultsText1 (simple) value is: " + resultsText1);
        
        // Second, the styled format string
        String resultsTextFormat = getResources().getString(R.string.formatStringWithTwoNumbersAndAString);
        Log.d(DEBUG_TAG, "resultsTextFormat value is: " + resultsTextFormat);
        String resultsText = String.format(resultsTextFormat, 5, 5, escapedWin);
        Log.d(DEBUG_TAG, "resultsText value is: " + resultsText);
        CharSequence styledResults = Html.fromHtml(resultsText);
        Log.d(DEBUG_TAG, "styledResults value is: " + styledResults);
        
        // SOME WIDGET/VIEW/IMAGE HANDLING
        // Grab the ImageView and display the flag graphic on the main layout ImageView
        ImageView flagImageView = (ImageView)findViewById(R.id.ImageView01);
        flagImageView.setImageResource(R.drawable.flag);
        
        // Grab the Bitmap flag graphic and check out some stuff about it
        BitmapDrawable bitmapFlag = (BitmapDrawable)getResources().getDrawable(R.drawable.flag); 
        Log.d(DEBUG_TAG, "bitmapFlag is HEIGHT: " + bitmapFlag.getIntrinsicHeight() + " and WIDTH: " + bitmapFlag.getIntrinsicWidth());
        
        // Grab a Nine-patch graphic and check out some stuff about it
        NinePatchDrawable stretchy = (NinePatchDrawable)getResources().getDrawable(R.drawable.pyramid);
        Log.d(DEBUG_TAG, "stretchy is HEIGHT: " + stretchy.getIntrinsicHeight() + " and WIDTH: " + stretchy.getIntrinsicWidth());
        
        // SOME ANIMATION HANDLING
        // Make the flag (png) spin using our spin.xml animation resource
        Animation an =  AnimationUtils.loadAnimation(this, R.anim.spin);
        flagImageView.startAnimation(an);
 
        // Make the red oval drawable shape resource pulse using our pulse.xml animation resource 
        ImageView redOval = (ImageView)findViewById(R.id.ImageView02);
        Animation an2 =  AnimationUtils.loadAnimation(this, R.anim.pulse);
        redOval.startAnimation(an2);
 
        // SOME XML HANDLING
        XmlResourceParser myPets = getResources().getXml(R.xml.my_pets);
        
        try {
      inspectPetsXml(myPets);
    } catch (Exception e) {
      Log.e(DEBUG_TAG, "InspectPetsXml broke.", e);
    }
    


    // SOME RAW FILE HANDLING
    InputStream iFile = getResources().openRawResource(R.raw.limerick);
        try {
          String strFile = inputStreamToString(iFile);
          Log.d(DEBUG_TAG, "Here's the limerick from the Input Stream:\n"+ strFile); 
          
    } catch (Exception e) {
      Log.e(DEBUG_TAG, "InputStreamToString broke.", e);
    }  
        
    }
    
    // This method is called when the user presses the Menu button
    @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // SOME MENU RESOURCE HANDLING
      getMenuInflater().inflate(R.menu.speed, menu);
      return true;
  }

    // Churns through a specific XML file, and prints all my pet elements and their attributes
  public static void inspectPetsXml(XmlResourceParser myPets) throws XmlPullParserException, IOException
    {
      
        // Let's inspect my pets a little 
      int eventType = -1;
        while (eventType != XmlResourceParser.END_DOCUMENT) {
         if(eventType == XmlResourceParser.START_DOCUMENT) {
           Log.d(DEBUG_TAG, "Document Start");
         } else if(eventType == XmlResourceParser.START_TAG) {
          
          // Get the name of the tag (eg pets or pet) 
          String strName = myPets.getName();

          if(strName.equals("pet"))
          {
               Log.d(DEBUG_TAG, "Found a PET");             
             Log.d(DEBUG_TAG, "Name: "+myPets.getAttributeValue(null, "name"));
             Log.d(DEBUG_TAG, "Species: "+myPets.getAttributeValue(null, "type"));      
          }
         } 
         
         eventType = myPets.next();
        }
         
        Log.d(DEBUG_TAG, "Document End");
      
      
    }
    
    
  // Converts an input stream to a string so we can print it using logcat debug logging.
    public String inputStreamToString(InputStream is) throws IOException
    {
      StringBuffer sBuffer = new StringBuffer();
      BufferedReader dataIO = new BufferedReader( new InputStreamReader(is));
      String strLine = null;
      
      while((strLine=dataIO.readLine()) != null){
          sBuffer.append(strLine+"\n");
      }

      dataIO.close();
      is.close();

      return sBuffer.toString();
      
    }

    
}




Java Source Code List

com.introtoandroid.advancedlayouts.AdaptersActivity.java
com.introtoandroid.advancedlayouts.AdvancedLayoutsActivity.java
com.introtoandroid.advancedlayouts.BasicLayoutActivity.java
com.introtoandroid.advancedlayouts.ContactAdapterActivity.java
com.introtoandroid.advancedlayouts.DialogActivity.java
com.introtoandroid.advancedlayouts.GridAdapterSampleActivity.java
com.introtoandroid.advancedlayouts.GridLayoutActivity.java
com.introtoandroid.advancedlayouts.GridListMenuActivity.java
com.introtoandroid.advancedlayouts.ListAdapterSampleActivity.java
com.introtoandroid.advancedlayouts.MenuActivity.java
com.introtoandroid.advancedlayouts.MyListActivity.java
com.introtoandroid.advancedlayouts.StyleSamplesActivity.java
com.introtoandroid.filesoc.FileStreamOfConsciousnessActivity.java
com.introtoandroid.filesoc.ViewLogActivity.java
com.introtoandroid.myfirstandroidapp.MyFirstAndroidAppActivity.java
com.introtoandroid.navigation.FirstChildActivity.java
com.introtoandroid.navigation.ParentActivity.java
com.introtoandroid.navigation.SecondChildActivity.java
com.introtoandroid.navigation.ThirdChildActivity.java
com.introtoandroid.parisview.ParisViewActivity.java
com.introtoandroid.passwordmatcher.PasswordMatcherActivity.java
com.introtoandroid.resourceroundup.ResourceRoundupActivity.java
com.introtoandroid.samelayout.MenuActivity.java
com.introtoandroid.samelayout.ProgrammaticLayoutActivity.java
com.introtoandroid.samelayout.ResourceLayoutActivity.java
com.introtoandroid.samelayout.SameLayoutActivity.java
com.introtoandroid.simpleactionbar.SimpleActionBarActivity.java
com.introtoandroid.simplealtresources.SimpleAltResourcesActivity.java
com.introtoandroid.simplecontacts.SimpleContactsActivity.java
com.introtoandroid.simplecontentprovider.MenuActivity.java
com.introtoandroid.simplecontentprovider.SimpleBookmarks.java
com.introtoandroid.simplecontentprovider.SimpleCallLog.java
com.introtoandroid.simplecontentprovider.SimpleContentProviderMenuActivity.java
com.introtoandroid.simplecontentprovider.SimpleMediaStore.java
com.introtoandroid.simplefiles.SimpleFilesActivity.java
com.introtoandroid.simplefragdialogs.SimpleFragDialogActivity.java
com.introtoandroid.simplefragments.FieldNoteListFragment.java
com.introtoandroid.simplefragments.FieldNoteViewActivity.java
com.introtoandroid.simplefragments.FieldNoteWebViewFragment.java
com.introtoandroid.simplefragments.SimpleFragmentsActivity.java
com.introtoandroid.simplelayout.FrameLayoutActivity.java
com.introtoandroid.simplelayout.GridLayoutActivity.java
com.introtoandroid.simplelayout.LinearLayoutActivity.java
com.introtoandroid.simplelayout.MenuActivity.java
com.introtoandroid.simplelayout.MultipleLayoutActivity.java
com.introtoandroid.simplelayout.RelativeLayoutActivity.java
com.introtoandroid.simplelayout.SimpleLayoutActivity.java
com.introtoandroid.simplelayout.TableLayoutActivity.java
com.introtoandroid.simplemultimedia.AudioActivity.java
com.introtoandroid.simplemultimedia.MenuActivity.java
com.introtoandroid.simplemultimedia.SimpleMultimediaActivity.java
com.introtoandroid.simplemultimedia.StillImageActivity.java
com.introtoandroid.simplemultimedia.VideoPlayActivity.java
com.introtoandroid.simplepreferences.MoreSimplePreferencesActivity.java
com.introtoandroid.simplepreferences.SimplePreferencesActivity.java
com.introtoandroid.simplepreferences.SuperSimplePreferencesActivity.java
com.introtoandroid.simpleresourceview.SimpleResourceViewActivity.java
com.introtoandroid.simplescrolling.BothScrollActivity.java
com.introtoandroid.simplescrolling.HorizontalScrollActivity.java
com.introtoandroid.simplescrolling.MenuActivity.java
com.introtoandroid.simplescrolling.NoScrollActivity.java
com.introtoandroid.simplescrolling.SimpleScrollingActivity.java
com.introtoandroid.simplescrolling.VerticalScrollActivity.java
com.introtoandroid.simpleuserprefs.SimpleUserPrefsActivity.java
com.introtoandroid.supportfragdialog.MyAlertDialogFragment.java
com.introtoandroid.supportfragdialog.SupportFragDialogActivity.java
com.introtoandroid.userprefsheaders.UserPrefsActivity.java
com.introtoandroid.viewsamples.ButtonsActivity.java
com.introtoandroid.viewsamples.ContainersActivity.java
com.introtoandroid.viewsamples.EventsActivity.java
com.introtoandroid.viewsamples.FormsActivity.java
com.introtoandroid.viewsamples.IndicatorsActivity.java
com.introtoandroid.viewsamples.MenuActivity.java
com.introtoandroid.viewsamples.PickersActivity.java
com.introtoandroid.viewsamples.TextDisplayActivity.java
com.introtoandroid.viewsamples.TextInputActivity.java
com.introtoandroid.viewsamples.ViewSampleActivity.java