0

I have a WebView that I want to add my own back button to, The webview is one that has been set up specifically for viewing pdfs in app. I have added the button and it works but I am unable to resize it or move it around the screen, and as a result the pdf controls are underneath the button and unusable. The button layout parameters seem to be something along these lines (this is not in the code anywhere this is just what it appears to be )

<ImageButton
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
     />

Here is the activity

public class PDFview extends Activity {

    ImageButton im;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);  
        Intent intent = getIntent();
        String LinkTo = intent.getExtras().getString("link");
        WebView mWebView= new WebView(this);
        im = new ImageButton(this);
        im.setImageResource(R.drawable.back);
        im.setLayoutParams(new LayoutParams(100,100));
        im.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                exitPDF();

            }
        });
        im.setLeft(0);
        im.setTop(200);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.loadUrl("https://docs.google.com/gview?embedded=true&url="+LinkTo);
        mWebView.addView(im);
        setContentView(mWebView);
    }
Cob50nm
  • 911
  • 2
  • 9
  • 27

1 Answers1

0

I have a WebView that I want to add my own back button to

More accurately, you have an activity or fragment in which you want to have a WebView and a back button.

I am unable to resize it or move it around the screen

That's because WebView is not really designed to hold other widgets.

Create an XML layout resource that contains a RelativeLayout, which in turn holds onto your WebView and your ImageButton. Make sure that the ImageButton is the second child of the RelativeLayout (with the WebView being the first), so the ImageButton is higher on the Z-axis and floats over the WebView (since that appears to be what you want). Then, use appropriate RelativeLayout positioning rules to put the ImageButton where you want.

All that being said:

  • Consider not having a back button at all, but instead use Android's own BACK button, by overriding onBackPressed(). Use that while there is Web history available (or whatever you are doing with the button), then call super.onBackPressed() after there's nothing more inside your activity to go "back" to, so the user can exit the activity.

  • If that is unacceptable for whatever reason, consider putting your "back" functionality in an action bar. Having a widget float over top of the WebView means that something the user wants to read will be unreadable, because the button is in the way.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I tried that and got a null pointer when trying to call `getSettings()` on the webView. I can't do either of the alternatives as this is to be used in a commercial setting and we do not want the user to have access to anything other than the webview which is being pointed at one specific site – Cob50nm Aug 29 '13 at 12:08
  • @Cob50nm: "I tried that and got a null pointer when trying to call getSettings() on the webView" -- then you will need to fix whatever bug you have, such as failing to retrieve the `WebView` via `findViewById()`. "I can't do either of the alternatives" -- yes, you can. The action bar is as much a part of your app as is your `WebView` and `ImageButton`. And, unless you're rolling your own ROM mod (or are running this on an Android 4.x phone, where you can hide the navigation bar), the user will have the BACK button whether you like it or not. – CommonsWare Aug 29 '13 at 12:13