Add Frame or Border to ImageView and Drop-Shadow - android

What I'm trying to do will work better with an example image. As you can see below I have a grey background, ontop of that sits a container with some padding containing an image. The container also has a slight dropshadow to it.
What I want to know, is if there's so non-painstaking way of doing this in my layout.xml? In a normal HTML document this would've been easy. But since this is for a mobile app and for a number of screen resolutions and so on, it's proving a bit difficult.
Any advice?
Edit: I eventually settled using a 9patch image. Everyting went really smooth in the creation of it but when I actually use it in my app I see these dark stripes on the right and bottom of the image. The dropshadow seems to work, it's a very light dropshadow.. but those darn stripes??

You can provide a border to a view by writing an xml file (say editBorder.xml) in drawable folder:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke
android:width="5dp"
android:color="#EBDDE2" />
<padding
android:bottom="2dp"
android:left="2dp"
android:right="2dp"
android:top="4dp" />
<gradient
android:centerColor="#color/white"
android:endColor="#color/white"
android:startColor="#color/white" />
<corners android:radius="8dp" />
</shape>
and to provide this border, use statement in ImageView as android:background="#drawable/editBorder"
This should solve your problem. :)

This can be done with proper padding and 9 patch image. See this link, may be it can help you.

ImageView having two property android:background and android:src
http://developer.android.com/reference/android/widget/ImageView.html
Create a blank white frame with drop shadow(Photoshop recommended).
So just do this
<ImageView android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/image"
android:background="#drawable/whiteFrame"
android:padding="10dp" >
</ImageView>

I found this imageView . It's may good for you

Related

How to have an ImageView with corners rounded and bordered while using setClipToOutline

following up on this answer, how can i achieve the same but including a border that runs smoothly around the clipped ImageView.
I have tried using this as a background drawable for the ImageView with red borders and imageView.setClipToOutline(true).
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#android:color/transparent" />
<corners
android:radius="35" />
<stroke
android:width="2dp"
android:color="#E42323" />
<padding
android:bottom="2dp"
android:left="2dp"
android:right="2dp"
android:top="2dp" />
</shape>
when not using an image as Android:src
it works fine.
but when i use Android:src the borders are clipped as well.
note: I know one can use a FrameLayout with ImageView as a child and set the background drawable to the FrameLayout but that's just asking for issues.
Ok, I don't think this is a solution you expected but as there was no easy solution I could find for the question instead came up with a funny workaround if API level >= 23, but since it works thought I'll share it incase it helps someone.
Basically, set the same drawable used as background and set it as
android:foreground="#drawable/background"
you still need to set background in order for setClipToOutline to clip the drawable properly, and foreground will create an overlapping border on the outside.
Even though foreground makes use of FrameLayout but since its for the same ImageView didn't find any issues with animations, they seem to work fine with it.

How to draw a rectangle around multiple views

I’d like to achieve the following look in my app to try and group the views into a more logical layout.
I was thinking of creating a Shape Drawable for the rectangle something along the lines of this:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#android:color/white"/>
<corners android:radius="12px"/>
<stroke android:width="2dip" android:color="#000000"/>
</shape>
But what I’m not sure about how to place the rectangle in the view. Most of the examples I’ve seen are placing a drawable as a background for a single view, not multiple views as per the above.
Do I need to create some sort of ‘blank’ view which holds no purpose but to house the rectangle? How would I go about this?
Everything is currently defined in a constraint layout.
Create a layout around the views and set its background as the drawable you created.
You will have to add this as a background to the Viewgroup where all these views are.
For eg. add this drawable as background to the relative layout.
Thanks for the comments all. Thought I'd update this with what I implemented as although the other answers were correct, I still didn't quite grasp what was required, so here's some additional information:
Create a file called 'rectangle.xml' in the drawable folder, with the following content:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#android:color/white"/>
<corners android:radius="12px"/>
</shape>
Then in the layout file in which I wanted to place the rectangle, I added the following view. The key bit that I didn't understand was how to specify the size/position of the rectangle. This was achieved by defining the following constraints to wrap it around the items I wanted.
<View
android:id="#+id/myRectangleView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="#id/unit_spinner"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="#id/time_label"
android:background="#drawable/rectangle"
android:elevation="2dp"
/>
Couple of other points I struggled with:
Even though I defined this view before all the views it surrounds,
the arrows on the spinners were displaying behind the rectangle. To fix this, I had to use the elevation property on the view, and set all the other views to be higher.
I struggled to get the position of
the rectangle where I wanted it using the margin/padding of the
rectangle view, so instead had to add padding to the views it was
surrounding. Not sure if this is the right approach - I'm still
toying with it.

How to glow effect behind the edit text like in the image?

I am trying to get a glow effect like the background for edittext
I tried doing it but the background is not as much effective as the image
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#68F30606"/>
<corners android:radius="10dp" />
</shape>
</item>
<item
android:left="1.7dp"
android:right="1.7dp"
android:top="1.7dp"
android:bottom="1.7dp">
<shape android:shape="rectangle">
<solid android:color="#android:color/white"/>
<corners android:radius="10dp" />
</shape>
</item>
</layer-list>
Can someone help me to solve this mystery?
Then you will need to use the old-technique(kind of) of 9-Patch Drawing. I used to do it too. Examples are scarce because they are big, but there is documentation.
Documentation: https://developer.android.com/studio/write/draw9patch.
Also, if this helps you can check this too: Outer glow in edittext
To use the 9-Patch images in Xml to this (remember this is after you have created the 9-Patch Images):
Reference the drawable with the name but don't include .9.png (auto-complete in eclipse will take care of this)
Make sure you only have 1 image under the main /drawable folder (not a version for each dpi folder)
The image must be specified using :background, not :src (this got me stuck for a while)
android:background="#drawable/splash_logo"
Make sure the image and layout that contains it are using:
android:layout_width="fill_parent"
android:layout_height="fill_parent"
Credit To: Creating & Using 9-patch images in Android
Also, check this website out, it contains a lot of useful examples that the documentation doesn't provide:
https://tekeye.uk/android/examples/ui/android-9-patch-image-files

Android Gridview No Spacing

Good Evening Everyone,
I have a gridview of images, but I want them flush against each other (without the border that appears when tapped).
I've set vertical and horizontal spacing to zero along with the padding. However, when tapped, the gridview "tap border" still shows up. How can I expand my images, or remove that so I no longer see it.
If you're still unclear, I can provide screenshots.
Thanks,
-Mitchell
Try to use nine-patch bitmap as selector.
Also make sure that bottom and right black lines (nine-patch) cover all sides.
In this case your pictures won't have offsets between each other
I think you should use
public void setSelector (int resID)
and set it to drawable that is fully transpert
the drawable you can define at xml file
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke android:width="0px" android:color="#android:color/transparent" />
<padding android:left="0dp" android:top="0dp" android:right="0dp"
android:bottom="0dp" />
<corners android:radius="0px"/>
</shape>

Shape corners with negative radius in Android

I want to create a composite view with corners that looks like this:
How it this possible in Android?
If someone have the same problem, best way is use 9.png drawables. In xml it's not possible.
You can achieve that with using <vector in a custom drawable file, here invert_shape.xml:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="200dp"
android:height="200dp"
android:viewportWidth="400"
android:viewportHeight="400">
<path
android:pathData="M3.146,256.500 L3.146,73.449 C41.770,72.388 72.757,41.041 72.757,2.500 L271.500,2.500 L271.500,256.500 L3.146,256.500 Z"
android:fillColor="#eeffcc"
android:strokeColor="#000000"
android:strokeWidth="5"/>
</vector>
After that you need to set this drawable as android:background of your LinearLayout in your activity_main.xml like this:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/linear_layout"
android:background="#drawable/invert_shape"
android:layout_centerInParent="true"
android:orientation="horizontal" />
The result:
You may asked yourself how to achieve the android:path in your <vector, I used a simple solution for that. You can use programs like Photoshop or Illustrator and create your shape there in a layer. If you have done that, right click on your layer and select "Copy SVG". Now you copied the shape, paste it in your drawable (here invert_shape.xml) in Android Studio and you will get parameters like android:path and android:stroke of your photoshop shape. Get rid of the unnecessary paramteres and make sure you use the right attributes in the XML itself. Now, there's no end for your creativity. Cheers! :)
You can able to give the OneSide Corner with set the below frame as background to the layout.
<?xml version="1.0" encoding="UTF-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="1dip"
/>
<solid
android:color="#3D2A1D"/>
<corners
android:radius="20sp"
android:topRightRadius="0dp"
android:bottomLeftRadius="0dp"
android:bottomRightRadius="0dp"/>
</shape>
I am not sure with the -ve border radios.

Categories

Resources