You can use image files of the following formats:
- PNG
- JPEG
- GIF
- If you use two files with the same base name you will receive an error. You cannot use two files like Hello.jpeg and Hello.png.
- If you create any subdirectory under res/drawable directory any image files in it will be ignored.
We will place an image called android.jpeg in res/drawable directory
We can use it from xml layout as this
<imageview android_layout_height="wrap_content" android_layout_width="wrap_content" android_src="@drawable/android" /%gt;
Or from code as this
ImageView img=(ImageView)findViewById(R.id.img);or like this to get the image as a drawable object
img.setImageResource(R.drawable.android);
ImageView img2=(ImageView)findViewById(R.id.img2);Color drawable Resources:
Drawable drawable=this.getResources().getDrawable(R.drawable.android);
img2.setBackgroundDrawable(drawable);
You can define XML files that contain definitions for color drawable resources which are color rectangles that can be used as backgrounds
You can define the color drawable resources in values/strings.xml file or by creating a custom xml file to hold these resources
You define color drawable resources like this:
<drawable name="redBox">#f00</drawable>You can use it from xml layout like this
Or from code like this:
TextView txt=(TextView)findViewById(R.id.txt);or like this:
txt.setBackgroundResource(R.drawable.redBox);
ColorDrawable drawable2=(ColorDrawable)this.getResources().getDrawable(R.drawable.redBox);or this
txt.setBackgroundDrawable(drawable2);
txt.setBackgroundResource(R.drawable.redBox);notice that if the textview does not have text the background color will not appear.
Download a demo application from here
0 comments:
Post a Comment