Central aligning the default message text in AlertDialog in android.

By | January 30, 2012

Hello all..

Today I will show you how to align the message text to center in an AlertDialog.
By default it is left aligned, so we have to get a reference to it to align it to center.
This is how we do it.
Check this code.

package com.coderzheaven.pack;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.TextView;

public class AlertTestDemo extends Activity {
    public void onCreate(Bundle savedInstanceState) {

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("My Title");
        builder.setMessage("CoderzHeavennHeaven of all working codes.");
        builder.setPositiveButton("OK", null);
        AlertDialog dialog = builder.show();
        TextView messageText = (TextView)dialog.findViewById(android.R.id.message);

Please leave your valuable comments on this post.

Leave a Reply

Your email address will not be published. Required fields are marked *