Monday, 26 September 2011

Android AbsoluteLayout Example

A layout defines the visual structure for a user interface, such as the UI for an activity or app widget. You can declare a layout in two ways:
  • Declare UI elements in XML. Android provides a straightforward XML vocabulary that corresponds to the View classes and subclasses, such as those for widgets and layouts.
  • Instantiate layout elements at runtime. Your application can create View and ViewGroup objects (and manipulate their properties) programmatically.
Write the XML:

Using Android's XML vocabulary, you can quickly design UI layouts and the screen elements they contain, in the same way you create web pages in HTML — with a series of nested elements.
Each layout file must contain exactly one root element, which must be a View or ViewGroup object. Once you've defined the root element, you can add additional layout objects or widgets as child elements to gradually build a View hierarchy that defines your layout.

Example:

XML layout that uses a vertical LinearLayout to hold a TextView and a Button:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:orientation="vertical" >
    <TextView android:id="@+id/text"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Hello, I am a TextView" />
    <Button android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello, I am a Button" />
</LinearLayout>


After you've declared your layout in XML, save the file with the .xml extension, in your Android project's res/layout/ directory, so it will properly compile. 

Load the XML Resource:

When you compile your application, each XML layout file is compiled into a View resource. You should load the layout resource from your application code, in your Activity.onCreate() callback implementation. Do so by calling setContentView(), passing it the reference to your layout resource in the form of: R.layout.layout_file_name

For example, if your XML layout is saved as main_layout.xml, you would load it for your Activity like so:
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_layout);
}
The onCreate() callback method in your Activity is called by the Android framework when your Activity is launched (see the discussion about lifecycles, in the Activities document).

This information from Android developers site(for more information):-http://developer.android.com/guide/topics/ui/declaring-layout.html#load

An Android layout is a class that handles arranging the way its children appear on the screen.  Anything that is a View (or inherits from View) can be a child of a layout. All of the layouts inherit from ViewGroup (which inherits from View) so you can nest layouts.  You could also create your own custom layout by making a class that inherits from ViewGroup.

The standard Layouts are:

AbsoluteLayout
FrameLayout
LinearLayout
RelativeLayout
TableLayout

Absolute Layout

AbsoluteLayout is based on the simple idea of placing each control at an absolute position.  You specify the exact x and y coordinates on the screen for each control.  This is not recommended for most UI development (in fact AbsoluteLayout is currently deprecated) since absolutely positioning every element on the screen makes an inflexible UI that is much more difficult to maintain.  Consider what happens if a control needs to be added to the UI. You would have to change the position of every single element that is shifted by the new control.

Example AbsoluteLayout :

<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Button
     android:id="@+id/backbutton"
     android:text="Back"
     android:layout_x="10px"
     android:layout_y="5px"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" />
    <TextView
     android:layout_x="10px"
     android:layout_y="110px"
     android:text="First Name"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" />
    <EditText
     android:layout_x="150px"
     android:layout_y="100px"
     android:width="100px"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" />
    <TextView
     android:layout_x="10px"
     android:layout_y="160px"
     android:text="Last Name"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" />
     <EditText
     android:layout_x="150px"
     android:layout_y="150px"
     android:width="100px"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" />
</AbsoluteLayout>

Output Screenshot:



Android Frame Layout Example

FrameLayout is designed to display a single item at a time. You can have multiple elements within a FrameLayout but each element will be positioned based on the top left of the screen. Elements that overlap will be displayed overlapping. I have created a simple XML layout using FrameLayout that shows how this works.

<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <ImageView
        android:src="@drawable/ic_launcher"
        android:scaleType="fitCenter"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"/>
    <TextView
        android:text="http://androidsurya.blogspot.in/"
        android:textSize="30sp"
        android:textColor="@android:color/holo_blue_bright"
        android:textStyle="bold"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:gravity="center"/>
</FrameLayout>



 





























Here is the result of this XML.

You can see I had both the ImageView and TextView fill the parent in both horizontal and vertical layout. Gravity specifies where the text appears within its container, so I set that to center. If I had not set a gravity then the text would have appeared at the top left of the screen.
FrameLayout can become more useful when elements are hidden and displayed programmatically. You can use the attribute android:visibility in the XML to hide specific elements. You can call setVisibility from the code to accomplish the same thing. The three available visibility values are visible, invisible (does not display, but still takes up space in the layout), and gone (does not display, and does not take space in the layout).






The connection to adb is down, and a severe error has occurred

Some times Android project may  shutdown abnormally, and caused the “adb.exe” to function unfortunately.
Then it shows message in your eclipse like this


To resolve use this below manually process Go to Task Manager in your system and end (adb.exe) process in your system.




Saturday, 17 September 2011

Android How to create folder in Sdcard Programmatically

Using below code we can create folder in Sdcard.(copy this below code in your activity)

String newFolder = "/mynewfolder";
String extSdcard = Environment.getExternalStorageDirectory().toString();
File file = new File(extSdcard + newFolder);
file.mkdir();

Note: Dont forget to add permission

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />











How to create folder in SD Card of Android Emulator, using adb (Eclipse)

If you want to create any folder in sdcard we have to use adb(Android Debug Bridge) in Eclipse

Steps : 

1. Choose Show View from Window->Other


















2. Show View Screen Appear under search File Explorer




















3.File Explorer is opened now follow below steps































Now you can see new folder which is created under sdcard of your Emulator.

Wednesday, 14 September 2011

How to Take Screen Shot in Android

Taking screen shot in a mobile is so easy. Just hold the home and power button together for a one second.then current screen as image in photo gallery.Applicable for any latest versions of Android device

Here you can see how to take screen shot from AVD(Android Virtual Device) 
What is AVD (Android Virtual Device) ?
The AVD Manager provides a graphical user interface in which you can create and manage Android Virtual Devices (AVDs), which are required by the Android Emulator.

Steps for taking Screen shot using AVD: 

Step 1: Run the android app in AVD.

Step 2: Go to Eclipse menu -> Window -> Show View  -> Devices



Now you will see below screen when you click on Device

Step3: click on camera icon to take screen shot from AVD



Now you will see below screen when you click on camera

Step 4: Using Save option you can save your screen shot in your local disk.








Android SQLite Database Viewer or Debuging with Stetho

Every Android Developer uses SQLite Database to store data into the Android Application data. But to view the data in SQLite have a lot of...