Custom Dialog on Android: How to Center Its Title

Custom dialog on Android: How can I center its 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);

How to show custom dialog at Center Vertical

what my problem is. "As you see - Dialog appear with unexpected margin"

it is called Dialog title Use dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
to remove title from Dialog like below code

Try this

final Dialog dialog = new Dialog(MainActivity.this);    
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialogforgot_password);

Title not center vertically on AlertDialog

So I ended up solving this with a more custom XML layout that contains the message TextView instead of using .setMessage().

The XML ended up being:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="vertical">

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical"
android:padding="10dp">

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:src="@drawable/help"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="Help"
android:textSize="24sp"
android:textColor="#ff33b5e5"/>
</LinearLayout>

<View android:id="@+id/titleDivider"
android:layout_width="match_parent"
android:layout_height="2dip"
android:background="#ff33b5e5" />

<TextView
android:id="@+id/message"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:maxLines="16"
android:minLines="3"
android:scrollbars="vertical"
android:fadeScrollbars="false"
android:textSize="20sp"
android:padding="7dp"/>

</LinearLayout>

And the Java code ended up as:

public class HelpDialog extends DialogFragment{
// Bundle variables/args
final static String ARG_HELP = "help";

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.custom_help_title, null);

Bundle args = getArguments();

builder.setView(view);

TextView message = (TextView) view.findViewById(R.id.message);
message.setMovementMethod(ScrollingMovementMethod.getInstance());
message.setText(args.getCharSequence(ARG_HELP));

builder.setPositiveButton("Done", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dismiss();
}
});

return builder.create();
}
}

In order to get the scrolling for larger messages to function properly the solution from here was used.

This code gave me the following results:

Sample Image

Sample Image

How to center an image in an Android Alert Dialog Title?

You can not do this, but You can create Your own Dialog like this:

/values/themes.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

<style name="Dialog" parent="android:style/Theme.Dialog">
<item name="android:windowBackground">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
</style>

</resources>

/layout/custom_dialog.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dip"
android:text="wpisz tekst by wyszukać piosenkę"
android:textColor="#5E636D" />

<EditText
android:id="@+id/value_mini"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="40.0dip"
android:background="@drawable/input_text"
android:paddingLeft="10px" />

<Button
android:id="@+id/search_mini"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="80dip"
android:text="szukaj" />
</RelativeLayout>

in method:

            Dialog dialog = new Dialog(Start.this, R.style.Dialog);
dialog.setContentView(R.layout.custom_dialog);
Button button = (Button) dialog.findViewById(R.id.search_mini);
final EditText et = (EditText) dialog.findViewById(R.id.value_mini);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//do something
}
});
dialog.show();


Related Topics



Leave a reply



Submit