AlertDialog inside checkbox in Android Studio

336 views Asked by At

I am new to Android Studio. I want to create an AlertDialog, which contains a simple TextView, that appears at every lap of time (e.g. 5 min) inside the checkbox, so if the checkbox is clicked, the AlertDialog appears every 5 min. If it's not clicked, then nothing appears. Help me please.

1

There are 1 answers

1
0xCursor On BEST ANSWER

After a bit of experimentation, I was able to create something similar to what I think you want. This is the small project I created, but you can take just the parts of the code that are needed for your project.

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity
{
    TextView input;
    long startTime = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        final LinearLayout ll = new LinearLayout(this);

        setContentView(ll);

        CheckBox cb = new CheckBox(getApplicationContext());
        cb.setText("Checkbox");
        ll.addView(cb);

        cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
            {
                if (isChecked)
                {
                    startTime = System.currentTimeMillis();
                    timerHandler.postDelayed(timerRunnable, 0);
                }
                else
                {
                    timerHandler.removeCallbacks(timerRunnable);
                }
            }
        });
    }

    public void showDialog()
    {
        AlertDialog.Builder alert = new AlertDialog.Builder(this);
        alert.setTitle("Title");
        alert.setMessage("Message");

        input = new TextView (this);
        alert.setView(input);
        input.setText("Text");

        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface dialog, int whichButton)
            {
                // do stuff when Ok is clicked
            }
        });

        alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface dialog, int whichButton)
            {
                // do stuff when Cancel is clicked
            }
        });
        alert.show();
    }

    Handler timerHandler = new Handler();
    Runnable timerRunnable = new Runnable()
    {
        @Override
        public void run()
        {
            showDialog();
            // Edit the second parameter to whatever time you want in milliseconds
            timerHandler.postDelayed(this, 300_000);
        }
    };

    @Override
    public void onPause()
    {
        super.onPause();
        timerHandler.removeCallbacks(timerRunnable);
    }
}

Hopefully, this helps.