Reputation: 454
I'm trying to add a center gravity to the message of a dialog fragment without having a separate XML file for a custom layout. I've tried getting the TextView and setting the gravity of that, but it doesn't seem to do anything:
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Replace current sound");
builder.setMessage("Choose a mp3 or ogg file or restore the default sound.\nThe current sound is:\n"+currentSound)
.setPositiveButton("Choose new sound", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// This is the Choose File dialog (uses aFileDialog library)
// to get the file replacement
FileChooserDialog dialog1 = new FileChooserDialog(context);
dialog1.show();
dialog1.addListener(new OnFileSelectedListener() {
@Override
public void onFileSelected(Dialog source, File file) {
// TODO Auto-generated method stub
source.hide();
Log.e("App Debugging", file.getAbsolutePath());
String fileReplacement = file.getAbsolutePath();
}
@Override
public void onFileSelected(Dialog source, File folder,
String name) {
// TODO Auto-generated method stub
}
});
}
})
.setNegativeButton("Restore default", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
AlertDialog dialog = builder.show();
TextView messageText = TextView)dialog.findViewById(android.R.id.message);
messageText.setGravity(Gravity.CENTER);
// Create the AlertDialog object and return it
return builder.create();
}
EDIT: Here are my findings so far: I can get it to work if I add another builder.show();
call after setting the messageText
gravity, but that leads to another dialog popping up right after closing the first one.
Upvotes: 3
Views: 1624
Reputation: 11
To solve this problem...
EDIT: Here are my findings so far: I can get it to work if I add another builder.show();
call after setting the messageText
's gravity, but that leads to another dialog popping up right after closing the first one.
You only have to return your dialog
AlertDialog dialog = builder.show();
TextView messageText = (TextView)dialog.findViewById(android.R.id.message);
messageText.setGravity(Gravity.CENTER);
return dialog;
Upvotes: 1
Reputation: 2086
Could you try like this
AlertDialog dialog = builder.show();
TextView messageText = (TextView)dialog.findViewById(android.R.id.message);
FrameLayout.LayoutParams p = (FrameLayout.LayoutParams) messageText.getLayoutParams();
p.gravity = Gravity.CENTER;
messageText.setLayoutParams(p);
Upvotes: 1