Centre message in AlertDialog box [duplicate] - java

This question already has answers here:
Center message in android dialog box
(7 answers)
Closed 4 years ago.
I have created a custom title for my AlertDialog box. But I have not been able to centre the message. It always comes left aligned.
TextView vehicle_no = (TextView) findViewById(R.id.vehicle_no);
String ScoreTitle = getResources().getString(R.string.AlertTitle);
AlertDialog.Builder alrtbuilder = new AlertDialog.Builder(this);
Context mContext = alrtbuilder.getContext();
LayoutInflater mLayoutInflater = LayoutInflater.from(mContext);
View mView = mLayoutInflater.inflate(R.layout.simple_dialog, null);
TextView mTextView = (TextView) mView.findViewById(R.id.title_text);
mTextView.setText(ScoreTitle);
The AlertDialog box has a positive and a negative button
alrtbuilder.setCustomTitle(mView);
alrtbuilder.setMessage(vehicle_no.getText().toString().toUpperCase()+" "+"?")
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
insert_timecard();
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
}).show;

Add android:gravity="center" to TextView with id R.id.title_text in your layout R.layout.simple_dialog.
<TextView
android:id="#+id/title_text"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="wrap_content" />
To center default message TextView you have to get the TextView from dialog after show().
alertDialog.show();
TextView messageView = (TextView)alertDialog.findViewById(android.R.id.message);
messageView.setGravity(Gravity.CENTER);
I do not know if its a good approach but you can also set the custom view to whole dialog then things can be handled easily.

Add:
mTextView.setGravity(Gravity.CENTER_HORIZONTAL);
TextView vehicle_no = (TextView) findViewById(R.id.vehicle_no);
String ScoreTitle = getResources().getString(R.string.AlertTitle);
AlertDialog.Builder alrtbuilder = new AlertDialog.Builder(this);
Context mContext = alrtbuilder.getContext();
LayoutInflater mLayoutInflater = LayoutInflater.from(mContext);
View mView = mLayoutInflater.inflate(R.layout.simple_dialog, null);
TextView mTextView = (TextView) mView.findViewById(R.id.title_text);
mTextView.setGravity(Gravity.CENTER_HORIZONTAL);
mTextView.setText(ScoreTitle);
This should resolve:

how about adding this?
TextView messageView = (TextView)dialog.findViewById(android.R.id.message);
messageView.setGravity(Gravity.CENTER);

Related

Alertdialog setView() using existing xml widget [duplicate]

This question already has answers here:
How to implement a custom AlertDialog View
(11 answers)
Closed 7 years ago.
I have an EditText in a .xml file with LinearLayout and want to add it as the setView() parameter on an AlertDialog. Is this possible? Here's what I've tried:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final EditText input = new EditText(this);
builder.setView(input);
But the dialog is blank when launched. What am I doing wrong?
Use it like this
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.your_layout, null);
builder.setView(dialogView);
AlertDialog alertDialog = builder.create();
alertDialog.show();
using dialogView you can get your EditText like this
EditText editText = (EditText) dialogView.findViewById(R.id.your_edit_text);
It might help
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
builder.setView(inflater.inflate(R.layout.YourLayout, null));
AlertDialog ad = builder.create();

how to get text from edittext inside a dialog [duplicate]

This question already has answers here:
Android Alert Dialog unable to find view
(4 answers)
Android can't get EditText getText().toString() in a Dialog
(1 answer)
Closed 8 years ago.
Like the title said how can I get text out of EditText which is found inside a custom dialog?
This is the onCreateDialog method from my dialogfragment:
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
final SharedPreferences.Editor editor = sharedPref.edit();
builder.setView(inflater.inflate(R.layout.name_dialog, null))
.setPositiveButton(R.string.name_ok, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
EditText nameEditText = (EditText) getActivity().findViewById(R.id.name_text_view);
editor.putString(getString(R.string.name_key), nameEditText.getText().toString());
editor.commit();
}
})
.setNegativeButton(R.string.name_cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
NameDialogFragment.this.getDialog().cancel();
}
});
return builder.create();
}
I can't get it to work.. I write something in the edittext then i press OK and it just crashes and gives me a null pointer error on this line :
editor.putString(getString(R.string.name_key), nameEditText.getText().toString());
You are looking for the EditText at the wrong View. Its not part of the Activity, its part of the dialog. So check the dialog for the view:
Dialog dialogView = dialog.getDialog();
EditText paymentEt = (EditText) dialogView.findViewById(R.id.edittext_payment);
Instead of using
EditText nameEditText = (EditText) getActivity().findViewById(R.id.name_text_view);
try using this
Dialog dialg = (Dialog) dialog;
EditText et = (EditText) dialg.findViewById(R.id.search_input_text);
val = et.getText().toString();

Cannot set EditText? Issue From dialog in a fragment - Strange - Android Java

I've come across an issue which I've never had before. I have a Fragment containing a button. This button (ViewBookingButton) shows a popup dialog. When the dialog opens I would like to set a string in an EditText (AllBooking). Unfortunately it crashes and shows NullPointerExecption. What is going wrong? Here is my code:
ViewBookingButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
AlertDialog.Builder ViewBookingHelpBuilder = new AlertDialog.Builder(getActivity());
ViewBookingHelpBuilder.setTitle("view booking:");
LayoutInflater inflater = getActivity().getLayoutInflater();
View DialogLayout = inflater.inflate(R.layout.view_booking, null);
ViewBookingHelpBuilder.setView(DialogLayout);
TextView AllBooking = (TextView)view.findViewById(R.id.AllBooking);
AllBooking.setText("Hello World");//WHEN I REMOVE THIS THE DIALOG WORKS
ViewBookingHelpBuilder.setPositiveButton("Done", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which)
{
}
});
AlertDialog helpDialog = ViewBookingHelpBuilder.create();
helpDialog.show();
}
});
Change this
TextView AllBooking = (TextView)view.findViewById(R.id.AllBooking);
to
TextView AllBooking = (TextView)DialogLayout.findViewById(R.id.AllBooking);
TextView belongs to the inflated layout and `findViewById looks for the view in the current view hierarchy.

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

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