Showing posts with label android tutorials. Show all posts
Showing posts with label android tutorials. Show all posts

Thursday, 1 September 2011

Android LinearLayout Example

LinearLayout organizes elements along a single line. You specify whether that line is verticle or horizontal using android:orientation. Here is a sample Layout XML using LinearLayout.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
     <Button
        android:id="@+id/backbutton"
        android:text="Backbutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:text="First Name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <EditText
        android:width="100px"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:text="Last Name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <EditText
        android:width="100px"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

Here is a screenshot of the result of the above XML




Here is a screenshot of the same XML except that the android:orientation has been changed to horizontal.

 

You might note that the EditText field at the end of the line has had its width reduced in order to fit. Android will try to make adjustments when necessary to fit items on screen. The last page of this tutorial will cover one method to help deal with this.

I mentioned on the first page that Layouts can be nested. Linear Layout is frequently nested, with horizontal and vertical layouts mixed. Here is an example of this.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
    android:layout_height="fill_parent">
     <Button
        android:id="@+id/backbutton"
        android:text="Back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
            <TextView
                android:text="First Name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <EditText
                android:width="100px"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"> 
            <TextView
                android:text="Last Name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <EditText
                android:width="100px"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
    </LinearLayout>
</LinearLayout>

As you can see we have a Vertical LinearLayout whose children are a button, and two horizontal LinearLayouts. Each horizontal LinearLayout has two child controls. You should note that in the child LinearLayout elements I used android:layout_height=”wrap_content” instead of fill_parent. If I had used fill_parent the first name TextView and EditView would have taken all of the available space on the screen, and the Last Name would not have been displayed. Here is what this XML does display.



Nested Layouts do not have to be of one type. I could, for example, have a LinearLayout as one of the children in a FrameLayout.

Saturday, 18 June 2011

Android Start Activity for Result

Starting Activities and Getting Results

The method startActivity(Intent) is used to start a new activity. But sometimes we want to get back results from an activity when it ends. For example, we may start an activity that lets the user input his/her name in an EditText, when it ends, it returns the name. To do this, you call the startActivityForResult(Intent, int) version with a second integer parameter identifying the call. The result will come back through ouronActivityResult(int, int, Intent) method.
When an activity ends, it can call setResult(int) to return data back to its parent. It must always supply a result code, which can be the standard results RESULT_CANCELED, RESULT_OK, or any custom values starting at RESULT_FIRST_USER. In addition, it can optionally return back an Intent containing any additional data it wants. All of this information appears back on the parent’s Activity.onActivityResult(), along with the integer identifier it originally supplied.
Note: If a child activity fails for any reason (such as crashing), the parent activity will receive a result with the code RESULT_CANCELED.


Create Project: Activities and Getting Results Example

1. Create a project with project name: ActivityResult
2. Fill Application Name: ActivityResult
3. Fill the Package Name as: androidsurya
4. I have used SDK version Android 4.0.3 and Eclipse

5. Add below 2 java files (MainActivity.Java and SecondActivity.Java) in you project’s /src folder.

  • MainActivity.Java
This activity is the base activity from which we will start another activity for getting result.
package com.androidsurya.activityresult;

import com.androidsurya.activityresult.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity
{
TextView textViewMessage;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// get The reference of The textView
textViewMessage=(TextView)findViewById(R.id.textViewMessage);
}
// Method to handle the Click Event on GetMessage Button
public void getMessage(View V)
{
// Create The Intent and Start The Activity to get The message
Intent intentGetMessage=new Intent(this,SecondActivity.class);
startActivityForResult(intentGetMessage, 2);// Activity is started with requestCode 2
}
// Call Back method to get the Message form other Activity
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);

// check if the request code is same as what is passed here it is 2
if(requestCode==2)
{
if(null!=data)
{
// fetch the message String
String message=data.getStringExtra("MESSAGE");
// Set the message string in textView
textViewMessage.setText("Message from second Activity: " + message);
}
}
}
}
  •  SecondActivity.Java
This activity will show user an EditText. Once user put some data and press Submit button; then the activity will close and send the data to the parent Activity (MainActivity Class) using API setResult(2,intentMessage).
package com.androidsurya.activityresult;

import com.androidsurya.activityresult.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class SecondActivity extends Activity
{
EditText editTextMessage;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.layout2);
// Get the reference of Edit Text
editTextMessage=(EditText)findViewById(R.id.editTextMessage);
}
public void submitMessage(View V)
{
// get the Entered message
String message=editTextMessage.getText().toString();
Intent intentMessage=new Intent();

// put the message in Intent
intentMessage.putExtra("MESSAGE",message);
// Set The Result in Intent
setResult(2,intentMessage);
// finish The activity
finish();
}
}

6. Add below 2 xml layout files (activity_main.xml and layout2.xml) in your project’s res/layout folder.
  • activity_main.xml
This is used in MainActivity.Java file for showing GUI to start a new Activity for result.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical" >

<TextView
android:id="@+id/textViewMessage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:textColor="#FF0000"
android:textSize="20dp"
android:text="Message" />

<Button
android:id="@+id/button1"
android:layout_marginTop="20dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="Get Message"
android:onClick="getMessage" />

</LinearLayout>

  • layout2.xml
This is used in SecondActivity.Java file for showing GUI for user input.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical" >

<EditText
android:id="@+id/editTextMessage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:textColor="#FF0000"
android:textSize="20sp"
android:hint="Enter The Message" />

<Button
android:layout_marginTop="20dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="Submit Message"
android:onClick="submitMessage" />

</LinearLayout>

  1. Add the below Manifest file (AndroidManifest.xml) in your project’s root folder.
  •        AndroidManifest.xml 
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidsurya.activityresult"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.androidsurya.activityresult.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity
android:name="com.androidsurya.activityresult.SecondActivity"/>
</application>

</manifest>

8. Build and Run the application, then observe the result.


Wednesday, 15 June 2011

Android Operation System.

Android is an operating system based on Linux with a Java programming interface. It provides tools, e.g. a compiler, debugger and a device emulator as well as its own Java Virtual machine (Dalvik Virtual Machine - DVM). Android is created by the Open Handset Alliance which is lead by Google. Android uses a special virtual machine, e.g. the Dalvik Virtual Machine. Dalvik uses special bytecode. Therefore you cannot run standard Java bytecode on Android. Android provides a tool "dx" which allows to convert Java Class files into "dex" (Dalvik Executable) files. Android applications are packed into an .apk (Android Package) file by the program "aapt" (Android Asset Packaging Tool) To simplify development Google provides the Android Development Tools (ADT) for Eclipse . The ADT performs automatically the conversion from class to dex files and creates the apk during deployment.
Android supports 2-D and 3-D graphics using the OpenGL libraries and supports data storage in a SQLitedatabase.
Every Android applications runs in its own process and under its own userid which is generated automatically by the Android system during deployment. Therefore the application is isolated from other running applications and a misbehaving application cannot easily harm other Android applications.

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...