Search this blog

Working with layouts in android

Most Android application user interfaces are defined using specially formatted XML
files called layouts. Layout resource files are included in the /res/layout directory.
You compile layout files into your application as you would any other resources.

Here is an example of a layout resource file:

<?xml version=”1.0” encoding=”utf-8”?>
<LinearLayout
xmlns:android=”http://schemas.android.com/apk/res/android”
android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”>
<TextView
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”@string/hello” />
</LinearLayout>
You might recognize this layout: It is the default layout, called main.xml, created
with any new Android application. This layout file describes the user interface of the only activity within the application. It contains a LinearLayout control that is used as a container for all other user interface controls—in this case, a single TextView control. The main.xml layout file also references another resource: the string resource called @string/hello, which is defined in the strings.xml resource file.
There are two ways to format layout resources. The simplest way is to use the layout resource editor in Eclipse to design and preview layout files. You can also edit the XML layout files directly.


READ MORE » Working with layouts in android

How to access image resources programatically android

Images resources are encapsulated in the class BitmapDrawable. To access a graphic resource file called /res/drawable/logo.png, you would use the getDrawable() method, as follows:

BitmapDrawable logoBitmap =
(BitmapDrawable)getResources().getDrawable(R.drawable.logo);

Most of the time, however, you don’t need to load a graphic directly. Instead, you
can use the resource identifier as an attribute on a control such as an ImageView
control. The following code, for example, sets and loads the logo.png graphic into
an ImageView control named LogoImageView, which must be defined within the
layout:

ImageView logoView = (ImageView)findViewById(R.id.LogoImageView);
logoView.setImageResource(R.drawable.logo);
READ MORE » How to access image resources programatically android

Image formats supported in anroid


Supported images format their description and required extension is given below

Supported Image  Format                Description                   Required Extension

Portable Network Graphics              Preferred format                    .png

Nine-Patch Stretchable Images         Preferred format                   .9.png

Joint Photographic Experts Group    Acceptable format                   .jpg

Graphics Interchange Format               Discouraged but                   .gif
READ MORE » Image formats supported in anroid

How to work with Dimensions in android

To specify the size of a user interface control such as a Button or TextView control,you need to specify different kinds of dimensions. You tag dimension resources with the <dimen> tag and store them in the resource file /res/values/dimens.xml. This XML resource file is not created by default and must be created manually.

Here is an example of a dimension resource file:
<?xml version=”1.0” encoding=”utf-8”?>
<resources>
<dimen name=”thumbDim”>100px</dimen>
</resources>
Each dimension resource value must end with a unit of measurement. Given below is the  list that shows the dimension units that Android supports.


Type of Measurement        Description                           Unit String
Pixels                                 Actual screen pixels                     px
Inches                                Physical measurement                 in
Millimeters                          Physical measurement                 mm
Points                                Common font measurement          pt
Density-independent pixels  Pixels relative to 160dpi                dp
Scale-independent pixels     Best for scalable font display         sp









READ MORE » How to work with Dimensions in android

Working with color in android

You can apply color resources to screen controls. You tag color resources with the
<color> tag and store them in the file /res/values/colors.xml. This XML
resource file is not created by default and must be created manually.



Here is an example of a color resource file:
<?xml version=”1.0” encoding=”utf-8”?>
<resources>
<color name=”background_color”>#006400</color>
<color name=”app_text_color”>#FFE4C4</color>
</resources>
The Android system supports 12-bit and 24-bit colors in RGB format. Given below is the list that how many color formats that the Android platform supports.


Color Formats Supported in Android

Format                  Description                         Example
#RGB                     12-bit color                       #00F (blue)
#ARGB                 12-bit color with alpha         #800F (blue, alpha 50%)
#RRGGBB             24-bit color                        #FF00FF (magenta)
#AARRGGBB        24-bit color with alpha         # 80FF00FF (magenta, alpha 50%)

The following code retrieves a color resource named app_text_color using the
getColor() method:
int textColor = getResources().getColor(R.color.app_text_color);










READ MORE » Working with color in android

How to log android application information

Android provides a useful logging utility class called android.util.Log. Logging
messages are categorized by severity (and verbosity), with errors being the most
severe. Below see some commonly used logging methods of the Log class.

Method               Purpose

Log.e()                 Logs errors
Log.w()                Logs warnings
Log.i()                  Logs informational messages
Log.d()                 Logs debug messages
Log.v()                 Logs verbose messages


The first parameter of each Log method is a string called a tag. One common
Android programming practice is to define a global static string to represent the
overall application or the specific activity within the application such that log filters
can be created to limit the log output to specific data.
For example, you could define a string called TAG, as follows:
private static final String TAG = “MyApp”;
Now anytime you use a Log method, you supply this tag. An informational logging
message might look like this:
Log.i(TAG, “In onCreate() callback method”);
READ MORE » How to log android application information

How to use dialogue methods of activity class

Methods and their purpose

(1)Activity.showDialog()      
Shows a dialog, creating it if necessary.
 
(2)Activity.onCreateDialog()
Is a callback when a dialog is being created for the first time and added to the activity dialog pool.
 
(3)Activity.onPrepareDialog()
Is a callback for updating a dialog on-the-fly.Dialogs are created once and can be used many times by an activity. This callback enables the dialog to be updated just before it is shown for each showDialog() call.
 
(4)Activity.dismissDialog()        
Dismisses a dialog and returns to the activity. The dialog is still available to be used again by calling showDialog() again.
 
(5)Activity.removeDialog()  
Removes the dialog completely from the activity dialog pool.

Activity classes can include more than one dialog, and each dialog can be created
and then used multiple times.

You can also create an entirely custom dialog by designing an XML layout file and
using the Dialog.setContentView() method. To retrieve controls from the dialog
layout, you simply use the Dialog.findViewById() method.
READ MORE » How to use dialogue methods of activity class

How to provide input to android emulator

As a developer, you can adopt any one of the below way to provide input to the emulator:

. Use your computer mouse to click, scroll, and drag items (for example, side
volume controls) onscreen as well as on the emulator skin.
. Use your computer keyboard to input text into controls.
. Use your mouse to simulate individual finger presses on the soft keyboard or
physical emulator keyboard.
. Use a number of emulator keyboard commands to control specific emulator
states.
READ MORE » How to provide input to android emulator

How to use Intents to launch other applications

Initially, an application may only be launching activity classes defined within its own package. However, with the appropriate permissions, applications may also launch external activity classes in other applications.
There are well-defined intent actions for many common user tasks. For example, you can create intent actions to initiate applications such as the following:

. Launching the built-in web browser and supplying a URL address
. Launching the web browser and supplying a search string
. Launching the built-in Dialer application and supplying a phone number
. Launching the built-in Maps application and supplying a location
. Launching Google Street View and supplying a location
. Launching the built-in Camera application in still or video mode
. Launching a ringtone picker
. Recording a ሶዑንድ

Here is an example of how to create a simple intent with a predefined action(ACTION_VIEW) to launch the web browser with a specific URL:
Uri address = Uri.parse(“http://www.google.com”);
Intent surf = new Intent(Intent.ACTION_VIEW, address);
startActivity(surf);
This example shows an intent that has been created with an action and some data.The action, in this case, is to view something. The data is a uniform resource identifier (URI), which identifies the location of the resource to view.
For this example, the browser’s activity then starts and comes into foreground, causing the original calling activity to pause in the background. When the user finishes with the browser and clicks the Back button, the original activity resumes.
Applications may also create their own intent types and allow other applications to call them, allowing for tightly integrated application suites.

READ MORE » How to use Intents to launch other applications

How to pass Information with Intents

We can use Intents to pass data between activities.We can use an intent in this way by including additional data, called extras, within the intent.To package extra pieces of data along with an intent, you use the putExtra() method with the appropriate type of object you want to include. The Android programming
convention forintent extras is to name each one with the package prefix( for example, com.android. johnson.NameOfExtra).
For example, the following intent includes an extra piece of information, the current game level, which is an integer:

Intent intent = new Intent(getApplicationContext(), HelpActivity.class);
intent.putExtra(“com.android.johnson.LEVEL”, 23);
startActivity(intent);
When the HelpActivity class launches, the getIntent() method can be used to retrieve the intent. Then the extra information can be extracted using the appropriate methods. Here’s an example:

Intent callingIntent = getIntent();
int helpLevel = callingIntent.getIntExtra(“com.android.johnson.LEVEL”, 1);
This little piece of information could be used to give special Help hints, based on the level.For the parent activity that launched a subactivity using the startActivityForResult() method, the result will be passed in as a parameter to the onActivityResult() method with an Intent parameter. The intent data can then be extracted and used by the parent activity.
READ MORE » How to pass Information with Intents