16

In my programme webview is load in separate layout when button click. that layout only have that web view. I want to add border for that. I add separate XML as follows to background for that webview but is not work.

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF" />
<stroke android:width="10dp" android:color="#000000" />
<padding android:left="2dp" android:top="2dp" android:right="2dp"android:bottom="2dp"/>
</shape>

how can I add a border for webview in Android..? thanks

Miuranga
  • 2,463
  • 10
  • 51
  • 81
  • 1
    Could refer to this [SO Answer](http://stackoverflow.com/questions/7074546/border-texture-for-a-view/7074764#7074764)? – Adil Soomro Aug 24 '11 at 09:54

3 Answers3

26

Enclose the WebView in a Layout, add the border to the Layout, and keep a padding of 2dp in the layout.

Abhinav Manchanda
  • 6,546
  • 3
  • 39
  • 46
6

Abhinav's answer is right on, I'm just adding this additional info for absolute beginners like myself who encounter this answer and don't even know yet how to "enclose the WebView in a Layout" or "add the border to the Layout"; hopefully it can help someone:

  1. Create a new directory under /res and name it drawable (you'll already have drawable-hdpi, drawable-mdpi, etc; these are for different resolutions--this new directory drawable will be used regardless of resolution).
  2. Create a new XML file under drawable and name it border.xml (in Android Studio, you can right-click on the drawable directory and click New>Drawable Resource File).
  3. Paste the entire contents of miuranga's XML into border.xml and save it. This is called a "Drawable Resource" and will be pulled into your layout file in the next step.
  4. In your layout file, create a new Layout around the WebView, as I've done as shown below with the child LinearLayout. Note that the LinearLayout retrieves the drawable resource with the attribute android:background set to @drawable/border. I believe it's retrieving border.xml by its filename minus the extension. By adding the border to the Layout that encloses the WebView, you're visually achieving a border around the Webview, which works nicely.

activity_main.xml contents:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context="com.example.test.app.MainActivity"
   android:orientation="vertical">
<LinearLayout android:background="@drawable/border"
                  android:layout_width="match_parent"
                  android:layout_height="380px">
    <WebView xmlns:android="http://schemas.android.com/apk/res/android"
                        android:id="@+id/webview"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent" />
     </LinearLayout>
</LinearLayout>
James Toomey
  • 5,635
  • 3
  • 37
  • 41
  • Thanks this worked great for me. Another thing to add: you can add a android:padding="5dp" to the root LinearLayout (the first one) to give the border some space from the edges of the screen. – J. Schei Mar 30 '17 at 02:42
4

WebView itself can't have Drawable as background - see WebView.onDraw in WebView.java. It has solid color only, default or taken from html content. Solution is (as already suggested) to make WebView as child of other Widget.

Pointer Null
  • 39,597
  • 13
  • 90
  • 111