Friday, September 30, 2011

Create Alert Dialog in Android

To create Alert Dialog just call the following code where you want to like on button click on back button press or where ever you wish


Like this is an example where I asked user to exit the application on back button pressed :

@Override
    public void onBackPressed() {

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("Are you sure you want to EXIT?")
            .setCancelable(false)
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) 
                  {
                     moveTaskToBack(true) ;
                     System.exit(0);
                }
            })
            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });
            AlertDialog alert = builder.create();
            alert.show();
     }


Wednesday, September 28, 2011

Vertical and Horizontal Scroll on a Layout in Android

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/cybersportsbg">

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scrollbars="vertical">

        <HorizontalScrollView
            android:id="@+id/horizontalScrollView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <LinearLayout
                android:id="@+id/linearLayout1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal">
.....
  ----Your Layout------
........


            </LinearLayout>
            </HorizontalScrollView>
            </ScrollView>


</LinearLayout>

Launch CALL Activity on TextView or button Click in Android

First set the on click listener for your Button :

               btnMail.setOnClickListener(new View.OnClickListener() {
                       @Override
                       public void onClick(View arg0)
                                  {
                                     // TODO Auto-generated method stub
                                      call(txtPhone.getText().toString());
                                  }
                           });


Then Call this function !!!!

               private void call(String phone) {
                       try {
                              Intent callIntent = new Intent(Intent.ACTION_CALL);
                               callIntent.setData(Uri.parse("tel:"+phone));
                              startActivity(callIntent);
                             } catch (ActivityNotFoundException e) 

                       }
                   

And  Bingo thats IT!!!

Tuesday, September 27, 2011

Launch Mail Activity on TextView or button Click in Android

First set the on click listener for your Button :

               btnMail.setOnClickListener(new View.OnClickListener() {
                       @Override
                       public void onClick(View arg0)
                                  {
                                     // TODO Auto-generated method stub
                                       mail(txtEmail.getText().toString());
                                  }
                           });


Then Call this function !!!!

             private void mail(String email)
                       {
                             Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
                             emailIntent.setType("text/plain");
                             emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,email);
                             startActivity(emailIntent);
                       }

And  Bingo thats IT!!!