1

I asked another question about custom AlertDialog here.

Then I clicked my way to this custom AlertDialog (found here):

import android.app.AlertDialog;
import android.content.Context;
import android.webkit.WebView;

/**
 * Display a simple about dialog.
 */
public class AboutDialog extends AlertDialog {

    protected AboutDialog(Context context) {
        super(context);
        setContentView(R.layout.about_dialog);

        setTitle(R.string.about_title);
        setCancelable(true);

        WebView webView = (WebView) findViewById(R.id.webview);
        webView.loadData("Written by Cédric Beust (<a href=\"mailto:cedric@beust.com\">cedric@beust.com)", "text/html", "utf-8");
    }

}

I modified like this:

import android.app.AlertDialog;
import android.content.Context;
import android.webkit.WebView;

/**
 * Display a simple about dialog.
 */
public class AboutDialog extends AlertDialog {

    protected AboutDialog(Context context) {
        super(context);
        setTitle("Test");
        setCancelable(true);
        setContentView(R.layout.paus);
    }
}

and then tried to use it, like this:

AboutDialog ad = new AboutDialog(getApplicationContext());
ad.show();

But I get this error:

android.util.AndroidRuntimeException: requestFeature() must be called before adding content
    at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:181)
    at com.android.internal.app.AlertController.installContent(AlertController.java:206)
    at android.app.AlertDialog.onCreate(AlertDialog.java:251)
    at android.app.Dialog.dispatchOnCreate(Dialog.java:307)
    at android.app.Dialog.show(Dialog.java:225)
    at TestPackage.MainActivity$5.onClick(MainActivity.java:382)
    at android.view.View.performClick(View.java:2538)
  etc...

So I'd like to know why this is happening.

===================== EDIT ==========================0

As per suggestions below, I modified the code so it looks like this:

import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.webkit.WebView;

/**
 * Display a simple about dialog.
 */
public class AboutDialog extends AlertDialog {

    protected AboutDialog(Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.paus);

    }
}

But I get a BadTokenException instead.

Community
  • 1
  • 1
Ted
  • 19,727
  • 35
  • 96
  • 154

3 Answers3

0

setContentView() must be called before setTitle() ...

Harald Wilhelm
  • 6,656
  • 11
  • 67
  • 85
  • I tried the other way around too... same problem. Thats why I moved setTitle to above setContentView, cause Android complained... – Ted Oct 20 '11 at 10:08
  • Forget my answer. I didn't see that you were calling these methods in the constructor. Use the suggesttion of Android_Crazy ... – Harald Wilhelm Oct 20 '11 at 10:11
0

override on create method of altertdialog and call following methods in overridden method oncreate

 setContentView(R.layout.paus);

this will solve your problem.

Ashwin N Bhanushali
  • 3,872
  • 5
  • 39
  • 59
  • Secondly, that didnt work unfortunately. BadTokenException =( See my update above. – Ted Oct 20 '11 at 10:20
  • When you create instance of AboutDialog create it as follows. AboutDialog ald = new AboutDialog(Activity.this) Dont use getApplicationContext(); – Ashwin N Bhanushali Oct 20 '11 at 10:50
0

why don't you add a title (a text view) in R.layout.param and also you can add buttons and then everything will be shown

vallllll
  • 2,731
  • 6
  • 43
  • 77
  • My main point was: take an AlertDialog, and then just tweak some EditText behaviour. I did not want to create a completely custom thing, just take the AlertDialog (with its title, buttons etc), and then just add som OnTextChange listeners to its EditTexts (that I have in my layout file). I can do something like that if I use a normal AlertDialog.Builder, but then **all code ends up in my MainActivity** which is pretty horrible. – Ted Oct 20 '11 at 10:23