How to Display Border to Imageview

Border for an Image view in Android?

I set the below xml to the background of the Image View as Drawable. It works.

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

And then add android:background="@drawable/yourXmlFileName" to your ImageView

Android - Set a border around an ImageView

If you put the padding=1 and the background color in the LinearLayout, you'll have a 1px yellow border.

How To Display Border To Imageview?

You can create a resource (layer drawable xml) for your ImageView's "border" (actually background), and declare in your theme that the ImageView's background resource is the drawable xml.

If you need this "border" to be changed based on the ImageView's state (focused, selected, etc.), then you should create more layer drawables, and put them together into a selector xml (state drawable).

Then in your theme you should set the ImageView's background to be this selector.xml.

Update

Below is a sample of how to specify a simple border to your images, that will result in

efteling with border

You have to

  1. create a new layer drawable file (image_border.xml),
  2. modify/create your styles.xml file
  3. modify/create your colors.xml file
  4. modify your layout xml file (or your code) to apply the style to the ImageView.

res/drawable/image_border.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<gradient android:angle="90"
android:startColor="@color/image_border_start"
android:centerColor="@color/image_border_center"
android:endColor="@color/image_border_end" />
</shape>
</item>
<item android:top="2dp" android:left="2dp"
android:right="2dp" android:bottom="2dp">
<shape android:shape="rectangle">
<solid android:color="@color/default_back_color" />
</shape>
</item>
</layer-list>

res/values/styles.xml

Add the following lines:

<style name="myImageView">
<!-- 3dp so the background border to be visible -->
<item name="android:padding">3dp</item>
<item name="android:background">@drawable/image_border</item>
<item name="android:scaleType">fitCenter</item>
</style>

res/values/colors.xml

Add the following lines:

<color name="image_border_start">#40990000</color>
<color name="image_border_end">#FF660000</color>
<color name="image_border_center">#FFFF3333</color>

And finally specify the style of your ImageView in your layout xml:

<ImageView android:id="@+id/my_image" 
android:layout_width="100dp" android:layout_height="100dp"
android:src="@drawable/efteling"
style="@style/myImageView" />

Android add border image to ImageView

You have got multiple options:

  1. Place an other View with the border (also imageview) above your Imageview. Thay might be the simples solution. Just add an other view to your xml and make sure they are overlapping. (Use for example a Relative or FrameLayout as container)

  2. Use a Layer List and draw a shape above a Bitmap and add that to your ImageView

  3. Write a custom ImageView and use the Canvas to draw the Border in the overwritten onDraw method. E.g. canvas.drawRect(...) its pretty straightforward.

set border to imageview dynamically

You can do it like the following:

ImageView v = new ImageView(this);
v.setBackgroundResource(R.drawable.btn_default);

You can also use v.setBackgroundDrawable if you have a Drawable object instead of the drawable's resource id.

Imageview shows border around photo that does not exist in the source image

Check this,

my xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="#000"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.charu.its2017huree.MainActivity"
tools:showIn="@layout/app_bar_main"
android:orientation="vertical">

<ImageView
android:background="#FFF"
android:src="@drawable/amanda"
android:id="@+id/my_image_view"
android:layout_width="match_parent"
android:layout_height="300dp" />

</LinearLayout>

I have set an imageView and i found out it seems there is something wrong with it's left and right edges. I have given match_parent to my imageView already.So I add a background color to my imageView to find out what's going on.

But that's not enough for me i want to see the boundaries as well.It's there on your android mobile!

Settings >Developer Options >(scroll bit down) under category Drawing there is a option Show layout boundaries > Tick that option .. mm You get some kind of grids in your mobile.It's normal it will be gone when you untick that option.

Sample Image

Let's jump back to image view and see how it looks now!

  1. Now i can clearly see a background color which i add in behind of the image
  2. Also image view width boundary is fine,it's match_parent.You can see that from your layout boundaries.

Sample Image

MM sounds like image didn't fit properly then i try something with scaleType like android:scaleType="fitXY" and run and see how is it now .. all-right it seems she has taken the full space .. I mean in my imageView :D

This way you can clearly identify what'w wrong with your imageView

Sample Image

Display border to image inside an ImageView

if you want border around imageview you can try this

You can use CirlcleImageView library for rounded ImageView:

compile this libray

compile 'de.hdodenhof:circleimageview:2.1.0'

use like below in your layout

 <de.hdodenhof.circleimageview.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/profile_image"
android:layout_width="96dp"
android:layout_height="96dp"
android:src="@drawable/profile"
app:civ_border_width="2dp"
app:civ_border_color="#FF000000"/>

for more information follow this link
Sample Image

How to highlight a border of an Image View when I clicked imageview in android

  1. Create a drawable file, in the drawable folder.

highlight.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="0dp"/>
<solid android:color="@android:color/transparent"/>
<stroke android:color="#FFC830"
android:width="3dp"/>
</shape>

  1. ImageView in your activity

    <ImageView
    android:id="@+id/iv_test"
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:padding="3dp"
    android:src="@drawable/yourImageName"
    android:tint="@color/colorAccent"/>

Here padding is important.


  1. Inside activity code

    imv.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    Drawable highlight = getResources().getDrawable( R.drawable.highlight);
    imv.setBackground(highlight);
    //
    //Do your other stuff here.
    //
    }
    });

If you want to remove the background, use this code :

imv.setBackground(null);

In the activity xml, the padding attribute is important because the highlight background will take the same size of the imageview. So if there is any image in the imageview we will not be able to see the background/highlight. So the padding attribute pushes the image a bit inwards, letting the highlight to be seen.

Output

First
When Clicked

UPDATE

Inside you code implement View.OnClickListener.

And change your onClick() method

@Override
public void onClick(View v) {
Drawable highlight = getResources().getDrawable( R.drawable.highlight );
for (int j=0;j<ii.length;j++)
{
if (ii[j].getBackground()!=null) {
ii[j].setBackground(null);
}
}
v.setBackground(highlight);

//
//Do what you want to do when clicking on the imageview
//
}

And right now you are using

ii=new ImageView(singleshooppingcart.this);

make it an array and use it like this

ii[i]=new ImageView(singleshooppingcart.this);

Change this

ii.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

Drawable highlight = getResources().getDrawable(R.drawable.imgviewpress);
ii.setBackground(highlight);

to

    ii[i].setOnClickListener(this);

Here i is the looping variable.

This way you will have objects for all your imageViews. and all these imageviews will have a ClickEvent which we have set.



Related Topics



Leave a reply



Submit