Custom dialog on Android: How can I center its title?

Viewed 76602

I'm developing an Android application.

How can I center the title for a custom dialog that I'm using?

14 Answers

Similar to @LandL Partners solution, but in Kotlin:

val builder = AlertDialog.Builder(this)
val inflater = this.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater

val view = inflater.inflate(R.layout.viewname, null)
builder.setView(view)
val title = TextView(this)
title.setText("Custom Centered Title")
title.setBackgroundColor(Color.DKGRAY)
title.setPadding(10, 10, 10, 10)
title.setGravity(Gravity.CENTER)
title.setTextColor(Color.WHITE)
title.setTextSize(20)

builder.setCustomTitle(title)
    AlertDialog alertDialog = new AlertDialog.Builder(activity)

            .setMessage(message)
            .create();
    alertDialog.setIcon(R.mipmap.ic_launcher_round);

    @SuppressLint("RestrictedApi")
    DialogTitle titleView=new DialogTitle(activity);
    titleView.setText(title);
    titleView.setPaddingRelative(32,32,32,0);
    alertDialog.setCustomTitle(titleView);

In Kotlin, you can do it in 1 line

dialog!!.window!!.attributes = dialog!!.window!!.attributes.apply { dimAmount = 0F }

Flutter | Center AlertDialog Title


There was no question nor way for me to add this answer... So this is where it lives now. Read the text at the end of the answer.

Flutter AlertDialog by default centers the text as long as it is shorter than the device width. Read the code below. I have included it in the onPressed braces for convenience. You can literally copy and paste ;)

You are welcome :)


onPressed: () {
  showDialog(
    useSafeArea: true,
    context: context,
    builder: (BuildContext context) {
      return AlertDialog(
        // ///////////////////////////////////////////////////////////////////////
        contentPadding: EdgeInsets.zero,
        // ///////////////////////////////////////////////////////////////////////
        title: Container(
          alignment: Alignment.center,
          width: double.infinity,
          child: Padding(
            padding: const EdgeInsets.only(
                bottom: 10),
            child: Column(
              mainAxisAlignment:
                  MainAxisAlignment.center,
              children: [
                Text("My Title"),
                Text("is looooong"),
                Text("so this is"),
                Text("how I center it"),
                Text("I can even add a"),
                Text("nice divider"),
                Divider(
                  thickness: 1,
                  color: Colors.black38,
                ),
              ],
            ),
          ),
        ),
        // ///////////////////////////////////////////////////////////////////////
        content: Column(
          children: [
            Text(
              "Your Content Goes Here",
            ),
          ],
        ),
        // ///////////////////////////////////////////////////////////////////////
        actions: [
          ElevatedButton(
            child: Text(
              'Cancel',
              style: TextStyle(
                color: Colors.white,
                fontSize: 16,
              ),
            ),
            onPressed: () {
              Navigator.of(context).pop();
            },
            style: ElevatedButton.styleFrom(
              backgroundColor: Colors.red,
            ),
          ),
          ElevatedButton(
            child: Text(
              'Submit',
              style: TextStyle(
                color: Colors.white,
                fontSize: 16,
              ),
            ),
            onPressed: () {
              // Do something here when you press the button
            },
            style: ElevatedButton.styleFrom(
              backgroundColor: Colors.blue,
            ),
          ),
        ],
        // ///////////////////////////////////////////////////////////////////////
      );
    },
  );
},

Please vote up. StackOverflow won't let me ask questions... Even though I have contributed lots!!!

It's a joke! And this place is supposed to be for learning... Haha. New people stay away. The community will smash you if you ask questions.

The only way you can build a profile on StackOverflow is if you create new accounts with multiple emails and then join them. There is no other way. Especially if you are new and learning.

Related