Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Android IMP data

Color Resources:
You can define XML files that contain definitions for colors that can be used in your application.

Colors in Android are hexadecimal RGB values, also optionally specifying an alpha channel (transparency).

You have your choice of single-character hex values or double-character hex values, leaving

you with four styles:

 #RGB (Red: #F00).


 #ARGB (Red with Alpha 5: #5F00).
 #RRGGBB (Red : #FF0000).
 #AARRGGBB (Red with Alpha 50: 50FF0000 #).
You define colors in the xml file as follows:

<color name="Red">#FF0000</color>

TextView txtColor=(TextView)findViewById(R.id.txtColor);

txtColor.setTextColor(this.getResources().getColor(R.color.Red));

<textview android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="This is a color


string from layout" android:textcolor="@color/Red"></textview>

Dimensions Resources:
You can define Dimension resources to use them with your widgets to define padding, width or height.

The dimension resources can be defined in the following units:

Pixels: (px).

Inches: (in).

Millimeters: (mm).

Points: (pt).

Density: (dp) density-independent pixels based on 160 dpi (dot per inch).

Scale: (sp) Scale-independent pixels (dimensions that allow for user sizing; helpful for use in fonts).
To define dimension resources in xml files you can write it like this:
<dimen name="PixelDim">10px</dimen>
<dimen name="PointsDim">10pt</dimen>
<dimen name="InchesDim">0.2in</dimen>
<dimen name="MilliDim">5mm</dimen>
<dimen name="DensityDim">20dp</dimen>
<dimen name="ScaleDim">20sp</dimen>

And you can use them from the xml layout definition like this:
<textview android:id="@+id/txtInches" android:layout_height="wrap_content"
android:layout_width="fill_parent" android:text="Font size is 0.2 Inches"
android:textsize="@dimen/InchesDim"></textview>

or from code like this:


TextView txtdp=(TextView)findViewById(R.id.txtdp);
txtdp.setTextSize(this.getResources().getDimension(R.dimen.DensityDim));

Array Resources:
Array resources allow you to define custom string arrays that hold values you can use in your application such as a
countries list or a list of names.
An Array resource is defined using string-array element while items are defined using item element.

<string-array name="countries">
<item>USA</item>
<item>UK</item>
<item>Canada</item>
</string-array>

and you can use them from code like this:


String [] Countries=this.getResources().getStringArray(R.array.countries);

Image Resources:
Android provides us with the ability to use image resources in our
applications. We can put image files in res/drawable directory and access them
from the xml layout or by the generated ID from R.java class file.

You can use image files of the following formats:


PNG
JPEG
GIF
Notice that there are two restrictions:
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.
Now let’s demonstrate how can we use image resources

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"/>
Or from code as this:

ImageView img=(ImageView)findViewById(R.id.img);
img.setImageResource(R.drawable.android);

You might also like