Saturday, February 13, 2010

Create a Custom Dialog

This article helps to creating a custom dialog box.

1. Create a Layout.xml for customDialog
Create a new layout which contains the view. in this example i have used edittext and button.


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

    <EditText android:id="@+id/EditText01" android:layout_height="wrap_content" android:text="Enter your name" android:layout_width="250dip"></EditText>

    <Button android:id="@+id/Button01" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:text="click"></Button>

</LinearLayout>



2. Create a Custom Dialog Class.
a. Create a class extends the dialog class
b. Create a Event Handler Interface as a member
c. Use the custom layout in onCreate Method.

public class MyCustomDialog extends Dialog {

    public interface ReadyListener {
        public void ready(String name);
    }

    private String name;
    private ReadyListener readyListener;
    EditText etName;

    public MyCustomDialog(Context context, String name,
            ReadyListener readyListener) {
        super(context);
        this.name = name;
        this.readyListener = readyListener;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mycustomdialog);
        setTitle("Enter your Name ");
        Button buttonOK = (Button) findViewById(R.id.Button01);
        buttonOK.setOnClickListener(new OKListener());
        etName = (EditText) findViewById(R.id.EditText01);
    }

    private class OKListener implements android.view.View.OnClickListener {
        @Override
        public void onClick(View v) {
            readyListener.ready(String.valueOf(etName.getText()));
            MyCustomDialog.this.dismiss();
        }
    }

}


3. Create a MainActivity and Implement the CustomDialog
public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        MyCustomDialog myDialog = new MyCustomDialog(this, "",
                new OnReadyListener());
        myDialog.show();
    }
    private class OnReadyListener implements MyCustomDialog.ReadyListener {
        @Override
        public void ready(String name) {
            Toast.makeText(MainActivity.this, name, Toast.LENGTH_LONG).show();
        }
    }
}



75 comments:

  1. Awesome, thanks for posting this code -- I had been struggling and looking at this helped me figure out my problems!

    ReplyDelete
    Replies
    1. Customize Android Search Dialog Using Library
      In this tutorial, we are going to learn how to implement and customize android search dialog in our android application. Search functionality is one of the major features most android applications have.
      http://www.tellmehow.co/customize-android-search-dialog/

      Delete
  2. Useful example. Shouldn't the Layout.xml file above be called mycustomdialog.xml instead?

    ReplyDelete
  3. Any ideas on why I cannot get my code to capture the events generated by my dialog's button?

    http://stackoverflow.com/questions/3914810/button-on-custom-dialog-not-responding-to-click-events

    Great example. Thanks

    ReplyDelete
  4. Thanks a lot for posting this code

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. Nice job

    Could you provide something more complicated?
    How to manage Title area?

    ReplyDelete
  7. http://hj-shared.orgfree.com/2011/01/android-part-i-customize-list-view/

    Shall we implement AutoComplete for this CustmaizedListView????

    ReplyDelete
  8. Great, i saw a site which is giving the full example for the custom dialog http://android-codes-examples.blogspot.com/2011/03/how-to-display-custom-dialog-and.html

    ReplyDelete
  9. Thank u so much. It's a good start for custom dialogs :)

    ReplyDelete
  10. thanks! nice codes, very OOP that makes it easy to miplement.

    ReplyDelete
  11. Put the screenshots it will be more clear

    ReplyDelete
  12. good one but how to set the edit text's text to my textview in main activity????

    ReplyDelete
  13. Android Dialog - Android Custom Dialog

    how to create Android Custom Dialg which is for user login, you may have any customized android layout.

    ReplyDelete
  14. Thanks for this helpful post! I compiled a list of some top resources for customizing dialog boxes in android. I included your tutorial. Check it out/ feel free to share. http://www.verious.com/board/Giancarlo-Leonio/customizing-dialog-boxes-in-android/ Hope this can be useful to other developers too. :)

    ReplyDelete
  15. The site was so nice, I found out about a lot of great things. I like the way you make your blog posts. Keep up the good work and may you gain success in the long run.
    Click here:
    angularjs training in tambaram
    Click here:
    angularjs6 Training in Chennai

    ReplyDelete
  16. Thank you for benefiting from time to focus on this kind of, I feel firmly about it and also really like comprehending far more with this particular subject matter. In case doable, when you get know-how, is it possible to thoughts modernizing your site together with far more details? It’s extremely useful to me.
    Click here:
    Microsoft azure training in chennai
    Click here:
    Microsoft azure training in online

    ReplyDelete
  17. All the points you described so beautiful. Every time i read your i blog and i am so surprised that how you can write so well.
    java training in chennai | java training in USA

    java training in indira nagar

    ReplyDelete
  18. I always enjoy reading quality articles by an individual who is obviously knowledgeable on their chosen subject. Ill be watching this post with much interest. Keep up the great work, I will be back

    Data Science course in kalyan nagar | Data Science course in OMR
    Data Science course in chennai | Data science course in velachery
    Data science course in jaya nagar | Data science training in tambaram

    ReplyDelete
  19. Very creativity blog!!! I learned a lot of new things from your post. It is really a good work and your post is the knowledgeable. Waiting for your more updates...
    Blue Prism Training Institute in Bangalore
    Blue Prism Course in Bangalore
    Blue Prism Training Bangalore
    Blue Prism Classes in Bangalore
    Blue Prism Course in Adyar
    Blue Prism Training in Mogappair

    ReplyDelete
  20. Thanks for the great article this is very useful info thanks for the wornderful post,
    custom software development company.

    ReplyDelete
  21. Nice post. By reading your blog, i get inspired and this provides some useful information. Thank you for posting this exclusive post for our vision. 
    Selenium training in Chennai

    ReplyDelete
  22. A very nice guide. I will definitely follow these tips. Thank you for sharing such detailed article. I am learning a lot from you.
    Java training in Chennai

    ReplyDelete
  23. Thanks for giving great kind of information. So useful and practical for me. Thanks for your excellent blog, nice work keep it up thanks for sharing.
    R Training Institute in Chennai | R Programming Training in Chennai

    ReplyDelete
  24. Software Development Company We specialize in Mobile App development, Web App development and all your customised online solutions. Get best impression at online by our services, we are familiar for cost effectiveness, quality, delivery and support.

    ReplyDelete
  25. This comment has been removed by the author.

    ReplyDelete
  26. I wanted to thank for sharing this article and I have bookmarked this page to check out new stuff.


    looking for digital marketing start learning today

    ReplyDelete
  27. As always your articles do inspire me. Every single detail you have posted was great. ExcelR Courses In Digital Marketing In Pune

    ReplyDelete


  28. I am reading your post from the beginning, it was so interesting to read & I feel thanks to you for posting such a good blog, keep updates regularly.i want to share about informatica mdm training and core java video tutorials

    ReplyDelete
  29. Thanks for sharing this informations.
    hadoop training in coimbatore

    C and C++ training in coimbatore

    embedded training institute in coimbatore

    RPA Course in coimbatore

    Robotic process automation training in coimbatore

    Blue prism training in coimbatore

    Ui Path training in coimbatore

    ReplyDelete
  30. Nice! you are sharing such helpful and easy to understandable blog. i have no words for say i just say thanks because it is helpful for me.

    Dot Net Training in Chennai | Dot Net Training in anna nagar | Dot Net Training in omr | Dot Net Training in porur | Dot Net Training in tambaram | Dot Net Training in velachery


    ReplyDelete
  31. Global Certified pro began from the need to develop a skilled pool of technology talent for the community such as microsoft azure training; keeping abreast of the constantly evolving IT landscape. Since, we have been recognized for our ability to be a life-long training partner and mentor for every individual. https://www.globalcertified.pro/certifications/microsoft-certification/

    ReplyDelete
  32. Global Certified pro began from the need to develop a skilled pool of technology talent for the community such as Microsoft Azure Certification keeping abreast of the constantly evolving IT landscape. Since, we have been recognized for our ability to be a life-long training partner and mentor for every individual. https://www.globalcertified.pro/certifications/microsoft-certification/

    ReplyDelete
  33. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
    DevOps Training in Chennai

    DevOps Course in Chennai


    ReplyDelete
  34. Infycle Technologies, the No.1 software training institute in Chennai offers the No.1 Selenium course in Chennai for tech professionals, freshers, and students at the best offers. In addition to the Selenium, other in-demand courses such as Python, Big Data, Oracle, Java, Python, Power BI, Digital Marketing, Cyber Security also will be trained with hands-on practical classes. After the completion of training, the trainees will be sent for placement interviews in the top companies. Call 7504633633 to get more info and a free demo.

    ReplyDelete
  35. The features of this software are listed below: Divide the files into different parts to accelerate downloads. IDM Full Version Download

    ReplyDelete