Monday, July 22, 2013

Android: Put CheckBox & URL In a AlertBox



Create a custom view and the view to AlertDialogBox to get.

Initialize CheckBox, TextView.
add both the views to layout.
Then add the layout to the alertDialog setView.
Run your code and check :)

For URL in alert box, have setMovementMethod(LinkMovementMethod.getInstance()) to the TextView, so that we can have action for the text.


<string name="url">Accept Google Link <a href="http://www.google.com"> Click Here </a></string>

public void callAlertBox() {

       CheckBox checkBox = new CheckBox(this);

       TextView textView = new TextView(this);
       textView.setMovementMethod(LinkMovementMethod.getInstance());
       textView.setText(R.string.url);

       LinearLayout checkBoxLayout = new LinearLayout(this);
       checkBoxLayout.setLayoutParams(new LinearLayout.LayoutParams(
                     LinearLayout.LayoutParams.MATCH_PARENT,
                     LinearLayout.LayoutParams.MATCH_PARENT));

       checkBoxLayout.addView(checkBox);
       checkBoxLayout.addView(textView);

       AlertDialog.Builder altDialog = new AlertDialog.Builder(this);
       altDialog.setView(checkBoxLayout);
       altDialog.setMessage("Check Box & URL in Alert Box");
       altDialog.setPositiveButton("OK",
              new DialogInterface.OnClickListener() {
                     @Override
                     public void onClick(DialogInterface dialog, int which) {
                                                                     Toast.makeText(getApplicationContext(), "Ok button Pressed", Toast.LENGTH_SHORT).show();
                     }
              });
       altDialog.setNegativeButton("Canceel",
              new DialogInterface.OnClickListener() {
                     @Override
                     public void onClick(DialogInterface dialog, int which) {
                                         dialog.dismiss();
                     }
              });

       altDialog.show();
}

Output:

No comments :

Post a Comment