Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm developing an Android application.

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

like image 883
VansFannel Avatar asked Oct 26 '10 16:10

VansFannel


People also ask

How do you give a dialog margin?

x yourDialog. getWindow(). setAttributes(params); Just add this before your show your dialog.

How do I customize my dialog box?

This example demonstrate about how to make custom dialog in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How do I change the dialog size in Android?

According to Android platform developer Dianne Hackborn in this discussion group post, Dialogs set their Window's top level layout width and height to WRAP_CONTENT . To make the Dialog bigger, you can set those parameters to MATCH_PARENT . Demo code: AlertDialog.

How many types of dialogue are there in Android?

There are three kinds of lists available with the AlertDialog APIs: A traditional single-choice list. A persistent single-choice list (radio buttons) A persistent multiple-choice list (checkboxes)


3 Answers

Another way that this can be done programatically is using the setCustomTitle():

// Creating the AlertDialog with a custom xml layout (you can still use the default Android version)
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.viewname, null);
builder.setView(view);

TextView title = new TextView(this);
// You Can Customise your Title here 
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);
like image 90
Raj Avatar answered Oct 28 '22 18:10

Raj


Just found this post while trying to figure out how to do the same thing. Here's how I did it for anyone else that finds this in the future.

Style xml is as follows:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <style name="PauseDialog" parent="@android:style/Theme.Dialog">
            <item name="android:windowTitleStyle">@style/PauseDialogTitle</item>
        </style>

        <style name="PauseDialogTitle" parent="@android:style/TextAppearance.DialogWindowTitle">
            <item name="android:gravity">center_horizontal</item>
        </style>
        <style name="DialogWindowTitle">
        <item name="android:maxLines">1</item>
        <item name="android:scrollHorizontally">true</item>
        <item name="android:textAppearance">@android:style/TextAppearance.DialogWindowTitle</item>
        </style>
    </resources>

And in my activities onCreateDialog method for the dialog I want styled I create the dialog like this:

Dialog pauseDialog = new Dialog(this, R.style.PauseDialog);
pauseDialog.setTitle(R.string.pause_menu_label);
pauseDialog.setContentView(R.layout.pause_menu);
like image 59
ChrisJD Avatar answered Oct 28 '22 17:10

ChrisJD


You can do it in code as well. Assume you have dialog fragment then add following lines of code.

@Override
public void onStart()
{
    super.onStart();

    TextView textView = (TextView) this.getDialog().findViewById(android.R.id.title);
    if(textView != null)
    {
        textView.setGravity(Gravity.CENTER);
    }
}
like image 8
Hesam Avatar answered Oct 28 '22 17:10

Hesam