Henry Zhu
Henry Zhu

Reputation: 2618

Android: Cannot set TextView for Title

I am using the technique brought up in previous StackOverflow questions, which suggest using TextViews and setting properties in them as the parameter for setTitle in an AlertDialog. I need this so I can style the font of the title.

The problem is that Android Studio says that it

cannot resolve method android.widget.TextView

Below is my code:

  TextView settingsTitle =  new TextView(this);
    settingsTitle.setText("Settings");
    settingsTitle.setTypeface(Typeface.createFromFile("monospace"));

    new AlertDialog.Builder(this)
            .setTitle(settingsTitle)
            .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // continue with delete
                }
            })
            .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // do nothing
                }
            })
            .setIcon(android.R.drawable.ic_dialog_alert)
            .show();

Upvotes: 1

Views: 113

Answers (1)

Lal
Lal

Reputation: 14810

Replace your code as

new AlertDialog.Builder(this)
            .setTitle(settingsTitle.getText().toString())
            .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // continue with delete
                }
            })

as there is no such method called setTitle() which has an argument of type TextView.

Thus use, settingsTitle.getText().toString() which gets the String in the TextView and use that to set the title like

setTitle(settingsTitle.getText().toString())

Read more about it in the docs

Upvotes: 1

Related Questions