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.
Asked
Active
Viewed 326 times
-1

0xCursor
- 2,242
- 4
- 15
- 33

marouane junior
- 3
- 1
-
Did you write code that creates the checkbox and AlertDialog? – 0xCursor Jun 01 '18 at 23:59
-
for the checkbox yes, but for the alert dialog i dont know how – marouane junior Jun 02 '18 at 01:03
-
Here, I can find you code for how to make the AlertDialog and maybe I can integrate a timer with it. – 0xCursor Jun 02 '18 at 01:28
-
Ok. Thank you.. – marouane junior Jun 02 '18 at 02:46
-
It's ok, take your time and thank you again – marouane junior Jun 02 '18 at 13:37
1 Answers
0
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.

0xCursor
- 2,242
- 4
- 15
- 33
-
Thank you for your time and kindness , im gonna working on this code – marouane junior Jun 02 '18 at 22:43