0

I want to play gif in alertdialog.

I write this code:

AlertDialog.Builder builder = new AlertDialog.Builder(myThis);
             builder.setCancelable(false);
             WebView view = new WebView(myThis); 
             view.loadUrl("file:///android_asset/gif_pokemon.gif");
             LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                     LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); 
             params.gravity=Gravity.CENTER;
             view.setLayoutParams(params);
             builder.setView(view);
             dlg_p=builder.create();
             WindowManager.LayoutParams wlmp = dlg_p.getWindow().getAttributes();       
             wlmp.gravity = Gravity.CENTER ; 
             dlg_p.show();

but the problem is gif is not places in center. Its gravity is left.

How can I set it to center?

CompEng
  • 7,161
  • 16
  • 68
  • 122

1 Answers1

0

Wrap the WebView using LinearLayout,

  • the LayoutParams of the LinearLayout is set to match_parent for the width and wrap_content for the height, if needed fully center then use match_parent for the height too.

  • set the LinearLayout gravity to center, you can also use centerVertical or centerHorizontal.

Hope it helps :)

AlertDialog.Builder builder = new AlertDialog.Builder(myThis); builder.setCancelable(false);

    LinearLayout linearLayout = new LinearLayout(myThis);
    linearLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    linearLayout.setGravity(Gravity.CENTER);

    WebView view = new WebView(myThis);
    view.loadUrl("file:///android_asset/gif_pokemon.gif");
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    params.gravity=Gravity.CENTER;
    view.setLayoutParams(params);

    linearLayout.addView(view);
    builder.setView(linearLayout);
    dlg_p=builder.create();
    dlg_p.show();
Ben Levi
  • 177
  • 1
  • 5