Wednesday, 22 February 2012

CheckBox Demo

package com.lesson1;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.Toast;
public class CheckBoxDemoActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final CheckBox check_box1 = (CheckBox) findViewById(R.id.cb1);
        final CheckBox check_box2 = (CheckBox) findViewById(R.id.cb2);
        check_box1.setOnClickListener(new View.OnClickListener() {
  
   @Override
   public void onClick(View v) {
    if(check_box1.isChecked()){
           System.out.println("Male is chosen");
           Toast.makeText(CheckBoxDemoActivity.this, "Come guys! I am a mail",Toast.LENGTH_LONG).show();
          }
          else {
           System.out.println("Female is chosen");
              Toast.makeText(CheckBoxDemoActivity.this, "Alas, I am a woman",Toast.LENGTH_LONG).show();
                 
           
           }
           
   }
         
 
       
       
    });
  
   
}
}

and the layout.xml

<?
<
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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<CheckBox android:id="@+id/cb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male" />
<CheckBox android:id="@+id/cb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female" />
</LinearLayout>

Learn Dialog

package com.project1;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;
public class AlertdemoActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        AlertDialog.Builder ad = new AlertDialog.Builder(this);
        ad.setMessage("Are you sure you want to proceeed")
        .setCancelable(false)
        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
  
   @Override
   public void onClick(DialogInterface dialog, int which) {
    Toast.makeText(AlertdemoActivity.this, "Yes, we can proceed", Toast.LENGTH_SHORT).show();
   
   }
  })
        .setNegativeButton("No", new DialogInterface.OnClickListener() {
  
   @Override
   public void onClick(DialogInterface dialog, int which) {
    Toast.makeText(AlertdemoActivity.this,"Thanks and we are exiting", Toast.LENGTH_SHORT).show();
   
   }
  });
         AlertDialog alert = ad.create();
         ad.show();
       
       
        }   
       
       
       
       
    }