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();
        }
    }
}



28 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. thanks
    it really helped!!!!

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

    ReplyDelete
  4. 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
  5. Thanks a lot for posting this code

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

    ReplyDelete
  7. Nice job

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

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

    Shall we implement AutoComplete for this CustmaizedListView????

    ReplyDelete
  9. 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
  10. Thank u so much. It's a good start for custom dialogs :)

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

    ReplyDelete
  12. Put the screenshots it will be more clear

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

    ReplyDelete
  14. Android Dialog - Android Custom Dialog

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

    ReplyDelete
  15. 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
  16. This information is impressive; I am inspired with your post writing style & how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic.android development course fees in chennai | android app development training in chennai|

    ReplyDelete
  17. 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
  18. 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
  19. That was a great message in my carrier, and It's wonderful commands like mind relaxes with understand words of knowledge by information's.
    Blueprism training in tambaram

    Blueprism training in annanagar

    Blueprism training in velachery

    ReplyDelete
  20. Thanks you for sharing this unique useful information content with us. Really awesome work. keep on blogging
    Devops training in velachery
    Devops training in annanagar

    ReplyDelete