I am creating a alert box in android studio - java

I am trying to build a alert box in android studio, i have 2 xml and 1 activity class where on click of a button i want to show the other layout.
I am getting a error, here is my code of MainActivity
cancelBookingButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LayoutInflater inflater = getLayoutInflater();
View alertLayout = inflater.inflate(R.layout.alert_cancel_confirm, null);
final TextView disAgree = (TextView) alertLayout.findViewById(R.id.TextDisagree);
final TextView Agree = (TextView) alertLayout.findViewById(R.id.TextAgree);
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Current Booking");
// This will set the view from XML inside ALertDialog
alert.setView(alertLayout);
// disabling cancel of AlertDialog on click of back button and outside touch
alert.setCancelable(false);
AlertDialog dialog = alert.create();
dialog.show();
}
});
In this line i am getting error
AlertDialog.Builder alert = new AlertDialog.Builder(this);
In Builder cannot be applied
Any idea why i am getting this.

AlertDialog.Builder alert = new AlertDialog.Builder(YourActivity.this);
alert.setTitle("Current Booking");
// This will set the view from XML inside ALertDialog
alert.setView(alertLayout);
// disabling cancel of AlertDialog on click of back button and outside touch
alert.setCancelable(false);
AlertDialog dialog = alert.create();
dialog.show();

Related

<Android> Full screen mode is broken and a transparent navigation bar appears when alert is made

I'm developing application for android tablet and wants it to be always keeping full screen mode.
I set flags for that, but when the alert dialog appears it breaks.
When it first appears it keeps full screen, however, when alert dialog is made again, the full screen mode is broken and a transparent navigation bar appears.
Why does the full screen mode fluctuate? Below is my code.
AlertDialog.Builder warning = new AlertDialog.Builder(my_context);
View view = ActivityTeetimeDetail.this.getLayoutInflater().inflate(my_alert_layout,null);
warning.setView(view);
AlertDialog dialog = warning.create();
TextView alertTitle = (TextView) view.findViewById(R.id.setAlertTitle);
alertTitle.setText("Text");
TextView alertContent = (TextView) view.findViewById(R.id.setAlertContent);
alertContent.setText("Text");
Button confirm = (Button) view.findViewById(R.id.dialog_button_confirm);
confirm.setText(R.string.Alert_Confirm);
confirm.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
}
});
Button cancel = (Button) view.findViewById(R.id.dialog_button_cancel);
cancel.setVisibility(View.INVISIBLE);
dialog.setCancelable(true);
dialog.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
View.SYSTEM_UI_FLAG_FULLSCREEN |
View.SYSTEM_UI_FLAG_LAYOUT_STABLE|
View.SYSTEM_UI_FLAG_IMMERSIVE|
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION|
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN|
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
dialog.show();
you are almost there, you just need to clear the focus from alertdialog that is navigationbar stays hidden.
AlertDialog.Builder warning = new AlertDialog.Builder(my_context);
View view = ActivityTeetimeDetail.this.getLayoutInflater().inflate(my_alert_layout,null);
warning.setView(view);
AlertDialog dialog = warning.create();
TextView alertTitle = (TextView) view.findViewById(R.id.setAlertTitle);
alertTitle.setText("Text");
TextView alertContent = (TextView) view.findViewById(R.id.setAlertContent);
alertContent.setText("Text");
Button confirm = (Button) view.findViewById(R.id.dialog_button_confirm);
confirm.setText(R.string.Alert_Confirm);
confirm.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
}
});
Button cancel = (Button) view.findViewById(R.id.dialog_button_cancel);
cancel.setVisibility(View.INVISIBLE);
dialog.setCancelable(true);
// add this to your code.
dialog.getWindow().
setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
dialog.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
View.SYSTEM_UI_FLAG_FULLSCREEN |
View.SYSTEM_UI_FLAG_LAYOUT_STABLE|
View.SYSTEM_UI_FLAG_IMMERSIVE|
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION|
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN|
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
dialog.show();

Add a cancel button to a dialog

i have dialog with onclick button and include final Dialog dialog = new Dialog(context); how i can add cancel button to my dialog for hide dialog
and my content layout is different from activity main layout
sorry for my language
my code :
public void showd(View view){
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom);
dialog.setCanceledOnTouchOutside(false);
dialog.show();
}
You should use a DialogBuilder instead of creating the Dialog yourself. You then can create a cancel button using the following code
// Create your dialog builder
AlertDialog.Builder builder = new AlertDialog.Builder(context);
// Apply your custom view
builder.setView(R.layout.custom);
// A null clickListener will cancel the dialog
builder.setNegativeButton("Cancel" /*TODO Replace with string resources*/, null);
// Show the dialog
builder.show();

Add a popup dialog over the call screen

How do I add a popup dialog over the call screen? I'll be using the BroadcastReceiver to listen for incoming calls and show it. I need an idea about how to write an activity that allows a dialog over an incoming call. Also, how do I make the dialog movable to any part of the screen? I already have the BroadcastRceiver implemented and performing other functions, so I could just use an intent and start the activity from this BroadcastRceiver
start an activity, then use AlertDialog Builder from that activity to prompt a dialog
set custom view to customize the dialog appearence
Try this
AlertDialog.Builder builder = new AlertDialog.Builder(context.getApplicationContext());
LayoutInflater inflater = LayoutInflater.from(context);
View dialogView = inflater.inflate(R.layout.caller_dialog, null);
ImageView button = dialogView.findViewById(R.id.close_btn);
builder.setView(dialogView);
final AlertDialog alert = builder.create();
alert.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
alert.getWindow().setType(WindowManager.LayoutParams.TYPE_PHONE);
alert.setCanceledOnTouchOutside(true);
alert.show();
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
Window window = alert.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
window.setGravity(Gravity.TOP);
lp.copyFrom(window.getAttributes());
//This makes the dialog take up the full width
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
window.setAttributes(lp);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//close the service and remove the from from the window
alert.dismiss();
}
});

Showing custom view inside AlertDialog

I have a class that extends LinearLayout. It contains simply two EditBox. On some button click, i want to load this class inside the alert Dialog. When i click on button, the alert dialog is displayed but the view class that extends LinearLayout is not displayed.The code is as follows. I am stucked into this. Can I get some solution??
public class StudentDialog extends LinearLayout {
Context context;
LinearLayout layout;
public StudentDialog(Context context) {
super(context);
this.context = context;
createStudentDialog();
}
private void createStudentDialog() {
layout = new LinearLayout(context);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
layout.setPadding(10, 10, 10, 10);
layout.setId(200);
EditText studentName = new EditText(context);
studentName.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
studentName
.setTextAppearance(getContext(), TEXT_DIRECTION_FIRST_STRONG);
EditText address = new EditText(context);
address.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
address.setTextAppearance(getContext(), TEXT_DIRECTION_FIRST_STRONG);
layout.addView(studentName);
layout.addView(address);
}
}
//Now i am calling this on some button click listener as follows. The alert dialog is displayed but not the StudentDialog.
StudentDialog dialog = new StudentDialog(this);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setView(dialog);
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
It should work if you change
alertDialogBuilder.setView(dialog);
as
alertDialogBuilder.setView(dialog.layout);
Do you want to show two edit text in a alert view just like a login dialog with button, if yes then just create a layout which you want to show in dialog view. Create a class which extends Dialog and set this layout in setContentView of onCreate of this class

Center message in android dialog box

I want the message text within my dialog box to be center aligned.
Of course, you can always set the gravity of the original text view. This allows you to not have to worry about formatting and padding.
For example
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Title");
builder.setMessage("Message");
builder.setPositiveButton("OK", null);
AlertDialog dialog = builder.show();
// Must call show() prior to fetching text view
TextView messageView = (TextView)dialog.findViewById(android.R.id.message);
messageView.setGravity(Gravity.CENTER);
Create your own TextView object and then supply it to popup builder as View:
AlertDialog.Builder popupBuilder = new AlertDialog.Builder(this);
TextView myMsg = new TextView(this);
myMsg.setText("Central");
myMsg.setGravity(Gravity.CENTER_HORIZONTAL);
popupBuilder.setView(myMsg);
You can control all other text parameters (style, color, size ...). To control margins you may programatically create LinearLayout, set LayoutParams, and then put TextView into it.
Building off of Chase's answer, here's how to also center the title. I think this is the easiest way. Why android doesn't center by default or make it a simple constructor param is beyond me.
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("My Title");
builder.setMessage("My message");
builder.setPositiveButton("OK", listener);
AlertDialog dialog = builder.show();
// Must call show() prior to fetching views
TextView messageView = (TextView)dialog.findViewById(android.R.id.message);
messageView.setGravity(Gravity.CENTER);
TextView titleView = (TextView)dialog.findViewById(context.getResources().getIdentifier("alertTitle", "id", "android"));
if (titleView != null) {
titleView.setGravity(Gravity.CENTER);
}
we can use as like.
public static void showAlert(Activity activity, String message) {
TextView title = new TextView(activity);
title.setText("Your Title Here");
title.setPadding(10, 10, 10, 10);
title.setGravity(Gravity.CENTER);
title.setTextColor(Color.WHITE);
title.setTextSize(20);
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setCustomTitle(title);
builder.setMessage(message);
builder.setCancelable(false);
builder.setNegativeButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.show();
TextView messageText = (TextView)alert.findViewById(android.R.id.message);
messageText.setGravity(Gravity.CENTER);
messageText.setTextColor(Color.RED);
}
you can try this code
public void Info(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Info Aplication");
builder.setIcon(R.drawable.info_adp);
builder.setMessage("Jakarta Hospital");
builder.setCancelable(false);
builder.setPositiveButton("Exit", null);
AlertDialog dialog = builder.show();
TextView messageView = (TextView)dialog.findViewById(android.R.id.message);
messageView.setGravity(Gravity.CENTER);
}
without using textview.
worked for me.
styles.xml
<style name="CustomAlertDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:gravity">center</item>
</style>
Alertdialog
final AlertDialog builder = new AlertDialog.Builder(activity,
R.style.CustomAlertDialog)
try this in your TextView:
android:gravity = "center_vertical|center"

Categories