Search code examples
androidcustomdialog

Android Dialog like WeChat


enter image description here

Has anyone know how to create a dialogbox like the picture showing above?

  1. Rounded corner.
  2. Transparent background.
  3. No title, buttons, border.
  4. Fade-in -- delay for 5 seconds -- fade out.

*I have seen toast, popup window, dialog, alert dialog, which of these best suit the above? :)


It would be nice if some code snippet could be provided, I'm fairly new to android :)


Solution

  • For custom dialog check http://www.c-sharpcorner.com/UploadFile/2fd686/androd-dialogs/

     private void createCustomDialog(){
            //Create a dialog object
            final Dialog dialog = new Dialog(MainActivity.this);
            //Set its layout
            dialog.setContentView(R.layout.custom_dialog_layout);
            //Set the title
            dialog.setTitle("This is custom layout");
            //Make it cancelable
            dialog.setCancelable(true);
            //We need to dismiss the dialog so we add a listener to the ok button
            dialog.findViewById(R.id.okButton).setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    dialog.dismiss();
                }
            });
    
            dialog.show();
        }
    }
    

    For the dark alpha background you can create a drawable. Below code will give you a semi transparent background with round corners.

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android"  >
          <item>
               <shape android:shape="rectangle">
                    <gradient
                        android:startColor="#AA000000"
                        android:endColor="#AA000000"
                        android:angle="-90"
                        android:type="linear"
                        />
                    <corners android:radius="10dp" />
                </shape>
            </item>
    </layer-list>
    

    For the auto hide part you can use

    Animation anim = new AlphaAnimation(1,0);
    anim.setDuration(300);
    anim.setStartOffset(5000);
    anim.setInterpolator(new LinearInterpolator());
    anim.setFillAfter(false);
    
    myView.startAnimation(anim);