Reputation: 169
I have a class of a generic AlertDialog in a DialogFragment, which works fine. I use it for example to pop-up an "About" dialog.
Problem is the text in the dialog is not justified so it doesn't look very nice. Is there a way I can justify the text? Meaning, align it to both right and left sides?
This is the relevant function in my code:
@Override
public AlertDialog onCreateDialog(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Resources res = getActivity().getResources();
String title = getArguments().getString("title");
String message = getArguments().getString("message");
String yesButton = getArguments().getString("yes_button");
String noButton = getArguments().getString("no_button");
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), android.R.style.Theme_Material_Dialog_Alert);
if (!noButton.equals(""))
builder.setNegativeButton(noButton, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
mCancel.run();
}
});
builder.setIcon(R.mipmap.my_launcher)
.setTitle(title.equals("") ? res.getString(R.string.app_name) : title)
.setMessage(message)
.setPositiveButton(yesButton, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
mConfirm.run();
}
});
AlertDialog dialog = builder.create();
dialog.show();
// text MATCH_PARENT
TextView messageView = (TextView)dialog.findViewById(android.R.id.message);
LinearLayout.LayoutParams messageViewLL = (LinearLayout.LayoutParams) messageView.getLayoutParams();
messageViewLL.width = ViewGroup.LayoutParams.MATCH_PARENT;
// If only positive button, center it.
if (noButton.equals("")) {
Button positiveButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
LinearLayout.LayoutParams positiveButtonLL = (LinearLayout.LayoutParams) positiveButton.getLayoutParams();
positiveButtonLL.width = ViewGroup.LayoutParams.MATCH_PARENT;
}
return dialog;
}
Upvotes: 1
Views: 917
Reputation: 352
If you mean align title/message text, then instead of .setTitle(String)
use .setCustomTitle(View)
and instead of .setMessage(String)
use .setView(View)
, you can just align text or set padding:
TextView titleView = new TextView(context);
titleView.setText(title.equals("") ? res.getString(R.string.app_name) : title);
titleView.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_END);
TextView messageView = new TextView(context);
messageView.setText(message);
messageView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
messageView.setPadding(20, 20, 20, 20);
// your code
builder.setIcon(R.mipmap.my_launcher)
.setCustomTitle(titleView)
.setView(messageView)
.setPositiveButton(yesButton, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
mConfirm.run();
}
});
Upvotes: 3