Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to center the title in the dialog?

Tags:

android

dialog

I have implemented the dialog in my app. But the title in the dialog by default in the left side. How can I make the dialog title in the center?

Here is my code

final Dialog dialog = new Dialog(getActivity());
dialog.setContentView(R.layout.contact_query);
dialog.setTitle("Query Form");
like image 360
Vivek Kumar Avatar asked Sep 03 '16 07:09

Vivek Kumar


2 Answers

You can try this:

// 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 183
Zeeshan Afzal Satti Avatar answered Oct 14 '22 03:10

Zeeshan Afzal Satti


*Try this one *

    Dialog dialog = new Dialog(this);

    dialog.setContentView(R.layout.yourlayout);

    TextView titleView = (TextView)dialog.findViewById(android.R.id.title);

    titleView.setGravity(Gravity.CENTER);

For more information http://kodecenter.com/article?id=2390ec63-63d7-4534-a76f-cc3b10497a2c

like image 26
AAli Avatar answered Oct 14 '22 02:10

AAli