Display alert when Switching from On Sate to Off state in Toggle Button : Android
<p>I  was trying to Display alert when i switch from On state to Off state in toggle button
i use toggle button in Adapter in side list view , please refer my code below, and please help me on doing when user switches from On sate to Off state in Toggle button...
actually when i am using IsChecked() then its always called either on to off or off to n , but i need to display Alert when only switching from On to off..</p>

<p>Xml:</p>

<pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp" &gt;

&lt;TextView
    android:id="@+id/state_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" /&gt;

&lt;ToggleButton
    android:id="@+id/statetoggleButton"
    style="@style/on_off_togle_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:background="@drawable/togllebuttonselector" /&gt;
</code></pre>

<p></p>

<p>Adapter class is : </p>

<pre><code>private final Activity context;
    public final List&lt;StatesModel&gt; list;
    AlertDialog alertDialog;

    public List&lt;StatesModel&gt; getList() {
        return list;
    }

    public StateListAdapter(Activity context, List&lt;StatesModel&gt; list) {
        super(context, R.layout.states_list, list);
        this.context = context;
        this.list = list;
    }

    static class ViewHolder {
        protected TextView text;
        protected ToggleButton checkbox;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;
        if (convertView == null) {
            LayoutInflater inflator = context.getLayoutInflater();
            view = inflator.inflate(R.layout.states_list, null);
            final ViewHolder viewHolder = new ViewHolder();
            viewHolder.text = (TextView) view.findViewById(R.id.state_name);
            viewHolder.checkbox = (ToggleButton) view
                    .findViewById(R.id.statetoggleButton);
            viewHolder.checkbox
                .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(
                            final CompoundButton buttonView,
                            boolean isChecked) {
                        final StatesModel element = (StatesModel) viewHolder.checkbox
                                .getTag();

                          if (!isChecked) { AlertDialog.Builder builder =
                          new AlertDialog.Builder( context);
                          builder.setTitle("Warning"); String message =
                          "Title is here"
                          ; builder.setMessage(message);
                          builder.setPositiveButton("OK", new
                          DialogInterface.OnClickListener() {

                          @Override public void onClick( DialogInterface
                          dialog, int which) { alertDialog.dismiss();
                          element.setSelected(buttonView .isChecked()); }
                          }); builder.setNegativeButton("Cancel", new
                          DialogInterface.OnClickListener() {

                          @Override public void onClick( DialogInterface
                          dialog, int which) { buttonView.setChecked(true);
                          alertDialog.dismiss(); } }); alertDialog =
                          builder.create(); alertDialog.show(); } else





                        element.setSelected(buttonView.isChecked());

                    }
                });
            view.setTag(viewHolder);
            viewHolder.checkbox.setTag(list.get(position));
        } else {
            view = convertView;
            ((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
        }
        ViewHolder holder = (ViewHolder) view.getTag();
        holder.text.setText(list.get(position).getName());
        holder.checkbox.setChecked(list.get(position).isSelected());
        return view;
    }
</code></pre>

<p>Please check On Checked change method ..thanks in Adv...any help would be highly appreciated..</p>
