Android: Custom dialogfragment content and buttons

Source: Internet
Author: User

Little problem, Record ~

Android4.0 later began to recommend the use of Dialogfragment instead of dialog. Two examples are given in the official Android documentation:

    • OneBasic Dialog
      An example of how to customize the window Content-Override onCreateView method.
    • OneAlert Dialog
      An example of how to customize the pop-up button-override onCreateDialog method.
Okay, so here's the problem.

In practice, it is often necessary to customize both the window content and the custom button.
At this time if we retrace, Dialogfragment Oncreateview and Oncreatedialog methods are rewritten, will find--bong! Exceptions such as "androidruntimeexception:requestfeature () must be called before adding content" appear.

The reason for this anomaly can be seen: StackOverflow's answer to this question is freerider and the comments below.
Excerpt: "You can override both (in fact the Dialogfragment says so), the problem comes when you try to inflate the view afte R has already creating the dialog view. You can still does other things in Oncreateview, like use the savedinstancestate, without causing the exception.

Then there is the solution:

Since two methods cannot be overridden at the same time, choose one to rewrite:
Overriding the Oncreatedialog method, you can customize the button by referencing the official example, and then modify it so that it can be customized view-- create() before Alertdialog.builder creates dialog. Use the Alertdialog.builder setView(view) method, where view is a custom view.

To feel the difference ~
Here's an example for Google:

@Overridepublic Dialog Oncreatedialog (Bundle savedinstancestate) {    int title = Getarguments (). GetInt ("title");    return new Alertdialog.builder (Getactivity ())            . SetIcon (R.drawable.alert_dialog_icon)            . Settitle (title)            . Setpositivebutton (R.STRING.ALERT_DIALOG_OK, ... )            . Setnegativebutton (R.string.alert_dialog_cancel, ... )            . Create ();}     


This is after the change:

@Overridepublic Dialog Oncreatedialog (Bundle savedinstancestate) {    //inflater instantiates View v in the preceding constructor    = Inflater.inflate (r.layout.dialog,null);    Imagegridview = (GridView) V.findviewbyid (r.id.gridviewimage);    Custom view,bla~bla~    Alertdialog.builder Builder = new Alertdialog.builder (getactivity ());    return Builder.setview (v)            . SetIcon (Android. R.drawable.ic_dialog_alert)            . Settitle (title).            setcancelable (False)            . Setpositivebutton ( R.STRING.ALERT_DIALOG_OK, ... )            . Setnegativebutton (R.string.alert_dialog_cancel, ... )            . Create ();}


Also at:http://www.barryzhang.com/archives/396

Android: Custom dialogfragment content and buttons

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.