MY mENU


Monday, 21 January 2013

Creating Alert dialogue boxes in Android:

public class ButtoneventActivity extends Activity implements View.OnClickListener {
    /** Called when the activity is first created. */
                private Button b1;
                private Button b2;
                private Button b3;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        b1=(Button)findViewById(R.id.button1);
        b1.setText("Click");
        b1.setOnClickListener(this);
        b2=(Button)findViewById(R.id.button2);
        b2.setText("alert");
        b2.setOnClickListener(this);
        b3=(Button)findViewById(R.id.button3);
        b3.setText("Conform");
        b3.setOnClickListener(this);
    }
                @Override
                public void onClick(View view) {
                                // TODO Auto-generated method stub
                                if(view==b1){
                                                AlertDialog showAlert=new AlertDialog.Builder(this).create();
                                                 showAlert.setTitle("it's clicked");
                                                 showAlert.setMessage("are you sure you want to Burn?");
                                                 showAlert.setButton("Burn",new OnClickListener(){
                                                                @Override
                                              public void onClick(DialogInterface dialog, int which) {
                                                                   // TODO Auto-generated method stub
                                                                                dialog.cancel();
                                                                }                                                             
                                                  });
                                                 showAlert.show();                         
                                        }
                                else if(view==b2){
                                               AlertDialog showAlert=new AlertDialog.Builder(this).create();
                                                 showAlert.setTitle("Button clicked");
                                                 showAlert.setMessage("are you sure you want to save?");
                                                 showAlert.setButton("save",new OnClickListener() {
                                                                @Override
                                                 public void onClick(DialogInterface dialog, int which) {
                                                                    // TODO Auto-generated method stub
                                                                                 dialog.cancel();
                                                                }       
                                       });
                                                 showAlert.setButton2("Cancle",new OnClickListener(){
                                                                                @Override
                                            public void onClick(DialogInterface dialog, int which) {
                                                                  //TODO Auto-generated method stub
                                                                                                 dialog.cancel();
                                                                                                                                                                                                       
                                       }                                 
                             });
                                                 showAlert.show();
                                }
                                else if(view==b3){
                                                AlertDialog showAlert=new AlertDialog.Builder(this).create();
                                                 showAlert.setTitle("Button clicked");
                                                 showAlert.setMessage("are you sure you want to save?");
                                                 showAlert.setButton("Yes",new OnClickListener(){
                                                                @Override
                                                  public void onClick(DialogInterface dialog, int which) {
                                                                        // TODO Auto-generated method stub
                                                                                 dialog.cancel();
                                                                }               
                               });
                                                 showAlert.setButton2("NO",new OnClickListener(){
                                                                                @Override
                                                      public void onClick(DialogInterface dialog, int which) {
                                                                               // TODO Auto-generated method stub
                                                                                                 dialog.cancel();               
                                                                                }     
                                                           });
                                                 showAlert.setButton3("Cancle",new OnClickListener(){
                                                                                @Override
                                     public void onClick(DialogInterface dialog, int which) {
                                                                // TODO Auto-generated method stub
                                                                                                 dialog.cancel();
                                                                                }      
                                        });  
                                  showAlert.show();   
                                               }
                             }
    }

Passing Data from One Intent to Another Intent:

Here I used two Activity classes to pass data from one activity to another activity through Intents.

Main Activity.java :  which Intent send the data:

public class MainActivity extends Activity implements OnClickListener {
     EditText name, pwd;
                Button sub;
                @Override
                protected void onCreate(Bundle savedInstanceState) {
                                super.onCreate(savedInstanceState);
                                setContentView(R.layout.main);
                                name=(EditText)findViewById(R.id.NameText);
                                pwd=(EditText)findViewById(R.id.PassWordText);
                                sub=(Button)findViewById(R.id.button1);
                                sub.setOnClickListener(this);
                }
                @Override
                public void onClick(View v) {
                                String ename=name.getText().toString();
                                String epwd=pwd.getText().toString();
                                Intent intn=new Intent(MainActivity.this,SecondActivity.class);

// here putExtra(keyvalue,value); // here keyvalue is a key value to identify value from first intent in another intent. Value is value which you are passing to another intent.

                                intn.putExtra("Name",ename);
                                intn.putExtra( "PassW",epwd);
                                startActivity(intn);
                }       }

Second Activity.java: which receives that data and prints that data:

public class SecondActivity extends Activity implements OnClickListener{
                Button get,search;
                EditText nm,pw;
                String sname,spwd;
                @Override
                protected void onCreate(Bundle savedInstanceState) {
                                super.onCreate(savedInstanceState);
                                setContentView(R.layout.second);
                                get=(Button)findViewById(R.id.button);
                                nm=(EditText)findViewById(R.id.editName);
                                pw=(EditText)findViewById(R.id.editPassWord);
                                search=(Button)findViewById(R.id.Searchbutton);
                                search.setOnClickListener(this);
                                get.setOnClickListener(this);

//Bundle is using to get the intent extra fileds from passing intent.

                                Bundle b=getIntent().getExtras();
                                sname=b.getString("Name");
                                spwd=b.getString("PassW");
                }
                @Override
                public void onClick(View v) {
                                // TODO Auto-generated method stub
                                if(v==get){
                                nm.setText(sname);
                                pw.setText(spwd);
                                }
                                else if(v==search){
                                                Intent intn=new Intent(Intent.ACTION_VIEW);
                                                intn.setData(Uri.parse("http://www.google.com"));
                                                startActivity(intn);
                }              }               }