Android AlertDialog not visible - android

When alertDialog.show gets called, it does pop up a dialog but it remains invisible. I have to tap on the screen until I happen to click the "OK" button that's on the alert even though it is not visibly showing.
public class RecordSignals extends Activity {
private ImageView pulseOxImage;
static AlertDialog poDialog;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(saveInstanceState);
pulseOxImage = new ImageView(this);
pulseOxImage.setImageDrawable(ContextCompat
.getDrawable(this,R.drawable.pulse_ox_animation));
poDialog = new AlertDialog.Builder(this).create();
...
}
final Runnable callback = new Runnable() {
#Override
public void run() {
Thread thread = new Thread() {
#Override
public void run() {
...
runOnUiThread(new Runnable() {
#Override
public void run() {
if (pulseOxArtifact >= 2 || pulseOxOutOfTrack >= 2 || pulseOxSensorAlarm >= 2) {
pulseOxStatus();
pulseOxImage.setVisibility(View.VISIBLE);
poDialog.show();
}
}
});
}
};
}
};
public void pulseOxStatus() {
poDialog.setView(pulseOxImage);
poDialog.setButton("OK", new DialogInterface.OnClicklistener() {
#Override
public void onClick(DialogInterface dialog, int which) {
poDialog.dismiss();
}
});
}
}

Related

Unable to add window -- token android.os.BinderProxy

Below is my utility method for showing alert dialog in my application
private void displayInternetAlert(final Context context) {
if (!((GlobalActivity) context).isFinishing() && context instanceof GlobalActivity) {
((GlobalActivity) context).runOnUiThread(new Runnable() {
#Override
public void run() {
if (Validator.isNull(internetDialog)) {
internetDialog = new AlertDialog.Builder(Util.createCustomAlertDialog(context)).create();
internetDialog.setMessage(context.getString(R.string.internet_disable));
internetDialog.setButton(DialogInterface.BUTTON_NEUTRAL, context.getString(R.string.settings), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
/*
Don't need to write onactivity result now as we try to connect every time
*/
context.startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
dialog.dismiss();
}
});
internetDialog.setButton(DialogInterface.BUTTON_NEGATIVE, context.getString(R.string.ignore), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
internetDialog.setOnShowListener(new DialogInterface.OnShowListener() {
#Override
public void onShow(DialogInterface dialog) {
internetDialog.getButton(AlertDialog.BUTTON_NEUTRAL).setTextColor(context.getResources().getColor(R.color.dialog_cancel_button));
internetDialog.getButton(AlertDialog.BUTTON_NEUTRAL).setTypeface(null, Typeface.BOLD);
internetDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(context.getResources().getColor(R.color.dialog_cancel_button));
}
});
}
if (!internetDialog.isShowing()) {
internetDialog.show();
}
}
});
}
}
and Below is my GlobalActivity class
public class GlobalActivity extends AppCompatActivity {
#Nullable
#Bind(R.id.adView)
public AdView adView;
public GlobalActivity() {
ExceptionHandler.getExceptionHandler().setActivity(this);
}
#Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
setContentView(R.layout.ad_system);
ButterKnife.bind(this);
}
#Override
protected void finalize() throws Throwable {
}
#Override
protected void onStart() {
super.onStart();
loadGoogleAdd();
}
#Override
protected void onResume() {
super.onResume();
if(Validator.isNotNull(adView)){
adView.resume();
}
}
#Override
protected void onPause() {
super.onPause();
if(Validator.isNotNull(adView)){
adView.pause();
}
Util.setSharedPreferences(this, Util.SHARED_PREFERENCE_PREFERENCES, Util.EXPOSE_GSON.toJson(Util.getPreferences(this)));
}
private void loadGoogleAdd() {
if (adView != null) {
AdRequest adRequest = new AdRequest.Builder().
addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();
adView.loadAd(adRequest);
}
}
public void loadErrorActivity() {
startActivity(new Intent(GlobalActivity.this, ErrorActivity.class));
}
#Override
protected void onDestroy() {
super.onDestroy();
if(Validator.isNotNull(adView)){
adView.destroy();
}
Request.getRequest().dismissProgressDialog(this);
}
}
from which I am extending all activities and in that activity I am replacing fragments although I made check for activity finishing.
But I'm still receiving this exception in SM-G935F (Samesung Galaxy s7 edge). I'm using appcompat 23.1.1. is it platform or device specific issue or whether I'm making any mistake please help me in finding solution for this.
Any help is appreiciated.
I don't think it possible to display dialog from background thread, with assumption method displayInternetAlert is called inside activity/fragment, you can display dialog directly like this:
private void displayInternetAlert(final Context context) {
if (!((GlobalActivity) context).isFinishing() && context instanceof GlobalActivity) {
if (Validator.isNull(internetDialog)) {
internetDialog = new AlertDialog.Builder(Util.createCustomAlertDialog(context)).create();
internetDialog.setMessage(context.getString(R.string.internet_disable));
internetDialog.setButton(DialogInterface.BUTTON_NEUTRAL, context.getString(R.string.settings), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
/*
Don't need to write onactivity result now as we try to connect every time
*/
context.startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
dialog.dismiss();
}
});
internetDialog.setButton(DialogInterface.BUTTON_NEGATIVE, context.getString(R.string.ignore), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
internetDialog.setOnShowListener(new DialogInterface.OnShowListener() {
#Override
public void onShow(DialogInterface dialog) {
internetDialog.getButton(AlertDialog.BUTTON_NEUTRAL).setTextColor(context.getResources().getColor(R.color.dialog_cancel_button));
internetDialog.getButton(AlertDialog.BUTTON_NEUTRAL).setTypeface(null, Typeface.BOLD);
internetDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(context.getResources().getColor(R.color.dialog_cancel_button));
}
});
}
if (!internetDialog.isShowing()) {
internetDialog.show();
}
}
}

Is there a way to show AlertDialog in AsyncTask commonly?

I want to show AlertDialog which is in other class in AsyncTask.
Example>
public class WardListAsyncTask extends AsyncTask<HashMap<String, String>, String, Object> {
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected Object doInBackground(HashMap<String, String>... params) {
...
}
#Override
protected void onPostExecute(Object result) {
ConfirmAlertDialog(ez_WardList.this,"HI?");
//this method is in another Class & I want to use this method another Asynctask also..
}
and ConfirmAlertDialog is...
Context g_ctx;
String g_content;
public void ez_ConfirmAlertDialog(Context ctx, String content) {
this.g_ctx=ctx;
this.g_content=content;
runOnUiThread(new Runnable() {
#Override
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(g_ctx, AlertDialog.THEME_HOLO_LIGHT);
builder.setMessage(g_content).setPositiveButton(getString(R.string.kor_confirm),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
alertDialog.getWindow().setLayout(displayWidth / 2, LayoutParams.WRAP_CONTENT);
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(Color.rgb(10, 174, 239));
}
});
}
I think g_ctx.class.runonuiThread ... but I can't call runonuithread...
How can I solve it?
runOnUiThread method is present in Activity class. So should pass Activity into your class.
Example:
public class MyClass{
public void ez_ConfirmAlertDialog(Activity activity, String content) {
this.g_ctx=ctx;
this.g_content=content;
if(activity == null){
return;
}
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(g_ctx, AlertDialog.THEME_HOLO_LIGHT);
builder.setMessage(g_content).setPositiveButton(getString(R.string.kor_confirm),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
alertDialog.getWindow().setLayout(displayWidth / 2, LayoutParams.WRAP_CONTENT);
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(Color.rgb(10, 174, 239));
}
});
}
}
You can call it in main thread using handler
new Handler(Looper.getMainLooper()).post(new Runnable() {
#Override
public void run() {
// Your code here
}
});

Suspend QR code detection while dialog is shown

I'm trying to create a real time QR Code decoder which allows user to confirm/cancel data read from the camera view.
(Of course, according confirmation or cancelation, it will execute some code...)
But there's a bad point:
The app I coded keeps reading qr code data even while in dialog and I can't find a way to prevent it.
Here is my main activity:
public class MainReadActivity extends Activity {
private SurfaceView cameraView;
private TextView barcodeInfo;
private BarcodeDetector barcodeDetector;
private CameraSource cameraSource;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_read);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
cameraView = (SurfaceView) findViewById(R.id.camera_view);
cameraView.requestFocus();
barcodeInfo = (TextView) findViewById(R.id.code_info);
barcodeDetector = new BarcodeDetector.Builder(this).setBarcodeFormats(Barcode.QR_CODE).build();
cameraSource = new CameraSource.Builder(this, barcodeDetector).build();
cameraView.getHolder().addCallback(new SurfaceHolder.Callback() {
#Override
public void surfaceCreated(SurfaceHolder holder) {
try {
cameraSource.start(cameraView.getHolder());
} catch (IOException ie) {
Log.e("CAMERA SOURCE", ie.getMessage());
}
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
cameraSource.stop();
}
});
barcodeDetector.setProcessor(new Detector.Processor<Barcode>() {
#Override
public void release() {
}
#Override
public void receiveDetections(Detector.Detections<Barcode> detections) {
final SparseArray<Barcode> barcodes = detections.getDetectedItems();
if (barcodes.size() != 0) {
barcodeInfo.post(new Runnable() { // Use the post method of the TextView
public void run() {
ConfirmationDialogFragment myDialog = new ConfirmationDialogFragment();
myDialog.show(getFragmentManager(),"");
barcodeInfo.setVisibility(View.VISIBLE);
barcodeInfo.setText(barcodes.valueAt(0).displayValue);
}
});
}
}
});
}
And here is my DialogFragment:
public class ConfirmationDialogFragment extends DialogFragment {
public Dialog ConfirmationDialogFragment(Bundle savedInstanceState){
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View v = inflater.inflate(R.layout.dialog_confirmation,null);
builder.setView(v);
Dialog dialog = builder.create();
return dialog;
}
}
Can anyone help me?
Bests,
P.
Stop and start camera on barcodeDetector callback
barcodeDetector.setProcessor(new Detector.Processor<Barcode>() {
#Override
public void release() {
}
#Override
public void receiveDetections(Detector.Detections<Barcode> detections) {
final SparseArray<Barcode> barcodes = detections.getDetectedItems();
if (barcodes.size() != 0) {
cameraSource.stop();
barcodeInfo.post(new Runnable() { // Use the post method of the TextView
public void run() {
builder
.setMessage("Are you sure you want to reset the count?")
.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface arg0, int arg1) {
cameraSource.start(cameraView.getHolder());
}
})
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(MainActivity.this, "Did Reset!", 5).show();
}
})
.create();
barcodeInfo.setVisibility(View.VISIBLE);
barcodeInfo.setText(barcodes.valueAt(0).displayValue);
}
});
}
}
});
I think that you forget some like this.
builder.setPositiveButton(Ok, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss(); //this close the dialog.
}
});
builder.setNegativeButton(Back, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
///do another thing
}
});
I hope this help you
just call the Camera.release() method after detection
#Override
public void receiveDetections(Detector.Detections < TextBlock > detections) {
final SparseArray < TextBlock > items = detections.getDetectedItems();
if (items.size() != 0) {
mTextView.post(new Runnable() {
#Override
public void run() {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < items.size(); i++) {
TextBlock item = items.valueAt(i);
stringBuilder.append(item.getValue());
stringBuilder.append("\n");
}
mTextView.setText(stringBuilder.toString());
mCameraSource.release();
}
});
}
}

Stop Current Thread Issue

My Problem is I am Running one Method into Thread and after 30 Seconds I will display alertdialog and click on alertdialog's ok button i will stop current thread but problem is thread is not stop, following is my Code and sorry for bad english comunication
public class CountDownTimerActivity extends Activity implements Runnable {
Thread t;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
t = new Thread(this);
t.start();
}
#Override
public void run() {
// TODO Auto-generated method stub
myTimer.start();
mDeclaration();
myTimer.cancel();
}
private CountDownTimer myTimer = new CountDownTimer(30000, 1000) {
// This will give you 30 sec timer with each tick at 1 second
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
t.interrupt();
//t.stop();
AlertDialog.Builder alert = new AlertDialog.Builder(
CountDownTimerActivity.this);
alert.setMessage("Loading...");
alert.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
alert.show();
}
};
public void mDeclaration() {
for (int i = 0; i < 100000; i++) {
System.out.println("Printing OK" + i);
}
}
}
see this question...instead you can put a flag in run method.. and check whether to run or not to run the code in thread.
try this:-
public class CountDownTimerActivity extends Activity implements Runnable {
Thread t;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
t = new Thread(this);
t.start();
}
#Override
public void run() {
// TODO Auto-generated method stub
myTimer.start();
mDeclaration();
myTimer.cancel();
}
private CountDownTimer myTimer = new CountDownTimer(30000, 1000) {
// This will give you 30 sec timer with each tick at 1 second
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
AlertDialog.Builder alert = new AlertDialog.Builder(
CountDownTimerActivity.this);
alert.setMessage("Loading...");
alert.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// t.interrupt();
t.stop();
}
});
alert.show();
}
};
public void mDeclaration() {
for (int i = 0; i < 100000; i++) {
System.out.println("Printing OK" + i);
}
}
}

Android - How to make some validations in sequence

I need to do some validations sequentially and some of them involve complex database operations.
So, I need to do this in a separated thread of UI Thread, ok?
But some validations show messages to user, what need confirmation and
when user confirm, the next validation should be call.
This code example explains what I want to implement:
void makeValidation1(){
if(condition1Valid()){
makeValidation2();
}else{
DialogInterface.OnClickListener onClick = new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
makeValidation2();
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setMessage("really want to do this?")
.setPositiveButton("Yes", onClick);
builder.create().show();
}
}
void makeValidation2(){
if(condition2Valid()){
}else{
//...
}
}
boolean condition1Valid() {
// complex database Operations
return false;
}
boolean condition2Valid() {
//complex database Operations
return false;
}
//...
void makeValidation9(){
//...
}
My question is: What the best way/pattern to implement this?
1 - Create one asyncTask for each validation? (I cant create only one AsyncTask, because confirmation messages can stop flux).
2 - Create a Runnable for each validation and create thread to run that when need call next validation?
3 - ???
edit
I tested this code #BinaryBazooka, but isnt work. Any help?
public class MainActivity extends Activity implements OnClickListener {
Thread mThread;
ProgressDialog mProgressDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button button = new Button(this);
button.setText("Start");
setContentView(button, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
button.setOnClickListener(this);
}
#Override
public void onClick(View v) {
mThread = new Thread(new Runnable() {
#Override
public void run() {
validations();
}
});
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Start Thread?");
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
mProgressDialog = new ProgressDialog(MainActivity.this);
mProgressDialog.show();
mThread.run();
}
});
builder.create().show();
}
void validations(){
//this method go on separated thread
validation1();
validation2();
validation3();
}
void validation1(){
if(true){
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setMessage("Validation 1 failed. Go validation 2?");
builder.setPositiveButton("Go", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
mProgressDialog.show();
//if user confirm, continue validation thread
mThread.notify();
}
});
builder.setOnCancelListener(new OnCancelListener() {
#Override
public void onCancel(DialogInterface dialog) {
//if user cancel, stop validation thread
mThread.interrupt();
}
});
runOnUiThread(new Runnable() {
#Override
public void run() {
mProgressDialog.hide();
builder.create().show();
}
});
try {
synchronized (mThread) {
//wait for user confirmation
mThread.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private void validation2() {
if(true){
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setMessage("validacao 2 failed. Go validation 3?");
builder.setPositiveButton("Go", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
mProgressDialog.show();
mThread.notify();
}
});
builder.setOnCancelListener(new OnCancelListener() {
#Override
public void onCancel(DialogInterface dialog) {
mThread.interrupt();
}
});
runOnUiThread(new Runnable() {
#Override
public void run() {
mProgressDialog.hide();
builder.create().show();
}
});
try {
synchronized (mThread) {
mThread.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private void validation3() {
Log.i("TAG", "<<<<<<<<<< >>>>>>>>>>>>");
runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(MainActivity.this, "finished", Toast.LENGTH_SHORT);
}
});
}
}
I would create a new thread and sleep it during these dialog calls, you can access the UI directly from within your runnable with..
runOnUiThread(new Runnable() {
public void run() {}
});
So something like..
Thread someThread = new Thread(new Runnable() {
#Override
public void run(){
runOnUiThread(new Runnable() {
public void run()
{
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage(R.string.msg);
builder.setPositiveButton(R.string.btn_ok, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
someThread.notify();
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
}
someThread.wait();
Works with AsyncTask. Ty.
Code:
public class MainActivity extends Activity implements OnClickListener {
//Thread mThread;
ProgressDialog mProgressDialog;
private ValidationsAsyncTask async;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button button = new Button(this);
button.setText("Start");
setContentView(button, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
button.setOnClickListener(this);
}
#Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Start Thread?");
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
mProgressDialog = new ProgressDialog(MainActivity.this);
mProgressDialog.show();
async = new ValidationsAsyncTask();
async.execute();
}
});
builder.create().show();
}
void validation1(){
if(true){
runOnUiThread(new Runnable() {
#Override
public void run() {
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setMessage("Validation 1 failed. Go validation 2?");
builder.setPositiveButton("Go", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
mProgressDialog.show();
//if user confirm, continue validation thread
synchronized (async) {
async.notify();
}
}
});
builder.setOnCancelListener(new OnCancelListener() {
#Override
public void onCancel(DialogInterface dialog) {
//if user cancel, stop validation thread
async.cancel(true);
}
});
mProgressDialog.hide();
builder.create().show();
}
});
Log.i("TAG - validation1", Thread.currentThread().getName());
try {
synchronized (async) {
//wait for user confirmation
async.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private void validation2() {
if(true){
runOnUiThread(new Runnable() {
#Override
public void run() {
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setMessage("validacao 2 failed. Go validation 3?");
builder.setPositiveButton("Go", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
mProgressDialog.show();
synchronized (async) {
async.notify();
}
}
});
builder.setOnCancelListener(new OnCancelListener() {
#Override
public void onCancel(DialogInterface dialog) {
async.cancel(true);
}
});
mProgressDialog.hide();
builder.create().show();
}
});
try {
synchronized (async) {
async.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private void validation3() {
runOnUiThread(new Runnable() {
#Override
public void run() {
mProgressDialog.dismiss();
Toast.makeText(MainActivity.this, "finished", Toast.LENGTH_SHORT).show();
}
});
}
class ValidationsAsyncTask extends AsyncTask<Void, Void, Void>{
#Override
protected Void doInBackground(Void... params) {
validation1();
validation2();
validation3();
return null;
}
#Override
protected void onCancelled() {
Toast.makeText(MainActivity.this, "cancelled", Toast.LENGTH_LONG).show();
}
}
}

Categories

Resources