Saturday, April 7, 2012

Load Android Drawable from XML

  • Create an XML file in res/xml with the Drawable. Assume the below to be my_drawable.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape 
       xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <solid
            android:color="#FF555599" />
        <stroke
            android:width="2dp"
            android:color="#FF000000"
            android:dashWidth="2dp"
            android:dashGap="1dp" />
    </shape>

  • Load the Drawable in code

    Drawable myDrawable;
    Resources res = getResources();
    try {
       myDrawable = Drawable.createFromXml(res, res.getXml(R.xml.my_drawable));
    catch (Exception ex) {
       Log.e("Error", "Exception loading drawable");
    }

  • Use the Drawable.

    @Override
    public void onDraw(Canvas canvas) {
       if (null != myDrawable) {
          myDrawable.setBounds(0, 0, getWidth(), getHeight());
          myDrawable.draw(canvas);
       }
    }

Sunday, April 1, 2012

Download Android source without "repo" (for browsing purposes)


  • Have Git working. On Windows, it could be cygwin, msysgit or any other.
  • Clone the manifest: git clone https://android.googlesource.com/platform/manifest
  • Open manifest/default.xml
  • You can checkout any Android project listed in default.xml
  • For example, default.xml list the base project:  <project path="frameworks/base" name="platform/frameworks/base" />. To checkout this project, use:  git clone https://android.googlesource.com/platform/frameworks/base