1

I want to align image in a webview to the centre of a dialog.

Relevant code:

void showImageInLargeMode(String link)
    {
        LayoutInflater inflater=ThirtyArticleDetail.this.getLayoutInflater();
        View layout=inflater.inflate(R.layout.showenlargeimagedialog,null);
        final Dialog d1 = new Dialog(ThirtyArticleDetail.this);
        d1.requestWindowFeature(Window.FEATURE_NO_TITLE);
        d1.setContentView(layout);
        d1.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        wvdisplayimage=(WebView)layout.findViewById(R.id.wvdisplayimage);
        wvdisplayimage.getSettings().setJavaScriptEnabled(true);
        wvdisplayimage.setBackgroundColor(Color.parseColor("#f0ece9"));
        wvdisplayimage.getSettings().setBuiltInZoomControls(true);
        wvdisplayimage.loadDataWithBaseURL(null, "<style>img{display: inline;height: auto;max-width: 100%;}</style><img src='" + link + "'/>", "text/html", "UTF-8", null);
        //wvdisplayimage.loadDataWithBaseURL(null, "<img src='" + link + "'/>", "text/html", "UTF-8", null);

        //wvdisplayimage.loadData("<html><head><style type='text/css'>body{margin:auto auto;text-align:center;} img{width:100%25;} </style></head><body><img src=''" + link + "'/></body></html>" ,"text/html",  "UTF-8");


        /*String html = "<html><body><img src=\"" + link + "\" width=\"100%\" \"/></body></html>";
        wvdisplayimage.loadData(html, "text/html", null);*/



        d1.show();


        btncanceldialog=(Button)layout.findViewById(R.id.btncancelredicon);
        btncanceldialog.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                d1.dismiss();

            }
        });

    }

showenlargeimagedialog.xml

<LinearLayout
    android:id="@+id/llshowmoredialog"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="horizontal"
    android:padding="2dp" >

    <!-- Contents will go here.. -->

    <WebView
        android:id="@+id/wvdisplayimage"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center" >
    </WebView>
</LinearLayout>

<Button
    android:id="@+id/btncancelredicon"
    android:layout_width="25dp"
    android:layout_height="25dp"
    android:layout_alignParentRight="true"
    android:layout_margin="5dp"
    android:background="@drawable/round_button_background"
    android:gravity="center_vertical|center_horizontal"
    android:text="X"
    android:textColor="#FFF"
    android:textSize="12sp"
    android:textStyle="bold" />

Output:

enter image description here

As you can see the image in webview is diaplaying at the top of the dialog.How to move the image in centre??

I have tried many other solutions in SO,but none of them seems working. Any Idea?

kgandroid
  • 5,507
  • 5
  • 39
  • 69

2 Answers2

1

Try using wrap_content instead match_parent in your WebView layout

mr.icetea
  • 2,607
  • 3
  • 24
  • 42
  • Thank you,it worked,but when i zoom the image,it is not expanding,any idea? – kgandroid Jul 23 '15 at 09:11
  • @kgandroid ok try another way, you can keep using `match_parent` in your `WebView` layout, but using `gravity="center"` instead `layout_gravity="center"` – mr.icetea Jul 23 '15 at 09:25
  • sorry for late reply.Tried it,but not working :( Image is aligned at the top – kgandroid Jul 23 '15 at 11:03
  • @kgandroid you can try this solution http://stackoverflow.com/questions/16783146/android-center-and-adjust-image-in-webview. – mr.icetea Jul 23 '15 at 14:48
  • @kgandroid i think you can simple align your image by using html attribute. So the image will stand at center of your page. – mr.icetea Jul 23 '15 at 14:50
  • yes i Have tried that way also...but unfortunately the I didnt get the result :( – kgandroid Jul 23 '15 at 16:16
0

Pass this style android.R.style.Theme_Black_NoTitleBar_Fullscreen Dialog constructor :

 final Dialog d1 = new Dialog(ThirtyArticleDetail.this,android.R.style.Theme_Black_NoTitleBar_Fullscreen);
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67