user3737467
user3737467

Reputation: 5

How to create a custom Dialog box

I need a Dialog box in my app that is diversified in it's appearance, I mean that for example for one time I need a dialog box with only on button and for the other time I need it to have 2 buttons. also I want some of it's features like it's image be differend depending on it's usage! Could any one suggests me How I can implement it? For my solution I've searched some web sites and I implemented the dialog box which was implemented in CustomDialogClass from the code in the link below, but it doesn't show the dialog box, Could any one help me how I can show it?

How to create a Custom Dialog box in android?

Upvotes: 0

Views: 120

Answers (4)

Dharmesh Gohil
Dharmesh Gohil

Reputation: 334

try this android dialog lib. very simple to use with few lines of code in your activity.

Pop.on(this)
    .with()
    .title(R.string.title) // if non needed skip this
    .layout(R.layout.custom_pop)
    .when(new Pop.Yah() { // if not needed skip this
        @Override
        public void clicked(DialogInterface dialog, View view) {
            Toast.makeText(getBaseContext(), "Yah button clicked", Toast.LENGTH_LONG).show();
        }
    })
    .when(new Pop.Nah() { // if not needed, skip this
        @Override
        public void clicked(DialogInterface dialog, View view) {
            Toast.makeText(getBaseContext(), "Nah button clicked", Toast.LENGTH_LONG).show();
        }
    }).show();

Upvotes: 1

Deepika kapila
Deepika kapila

Reputation: 59

    final Dialog d = new Dialog(DisplayUserData.this);
    d.requestWindowFeature(Window.FEATURE_NO_TITLE); //before
    d.setContentView(R.layout.edit_msg_dialog);

    ImageView ivCross = (ImageView) d.findViewById(R.id.ivCross_msg);
    final EditText et_Message = (EditText) d.findViewById(R.id.etMessage);
    TextView tv_OK = (TextView) d.findViewById(R.id.tvOkButton);
    TextView tv_SKIP = (TextView) d.findViewById(R.id.tvSkipButton);

    tv_OK.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(et_Message.getText().toString().length()>= 40){
                Toast.makeText(getApplicationContext(),"You Can not enter                                          more then 40 characters.", Toast.LENGTH_SHORT).show();
            }else if(et_Message.getText().toString().length()< 1){
                Toast.makeText(getApplicationContext(),"Please Enter something", Toast.LENGTH_SHORT).show();
            }else {
                d.dismiss();
                msgWritten = et_Message.getText().toString();
                submittData();
            }
        }
    });

    ivCross.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            d.cancel();
        }
    });

    tv_SKIP.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            d.cancel();
            submittData(); 
        }
    });
    d.show();
    d.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

    WindowManager.LayoutParams lp = d.getWindow().getAttributes();
    lp.dimAmount = 0.7f;
    d.getWindow().setAttributes(lp);

Upvotes: 0

Rod_Algonquin
Rod_Algonquin

Reputation: 26198

You can add different layout for each of the dialog. one layout for 1 button other is with buttons and images, etc.

example:

        final Dialog dialog = new Dialog(context);
        dialog.setContentView(R.layout.custom); //the layout of your custom dialog
        dialog.setTitle("Title...");
        dialog.show();

You can find more info here on how to implement the action within the dialog.

Upvotes: 0

Pararth
Pararth

Reputation: 8134

I think what you need is a Dialog, not a Dialog themed Activity. As when that happens, you would have to initiate the (new) activity every time you want to show the dialog.

Instead, you can use Dialog like:

// custom dialog
final Dialog dialog = new Dialog(context);
//specify to not display the default title/header to customize the whole layout
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
dialog.setContentView(R.layout.custom_layout_here);
// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText(message);

//from the following part- buttons can be managed for your custom layout
Button dialogButton = (Button) dialog.findViewById(R.id.buttonOk);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
//if you want another button, add it here, just like the one above
//play with the visibility of button(or components) to hide or show acc to your requirement

dialog.show();

Upvotes: 0

Related Questions