0

I have created six dynamic Checkbox for my app and i want to Pop-up an AlertBox if the check box is checked but i am finding any result from my code. I am true value to the checkbox but no AlertBox is coming.

Java Code :

package com.example.mycheckbox;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.util.SparseBooleanArray;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ListView;

public class MainActivity extends Activity 
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LinearLayout ll = (LinearLayout) findViewById(R.id.ii);
        CheckBox[] cb = new CheckBox[6];

        for (int i = 0; i < 6; i++) 
        {
            cb[i] = new CheckBox(this);
            cb[i].setText("Dynamic Checkbox " + i);
            cb[i].setId(i + 6);

            if (cb[i].isChecked())
            {
                AlertDialog.Builder myalert = new AlertDialog.Builder(this);

                myalert.setTitle("Demo Title - "+i).setMessage(" Message from - "+i).setNeutralButton("Close", null).show();
            }

            ll.addView(cb[i]);                       
        }
    }
}

I want to pop an AlertDialog for each CheckBox if that is checked.

Mickäel A.
  • 9,012
  • 5
  • 54
  • 71
Anshuman Pattnaik
  • 883
  • 3
  • 16
  • 37

3 Answers3

2

The AlertDialog isn't showing cause you didn't create the AlertDialog object from the AlertDialog.Builder. Create ab AlertDialog object as below and then call show() method using that object to show the Dialog.

Another thing, you didn't assign the Listener to those CheckBox. Now set the listener to them and show the dialog as below...

cb[i].setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

   @Override
   public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

        AlertDialog.Builder myalert = new AlertDialog.Builder(this);

        myalert.setTitle("Demo Title - "+i);
        myalert.setMessage(" Message from - "+i);
        myalert.setNeutralButton("Close", null);

        AlertDialog dialog = myalert.create();
        dialog.show();
   }
}
Hamid Shatu
  • 9,664
  • 4
  • 30
  • 41
  • No friend , not showing any alert box.... i think your code is quite perfect but not getting any result.... – Anshuman Pattnaik Mar 07 '14 at 19:12
  • Yes man, getting the alertbox but it's showing the last index value for each alert box , so the result i am getting for each alertbox is Demo Title - 6 , Message from - 6 .... – Anshuman Pattnaik Mar 07 '14 at 19:26
  • Please, try it yourself...I have to go cause its late night here...if you didn't get any solution then I will check it in the morning...best of luck...and if its helpful then don't forget to accept it. – Hamid Shatu Mar 07 '14 at 19:31
0

May be this will help you.Checkbox Listener. Add them while creating the checkbox. Or refer to the checkbox created by the id you are setting and set a listener then.

Community
  • 1
  • 1
Prachur
  • 1,100
  • 7
  • 24
  • 42
0

As default, they won't be checked, so no popup will be shown, you should add a listener if someone check it or not.

cb[i].setOnCheckedChangeListener(new OnCheckedChangeListener() {

      @Override
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
          // alert dialog 

      }
});

or you can implements OnCheckedChangeListener and use one listener for all checkboxes.

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    //

}
Orhan Obut
  • 8,756
  • 5
  • 32
  • 42