Search This Blog

Wednesday, 6 June 2012

Quick Layout Tutorial for Android


 Layout Basics


A user interface template drawn on the screen. This templates can be managed using XML files in android and stored  in res/layout directory for the application. A group of controls defined and organized in the user interface

An Android layout is a class that handles the screen arrangements for this child elements.  Anything that is a View (or inherits from View) can be a child of a layout.

You can nest the layout and you can create the custom layouts by inherit from ViewGroup.
  following is the list of standard layouts

AbsoluteLayout
FrameLayout

LinearLayout

RelativeLayout

TableLayout


This post is explaining the AbsoluteLayout

AbsoluteLayout places each control at an absolute position. The exact x and y positions need specify for each control.

The Absolute Layout is difficult to maintain for most of the UIs as adding a new control is not flexible in the absolute layouts.

<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
     android:layout_x="10px"
     android:layout_y="110px"
     android:text="Layout"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" />
    <TextView
     android:layout_x="150px"
     android:layout_y="110px"
     android:text="Absolute layout"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" />
  </AbsoluteLayout>
 
Each control specify a x and y positions by android:layout_x and android:layout_y.
Android defines the top left of the screen as (0,0) and x coordinate move from 
left to right and y move from top to bottom. 
 
See the screen shot here for above example.
 
Absolute layout
 
 
 

How to implement fontsize for multiple density screen in android

Android runs on a variety of devices that offer different screen sizes and densities.

To handle fonts on the multiple screen and densities on android devices you can use the following approach

//get the density
final float densityMultiplier = getResources().getDisplayMetrics().density;

Now you can multiple the densityMultipler with your font size

float fontsize = 10.0f * densityMultiplier;


How to finish the parent activity from child activity

finishing a parent activity  from child activity can be implement by starting the child activity using the startActivityForResult and implement the onActivityResult to process the result return by the child activity.

For Example if you have two activities ParentActivity and ChildActivity

Implement the following code in Parent Activity

/*start the child activity for a result*/

//bundle your stuff to pass data from parent activity to child activity

Bundle bundle = new Bundle();
bundle.putInt("category", 1);
Intent done = new Intent(ParentActivity.this, ChildActivity.class);
done.putExtras(bundle);
//start the activity for result
startActivityForResult(done,1);

Now implement the onActivityResult in parent activity

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
     super.onActivityResult(requestCode, resultCode, data);
     //matches the result code passed from ChildActivity
     if(resultCode == 0)
     {
                       //kill self
         ParentActivity.this.finish();
     }
}

Implement the following code in ChildActivity

@Override
    public void onStop()
    {
        super.onStop();
        this.setResult(0);
    }


@Override
    public void onDestroy()
    {
        super.onDestroy();
        this.setResult(0);
    }

Enjoy Creative Android Apps

Sunday, 3 June 2012

Show a ProgressBar in Android

This post is explaining the Progress Bar creation in android application

You can create a progress bar using the ProgressDialog

ProgressDialog dialog = ProgressDialog.show(MyActivity.this, "Title Here",
                "Progress Message Here", true);

This will show the following progress bar.



to change the progress style use the following method

dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);


to cancel the dialog you can use the dialog.dismiss() or dialog.cancel() methods.


Show an Alert Message when user press the back button

This post is explaining how to show a alert message box when the user clicks the back button of the phone.

@Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_BACK)
            {
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setMessage("Application TiTle");
                builder.setMessage("Back Button Pressed");
                AlertDialog alert = builder.create();
                alert.show();
            }
     
            return true;

        }

Add a onKeyDown Method to the activity class and handle the keycode (KEYCODE_BACK).

This will receive when ever the activity is in front and user pressed a back button of phone.

You can create a simple alert message using the AlertDialog interface.

Alert Message Box in Android


Creating  Alert Message Box in Android needs a bit of coding. Here is a sample code for  simple alert dialog using Dialog Builder.

Alert dialog extends the dialog and provides a easy method to create a message box in android.

 
 AlertDialog.Builder builder = new AlertDialog.Builder(this);  
         builder.setMessage("Title Here");  
         builder.setMessage("Message here")  
             .setCancelable(false)  
             .setPositiveButton("Yes", new DialogInterface.OnClickListener() {  
               public void onClick(DialogInterface dialog, int id) {  
               // TODO : Add Action code for yes button
               }  
             })  
             .setNegativeButton("No", new DialogInterface.OnClickListener() {  
               public void onClick(DialogInterface dialog, int id) {  
                 dialog.cancel();  
               }  
             });  
         AlertDialog alert = builder.create();  
         alert.show();