Dialogue box does not show up - java

I am creating a dialogue box that should appear to the user in case of the GPS service is disabled. But what is happening is, although I disabled the service manually to force the dialogue appear, the App starts and nothing is happening.
The code below shows how I tried to create the dialogue box and when. Please let me know if there is any mistake.
JavaCode:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gpstest00);
locMgr = (LocationManager) getSystemService(LOCATION_SERVICE);
gpsEnable = locMgr.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!gpsEnable) {
showGPSDialogueBox();
}
locMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 1, this.locationListener);
}
/*if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
}*/
private void showGPSDialogueBox() {
AlertDialog.Builder alertDialogue = new AlertDialog.Builder(this);
alertDialogue.setTitle("GPS Settings");
alertDialogue.setMessage("GPS is deactivated. Do you want to switch " + to settings menu to activate it?");
alertDialogue.setPositiveButton("Settings",new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
});
alertDialogue.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.cancel();
}
});
}// Enf of showDialogueBox function.

You need to call show function for the dialog box to show
alertDialogue.show();

You need to create AlertDialog from AlertDialog.Builder object then show the AlertDialog using show() method as follows...
AlertDialog dialog = alertDialogue.create();
dialog.show();
Update your showGPSDialogueBox() as below...
private void showGPSDialogueBox() {
AlertDialog.Builder alertDialogue = new AlertDialog.Builder(this);
alertDialogue.setTitle("GPS Settings");
alertDialogue.setMessage("GPS is deactivated. Do you want to switch " +
" to settings menu to activate it?");
alertDialogue.setPositiveButton("Settings",new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
});
alertDialogue.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.cancel();
}
});
AlertDialog dialog = alertDialogue.create();
dialog.show();
}

Related

How to make AlertDialog view in Input method Service?

I would like to make an input method which is used only for SoftKeyboard. My how to make popup onkey event in input method.
I am creating Dialog but here is problem you see my logcat:
09-14 11:16:54.349: E/MessageQueue-JNI(7172): at android.inputmethodservice.KeyboardView.detectAndSendKey(KeyboardView.java:824)
Softkeyboard.java
Here java code
public void onKey(int primaryCode, int[] keyCodes) {
if (primaryCode == -2) {
// add this to your code
dialog = builder.create();
Window window = dialog.getWindow();
WindowManager.LayoutParams lp = window.getAttributes();
lp.token = mInputView.getWindowToken();
lp.type = WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
window.setAttributes(lp);
window.addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
// end addons
builder.show();
}
Thanks for advance..
You need to have ACTION_MANAGE_OVERLAY_PERMISSION permission to open/display Alert onkey event in input method.
Before you set your custom Keyboard Check for Overlay Permission.
final boolean overlayEnabled = Settings.canDrawOverlays(MainActivity.this);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !overlayEnabled) {
openOverlaySettings();
}
#TargetApi(Build.VERSION_CODES.M)
private void openOverlaySettings() {
final Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getPackageName()));
try {
startActivityForResult(intent, RC_OVERLAY);
} catch (ActivityNotFoundException e) {
Log.e("MainActivity", e.getMessage());
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case RC_OVERLAY:
final boolean overlayEnabled = Settings.canDrawOverlays(this);
if (overlayEnabled) {
preferenceManager.setBooleanPref(IS_CYBER_BULLING_ON, true);
Intent intent = new Intent(MainActivity.this, ImePreferences.class);
startActivity(intent);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
} else {
// switchCyberBulling.setChecked(false);
}
// Do something...
break;
}
}
Then inside your SoftKeyboard.java class, put code for alert dialog & set alert type of "TYPE_APPLICATION_OVERLAY".
AlertDialog.Builder builder = new AlertDialog.Builder(this)
//set icon
.setIcon(android.R.drawable.ic_dialog_alert)
//set title
.setTitle("Warning!")
//set message
.setMessage("This is alert dialog!")
//set positive button
.setPositiveButton("Okay", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
//set what would happen when positive button is clicked
dialogInterface.dismiss();
}
})
//set negative button
.setNegativeButton("Dismiss", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
//set what should happen when negative button is clicked
dialogInterface.dismiss();
}
});
AlertDialog alertDialog = builder.create();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
}else{
alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
}
alertDialog.show();
Do not forget for Draw Overlay Permission. Hope this helps you. :)

Android alert dialog and set positive button

This is for a slider puzzle. I want to show a dialog box with OK button when the puzzle is completed. When the OK button is pressed, I use an Intent to load a website via Android browser. Only problem is that with the current code, when the puzzle is complete, it does not load a box (it does when I use null). It doesn't do anything. Any ideas?
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(!puzzle.isSolved() ? R.string.title_stats : stats.isNewBest() ? R.string.title_new_record : R.string.title_solved);
builder.setMessage(msg);
builder.setPositiveButton(R.string.label_ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent("android.intent.action.VIEW", Uri.parse("http://www..com"));
Bundle b = new Bundle();
b.putBoolean("new_window", true); //sets new window
intent.putExtras(b);
startActivity(intent);
}
});
AlertDialog.Builder builder = new AlertDialog.Builder(your_activity.this);
builder.setTitle(!puzzle.isSolved() ? R.string.title_stats : stats.isNewBest() ? R.string.title_new_record : R.string.title_solved);
builder.setMessage(msg);
builder.setPositiveButton(R.string.label_ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent("android.intent.action.VIEW", Uri.parse("http://www..com"));
Bundle b = new Bundle();
b.putBoolean("new_window", true); //sets new window
intent.putExtras(b);
startActivity(intent);
}
});
builder.show();
try this
Check the below code. It may help you
AlertDialog alertDialog = new AlertDialog.Builder(
GeneralClassPhotoCaptureImageVideo.this).create(); // Read
// Update
alertDialog.setTitle("Title of dialog");
alertDialog
.setMessage("contents");
alertDialog.setButton(Dialog.BUTTON_POSITIVE, "Ok",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Intent intent = new Intent("android.intent.action.VIEW", Uri.parse("http://www..com"));
Bundle b = new Bundle();
b.putBoolean("new_window", true); //sets new window
intent.putExtras(b);
startActivity(intent);
}
});
alertDialog.setButton(Dialog.BUTTON_NEGATIVE, "Cancel",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
alertDialog.show();
Add following code to show the dialog.
AlertDialog alert = builder.create();
alert.show();

Execution doesn't wait to Alertdialog.dismiss()

I'm developing an Android application and I have this question:
How can I do to make execution waits until user has selected an option from an AlertDialog?
This is my code:
if (mPerson== null)
{
mPerson = new Person();
AlertDialog dialog = null;
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(getString(R.string.dialog_message_select))
.setTitle(getString(R.string.dialog_title_attention));
builder.setPositiveButton(R.string.male, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
mPerson.setGender(Gender.male);
dialog.dismiss();
}
});
builder.setNegativeButton(R.string.female, new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int arg1)
{
mPerson.setGender(Gender.female);
dialog.dismiss();
}
});
dialog = builder.create();
dialog.show();
}
// TODO: Show data.
getWidgetsRefereces();
customizeLayout();
loadSpinnerValues();
After dialog.dismiss() I have to execute this:
// TODO: Show data.
getWidgetsRefereces();
customizeLayout();
loadSpinnerValues();
Do the following:
AlertDialog dialog = null;
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(getString(R.string.dialog_message_select))
.setTitle(getString(R.string.dialog_title_attention));
builder.setPositiveButton(R.string.male, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
mPerson.setGender(Gender.male);
dialog.dismiss();
postSelection();
}
});
builder.setNegativeButton(R.string.female, new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int arg1)
{
mPerson.setGender(Gender.female);
dialog.dismiss();
postSelection();
}
});
dialog = builder.create();
dialog.show();
}
Call this method once the selection is complete.
public void postSelection(){
getWidgetsRefereces();
customizeLayout();
loadSpinnerValues();
}
Think in terms of event based execution. If you want some code to execute when you press a button, then wire it to do so. Place the code in question in a method that you can call whenever you want.
Generally, when you are programming on Android, you need to adhere to the event based nature of the platform. Traditional procedural sequential thinking will lead you to dead ends.
You have to customize your dialog interface like this .
You should use onDismissListener on dialog interface.Dont make it anonymous.
private class MyDialogInterfaceMethod implements DialogInterface.OnClickListener,DialogInterface.OnDismissListener
{
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
//when user click a button
}
#Override
public void onDismiss(DialogInterface dialog) {
// TODO Auto-generated method stub
//put your code here
}
}
Than use it on your alert dialog like this
builder.setNegativeButton("CANCEL",new MyDialogInterfaceMethod ());

Application alarm causing a weird error until I press the back button

I have an alarm in my application that notifies me every 10 minutes. It works fine but each time when the alarm notifies me, my application name will show as a box in the middle of the screen, and I can't press anything until I press the back button. It's really weird!
Here is my code:
// Alert Code
final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Confirmation");
alertDialog.setMessage("Are you sure you want to send this report?");
run(gotDataS);
// Timer Code
pendingIntent = PendingIntent.getService(CreateNewForm_3.this, 0,
new Intent(NewForm_3.this, MyAlarmService.class), 0);
Send.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
alertDialog.setButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
Toast.makeText(NewForm_3.this, "Sent", 0)
.show();
long firstTime = SystemClock.elapsedRealtime();
am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
firstTime + 10 * 1000, 30 * 1000, pendingIntent);
// firstTime Will be the current_time + ( 10 * 60 * 1000) =)
// 10* 1000 will be (10 * 60 * 1000)
new Handler().postDelayed(new Runnable() {
public void run() {
bCancel.performClick();
}
}, (30 * 1000));
// ( 30 * 1000) will be firstTime + ( 2- duration)
Intent toRecentCases = new Intent(CreateNewForm_3.this,
LMPActivity.class);
startActivity(toRecentCases);
}
});
alertDialog.setButton2("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// Here you can add functions
}
});
alertDialog.setIcon(android.R.drawable.ic_dialog_alert);
alertDialog.show();
}
});
bCancel.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
am.cancel(pendingIntent);
}
});
//For Notification -3-
final AlertDialog alertDialog3 = new AlertDialog.Builder(this).create();
alertDialog3.setTitle("Confirmation");
alertDialog3.setMessage("Are you sure you want to quite?");
// Press cancel button it will move user to actvity p
Cancelb.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
alertDialog3.setButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
Intent toRecentCases = new Intent(NewForm_3.this,
LMPActivity.class);
startActivity(toRecentCases);
}
});
alertDialog3.setButton2("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// Doing nothing!
}
});
alertDialog3.setIcon(android.R.drawable.ic_dialog_alert);
alertDialog3.show();
}
});
}
This is Showing Dialog Activity
public class ShowingDialog extends Activity {
boolean b;
String CancelMsg;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//For Notification -1-
final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Confirmation");
alertDialog.setMessage("Do you really want it?");
// For Notification -2-
final AlertDialog alertDialog2 = new AlertDialog.Builder(this).create();
alertDialog2.setTitle("Confirmation");
alertDialog2.setMessage("Are you sure you want it?");
alertDialog.setButton("yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent= new Intent(ShowingDialog.this,MyPage.class);
startActivity(intent);
;
}
});
alertDialog.setButton2("no",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
alertDialog2.setButton("yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Here you can add functions
// Sending a Message to server that the plaintiff found the case
// For Sending SMS with cancel Request
// Getting Case_ID + putting it inside CancelMsg
CancelMsg = "Case_ID cancel";
if (!b) {
try {
sendSMS("5556", CancelMsg);
Toast.makeText(ShowingDialog.this, "Sent", Toast.LENGTH_LONG)
.show();
} catch (Exception e) {
// TODO Auto-generated catch block
Toast.makeText(ShowingDialog.this, e.getMessage(),
Toast.LENGTH_LONG).show();
}
}
}
});
alertDialog2.setButton2("no", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Here you can add functions
// Do nothing
}
});
alertDialog2.setIcon(android.R.drawable.ic_dialog_alert);
alertDialog2.show();
}
});
alertDialog.setIcon(android.R.drawable.ic_dialog_alert);
alertDialog.show();
}
public void sendSMS(String number, String msg) throws Exception {
if (!b) {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(number, null, msg, null, null);
}
b = true;
}
}
In Manifest :
<activity
android:name=".ShowingDialog"
android:theme="#android:style/Theme.Dialog" />
It doesn't appear that you're overloading the back statement, so when you hit the back button you're traveling back up your Back Stack. The Android documentation has more information on this:
Tasks and Back Stack

Showing a dialog box with contents of choosing file and camera

i want to open a dialog box that gives me two option.
1- Choose file from SD Card
2- Take a snapshot from Camera
Right now i am using this code
receipt.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
showDialog(RECEIPT_DIALOG_ID);
}
});
protected Dialog onCreateDialog(int id) {
Dialog dialog = null;
AlertDialog.Builder builder = new Builder(this);
case RECEIPT_DIALOG_ID:
builder.setTitle("Choose your file");
dialog = builder.create();
return dialog;
}
Now, how can i add these two options.
Best Regards
Try this it provides both the options
final CharSequence[] items = {"Camera", "Memory Card"};
builder.setTitle(R.string.pic_option);
builder.setIcon(R.drawable.camera_icon);
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
launchCamera(item);
}
});
builder.create();
builder.show();
and the Function launchCamera(item) is here
public void launchCamera(int id){
switch (id) {
case 0:
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
((Activity)getParent()).startActivityForResult(cameraIntent, 1888);
break;
case 1:
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
((Activity)getParent()).startActivityForResult(intent, 2);
break;
default:
break;
}
}
Use this directly in your method.It will show 2 buttons on dialog.
Dialog dialog;
dialog = new Dialog(getParent());
dialog.setContentView(R.layout.camdialog);
dialog.setTitle(" Select the Image");
dialog.setCancelable(true);
sdCard = (Button) dialog.findViewById(R.id.sdCard);
camDialog = (Button) dialog.findViewById(R.id.camDialog);
dialog.show();
dialog.getWindow().setLayout(LayoutParams.FILL_PARENT, 200);
try this...
new AlertDialog.Builder(menu)
.setTitle("Choose your file")
.setMessage("Your msg")
.setPositiveButton("Choose file from SD Card",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
// Your code
}
})
.setNegativeButton("Take a snapshot from Camera",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
//Your code
}
}).show();

Categories

Resources