[ACCEPTED]-How to add title to the custom Dialog?-dialog

Accepted answer
Score: 17

Using some of your snippet:

public void customDialog() {
    Dialog dialog=new Dialog(this);
    dialog.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    dialog.setContentView(R.layout.dialog_submit);
    dialog.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);
    dialog.show();
}

res/layout/custom_title.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="This is a custom title"/>

0

Score: 3

Did you try?

dialog.setTitle(R.string.app_name);

0

Score: 3

Using your layout definition and this piece 3 of code:

public void customDialog()
{
    Dialog dialog = new Dialog( this );
    dialog.setContentView( R.layout.dialog_submit );
    TextView edit_model = (TextView) dialog.findViewById( R.id.edit_model );
    edit_model.setText( android.os.Build.DEVICE );
    dialog.setTitle( "Enter Details" );
    dialog.show( );
}


I get this dialog:

enter image description here


So, you might 2 want to try dialog.setTitle( "Enter Details" ) again.
I 1 used the emulator running Android 2.1.

Score: 0

What you might try, if you still need an 31 answer to this, is an AlertDialog.Builder Object. In this Object 30 you can also call the setMessage("Title") method, which will 29 set the title in the Dialog you eventually create 28 with this. Moreover, you can also specify 27 a positiveButton, neutralButton and a negativeButton (let's say "Add", "Ok" and "Cancel" in 26 that order, though you can specify your 25 own text).

I believe the problem lies in 24 that when you call Dialog dialog = new Dialog(this) the onCreateDialog(int id) is called. But 23 here's the catch: this method is called 22 once and delivers a Dialog which is reused whenever 21 you need a new Dialog. The Dialog however, can't be 20 edited any more (as far as I know). Well 19 maybe with the onPrepareDialog(int id, Dialog dialog) method, but I am still trying 18 to get that working myself. My point is, that 17 after creation, you can't edit the User 16 Interface on the Dialog anymore. So the 15 way that does work is by Overriding the 14 onCreateDialog(int id) in your code, creating an AlertDialog.Builder (or whatever 13 you are basing your Dialog on: ProgressDialog/AlertDialog/etc.) and setting 12 the title, layout and buttons here. After 11 this, you can call the create() method, which will 10 actually build the Dialog with your settings.

@Override
public dialog onCreateDialog(int id){
    // Create the View to use in the Dialog.
    LayoutInflater inflater = getLayoutInflater();
    // Inflate the View you want to set as the layout.
    final View layout = inflater.inflate(R.layout.your_dialog_layout,
                                         (ViewGroup) findViewById(R.id.your_parent_view);
    // Create Dialog Builder Object to create Dialog from.
    AlertDialog.Builder adBuilder = new AlertDialog.Builder(this);
    // Set the title to use.
    adBuilder.setMessage("Your title");
    // Add only a positive button.
    adBuilder.setPositiveButton("Add", new DialogInterface.OnClickListener(){
        @Override
        public void onClick(DialogInterface dialog, int which){
            // Handle click on positive button here.
        }
    };
    // Set the layout which you want to use (inflated at the beginning).
    adBuilder.setLayout(layout);
    // After you've set all the options you want to set, call this method.
    AlertDialog dialog = adBuilder.create();
    return dialog;
}

This 9 will create a Dialog with the title set to "Your 8 Title", which uses the layout you specified, and 7 has one button with the text "Add". Note 6 that the main difference between positive, neutral 5 and negative buttons is that their layout 4 on the Dialog is changed accordingly (positive 3 = left, neutral = middle and negative = right).

For 2 more information I would advise you to see 1 the documentation about this.

Score: 0

Why not use an AlertDialog if you have 3 or less buttons?

My 2 AlertDialog looks like this:

enter image description here

My java code:

LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.add_filter, null);

AlertDialog alertDialog = new AlertDialog.Builder(this)
        .create();
alertDialog.setTitle("AlertDialog title");
alertDialog.setMessage("AlertDialog message");
alertDialog.setView(view);
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "OK",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,
                    int which) {
                dialog.dismiss();
            }
        });
alertDialog.show();

My 1 XML:

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

    <Spinner
        android:id="@+id/spinner_filter"
        android:layout_width="wrap_content"
        android:spinnerMode="dropdown"
        android:layout_height="wrap_content"
        android:entries="@array/filter_array"
        android:prompt="@string/filter_prompt" />

</LinearLayout>

Simple yet does what you need.

Score: 0

This question is old, but my solution is 5 using an relative layout within the main 4 relative layout. That way you can create 3 your own title. It doesn't see a top TextView 2 as a title if you use it that way:

  <?xml version="1.0" encoding="utf-8"?>
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical" 
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:padding="10dp"
      >
 <RelativeLayout
      android:id="@+id/layout_root"
      android:orientation="vertical" 
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      >
 <TextView android:id="@+id/txt_name"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:textColor="#FFF"
      android:text="Name"
      android:textStyle="bold"
      />
 <EditText android:id="@+id/edit_name"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_below="@+id/txt_name"
      />
<TextView android:id="@+id/txt_model"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:textColor="#FFF"
      android:layout_below="@+id/edit_name"
      android:text="Phone Model"
      />
 <TextView android:id="@+id/edit_model"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_below="@+id/txt_model"
      />

 <Button android:id="@+id/but_cancel"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_below="@+id/edit_model"
      android:text="Cancel"     
      />
 <Button android:id="@+id/but_submit"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_below="@+id/edit_model"
      android:layout_toRightOf="@+id/but_cancel"    
      android:text="Submit"     
      />   
 </RelativeLayout>                    
 </RelativeLayout>

This seems 1 to be the easiest way.

Score: 0

Please use this line in to hide built-in 2 title from dialog

dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

and 1 add textView in your layout file.

More Related questions