CustomAlert without any custom Layout in android.

By | February 6, 2012

We can create a dialog with custom alert dialog with our xml in android.
But for making simple alerts we can make custom alerts through code itself.

Here is a simple function for this. Here I am having a Listview and an edittext with two buttons in the alertBox.

  public void addDialog(){

          String test[] = {"hello1","hello2"};

    	  AlertDialog.Builder builder = new AlertDialog.Builder(this);
          builder.setCancelable(true);
          builder.setTitle("My Title");
          builder.setInverseBackgroundForced(true);
          builder.setIcon(R.drawable.android_2);
          builder.setItems(test, new OnClickListener() {
  			@Override
  			public void onClick(DialogInterface dialog, int which) {
  				System.out.println("onClick " + which);
  			}
  		  });
          builder.setAdapter(myadp, new OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				System.out.println("DialogInterface : " + which);
			}
		  });

          EditText ed = new EditText(this);
          builder.setView(ed);
          builder.setPositiveButton("Yes",
                  new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialog,
                              int which) {
                          dialog.dismiss();
                      }
                  });
          builder.setNegativeButton("No",
                  new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialog,
                              int which) {
                          dialog.dismiss();
                      }
                  });
          AlertDialog alert = builder.create();
          alert.show();
    }

Please leave your valuable comments on this post.

Custom Alert

Website Pin Facebook Twitter Myspace Friendfeed Technorati del.icio.us Digg Google StumbleUpon Premium Responsive

Leave a Reply

Your email address will not be published. Required fields are marked *