Friday, March 23, 2012

Central aligning the default message text in AlertDialog in android.


Central aligning the default message text in AlertDialog in android.

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.
?
Drag and copy the code
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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 {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
 
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("My Title");
        builder.setMessage("CoderzHeaven\nHeaven of all working codes.");
        builder.setPositiveButton("OK", null);
        AlertDialog dialog = builder.show();
        TextView messageText = (TextView)dialog.findViewById(android.R.id.message);
        messageText.setGravity(Gravity.CENTER);
    }
}

2 comments:

Raymond said...

Thanks, this is really helpful! =)

Unknown said...

thanks for visiting keep on visiting the blog.........