Domanda

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 :)

È stato utile?

Soluzione

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

Altri suggerimenti

It is not a problem at all. Just create 9nth patch drawable with delays and fading and put it as bg for the dialog.

Try this

Create XML with with your desired Content and then set transparent image to that

I am providing you image, use this

and the

Declare field of type PopupWindow. PopupWindow popup;

inflate your layout here

 View v = inflatter.inflate(R.layout.yourlayout, null); 

set your layout to the popup window

v1.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
int height1 = v1.getMeasuredHeight();
popup= new PopupWindow(v, (int) (width * 0.8), height1, true);
 popup.showAtLocation(mainlayout, Gravity.CENTER, 0, 0);

mainlayout is your activity view group

this is the piece of code that I have used in my app.

Transparent Backgrouund

Example I have used something like this in my app

popup example

Custom Toast would do everything for you, just prepare your xml and set it to Toast, here is a sample:

public class CustomToast {

public CustomToast(Context ctx, CharSequence text) {

    LayoutInflater inflater = LayoutInflater.from(ctx);
    View layout = inflater.inflate(R.layout.toast_layout, null);

    TextView txt = (TextView) layout.findViewById(R.id.toastText);
    txt.setText(text);

    Toast myToast = new Toast(ctx.getApplicationContext());
    myToast.setGravity(Gravity.CENTER_VERTICAL, 0, 100);
    myToast.setDuration(Toast.LENGTH_SHORT);
    myToast.setView(layout);
    myToast.show();
    }

}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top