Center message in android dialog box - java

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"

Related

Problem with blurred background on AlertDialog

I'm tried to blur background using this post:
https://stackoverflow.com/a/21278278/10837992.
It's work good but i have a little problem with the size of the dialog.
I'm created custom layout :
I'm changed the realization of the method.
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.alert_dialog_layout, null);
builder.setView(dialogView);
TextView title = dialogView.findViewById(R.id.Title);
TextView desc = dialogView.findViewById(R.id.Desc);
title.setText("Title");
desc.setText("Desc");
AlertDialog alert = builder.create();
Bitmap map = new BlurUtils().takeScreenShot(getActivity());
Bitmap fast = new BlurUtils().fastblur(map, 50);
final Drawable drawable = new BitmapDrawable(getResources(), fast);
alert.getWindow().setBackgroundDrawable(drawable);
alert.show();
While running this method, the size of the Dialog is cropped:
Screenshoot:
Ok , I find a solution:
Just added Theme to AlertDialog.Builder.
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(),android.R.style.Theme_DeviceDefault_Light_NoActionBar_Fullscreen);

I am creating a alert box in android studio

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();

How do I make my AlertDialog display its text in monospace font?

Here's where I display an AlertDialog:
public void showCenteredInfoDialog(TextView _title, TextView _message) {
_title.setGravity(Gravity.CENTER);
this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setPositiveButton("OK", null);
builder.setCustomTitle(_title);
builder.setMessage(_message.getText());
AlertDialog dialog= builder.show();
TextView messageView = (TextView) dialog.findViewById(android.R.id.message);
messageView.setGravity(Gravity.CENTER);
}
I'd like the font to be monospace.
Do I need to modify an xml file or can Java alone handle this? (If Java CAN handle it, it would probably take API 21, yes?)
I wonder if adding messageView.setFontFeatureSettings("..."); or messageView.setTypeface(...) with some way to refer to monospace would work. I can't find syntax for the String argument for setFontFeatureSettings.
This should work for your problem. Add it in the TextView part:
TextView messageView = (TextView) dialog.findViewById(android.R.id.message);
messageView.setTypeface(Typeface.MONOSPACE);
messageView.setGravity(Gravity.CENTER);
Also is possible to change the font in XML, but i think this it's more confortable.
Hope this will help you! :)
Here's a nice look at what #rpfc made for me (and it doesn't require API 21):
Method to show AlertDialog with a TextView _title and _message in MONOSPACE font:
public void showCenteredInfoDialog(TextView _title, TextView _message) {
_title.setGravity(Gravity.CENTER);
this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setPositiveButton("OK", null)
.setCustomTitle(_title)
.setMessage(_message.getText());
AlertDialog dialog= builder.show();
TextView messageView = (TextView) dialog.findViewById(android.R.id.message);
messageView.setTypeface(Typeface.MONOSPACE);
messageView.setGravity(Gravity.CENTER);
}

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();
}
});

Android: put 2 buttons in custom dialog to appear in center

I have a custom dialog.
I want to put 2 buttons one near the other horizontally, to appear in center.
How can I achieve that?
Very similiar to Yes/No message box.
I am seeking a way to do it in layout xml file.
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:orientation="horizontal"
android:width="wrap_content"
android:height="wrap_content"
android:layout_centerHorizontal="true">
... And two buttons here
From Android Dev
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
MyActivity.this.finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
You may be looking for something similar to this. You can set the "Positive" and "Negative" button to say whatever you want and have each one do its own thing. In your case (and most cases) Yes and No would be perfect here!
Some examples of xml files can also be found by following that link as well as this one that I have used before.
Blog about custom dialogs. (Nice Example Code)
You can use an xml to define the layout of the content of the Dialog anyway you want, then you use an layoutInflater to set as a view in an AlertDialog:
AlertDialog.Builder builder;
AlertDialog alertDialog;
Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_dialog, (ViewGroup) findViewById(R.id.layout_root));
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Hello, this is a custom dialog!");
ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.android);
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
alertDialog = builder.create();
Documentation source: http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog

Categories