I\'m developing an Android application.
How can I center the title for a custom dialog that I\'m using?
For your custom DialogFragment you can do this:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Dialog dialog = super.onCreateDialog(savedInstanceState);
final TextView textView = (TextView) dialog.findViewById(android.R.id.title);
if(textView != null) {
textView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
}
return dialog;
}
You've got some starting tips here for modifying the title of a dialog: Android - change custom title view at run time Don't know if it can be centered(haven't tried), but if it's a custom View I guess it's very possible.
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);
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)
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);
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);