set border to imageview dynamically - android

I have a xml file in drawable to set border for imageview know as imgborder.xml as below
<?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>
May I know how can I apply it dynamically in code? Like android:background in xml layout?
Thank you.

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.

Related

How to add photo frame over an image?

enter image description here
How to add this type of frame in android over an image like the picture given
If you are searching for custom border and shape of ImageView
Try this
or
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

how to customize an image view and textview?

i have a listview and the row model/design should be something as shown in the image below.
My question is, how can I design a view to appear as shown in the image in the the third row?? I want to design something like the blue rectangular shape with round corners and then write the value on it as shown in the image
Is it a textview or imageView? Please advice how can i design such a view.
image:
You must create a TextView and give it a Shape:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke
android:width="1dp"
android:color="#color/common_border_color" />
<solid android:color="replace with blue color" />
<padding
android:left="1dp"
android:right="1dp"
android:top="1dp" />
<corners android:radius="5dp" />
</shape>
Make this in an xml called rounded_corner.xml in the drawable folder.
android:background="#drawable/rounded_corner"
You can use textview and as a background of text view use below xml
rounded_corner.xml
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid
android:color="#abcdef" >
</solid>
<corners
android:radius="20dp" >
</corners>
</shape>
In your drawable create one xml file and add this code in that
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<padding
android:top="3dp"
android:left="8dp"
android:right="8dp"
android:bottom="3dp"/>
<solid android:color="#00796B" />
<corners
android:bottomLeftRadius="20dp"
android:bottomRightRadius="20dp"
android:topLeftRadius="20dp"
android:topRightRadius="20dp" />
In your view(Button/TextView) add background, some ex code
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/delete"
android:text="Ur text"/>

Add gradient in edit text border in android

I want to add gradient only to the border of my editText , I can add gradient but it takes the entire background . How can I apply gradient only to the border , and the rest should be transparent?
This one does the trick :)
<?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="45"
android:endColor="#0256FC"
android:centerColor="#0256FC"
android:startColor="#FE00B1" />
<corners android:radius="4dp" />
<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
<size
android:width="162dp"
android:height="44dp" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<solid android:color="#color/colorWhite" />
<corners android:radius="4dp" />
<size
android:width="50dp"
android:height="50dp" />
</shape>
</item>
</layer-list>
Add the edittext in a layout
Set gradient background color for that layout
Set margin 1dp for edittext.
Set white background for edittext
i think now it's displays like your expectation, thank you
Create your button_border.xml and keep it within drawable.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- view border color and width -->
<stroke
android:width="1dp"
android:color="#color/white" >
</stroke>
<!-- The radius makes the corners rounded -->
<corners
android:radius="2dp" >
</corners>
</shape>
Now set your editText background as follows :
android:background="#drawable/button_border"

ImageView with rouded corners, solid border and glow effect

I want to put rounded corners and border to an ImageView, with custom border color. Also, I would like to achieve a glowing effect with the color of the border. Attached sample image. Note that the source image has square borders.
I suppose this is achievable only by using Canvas? Any ideas and sample code?
this is what i have done for my ImageView to make same changes as you want.
made one image_shape.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
android:width="1dp"
/>
<corners
android:radius="50dp" />
<padding
android:left="10dp"
android:right="10dp"
android:top="10dp"
android:bottom="10dp"/>
<solid android:color="#10151D"/>
</shape>
Now, put this xml as the background of your imageView then you will get effect as you want.
Right now i have set my own color. you can put your desire color and get effect as rounded glow border effect.
try using the following code in a new xml file named roundCorners in your drawable folder:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#000000" />
<stroke
android:width="1dp"
android:color="#DDDDDD" />
<corners
android:bottomLeftRadius="2dp"
android:bottomRightRadius="2dp"
android:topLeftRadius="2dp"
android:topRightRadius="2dp" />
</shape>
And then set it as a background to your ImageView in the layout file by:
android:background="#drawable/roundCorners"
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:thickness="0dp" >
<stroke android:color="#ff5febe7" android:width="2dp" />
<solid android:color="#601E3232"/>
<corners android:radius="5dp" />
</shape>

How to set image border for Layout (RelativeLayout) in Android?

I want to use border image is below :
set border for Layout ...?
Should i do it ?
<?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>
Save this file in as customborder.xml in drawbale folder. In you layout file add this line android:background="#drawable/customborder"
You can make a 9-patch image as apply it as the background. Make the middle transparent and fill the border the way you want.
Here is the official documentation: http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch
What you can do is create an custom drawable shape that will apply as the background of ImageView which will create a border.
Something like below xml, you can customize it yourself. Put it in drawable/image_border.xml
<?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:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
</shape>
Apply it as ImageView's background. It should create a border for your ImageView.
first create simple border.xml and save in to drawable
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle" >
<corners android:radius="15dip" />
<solid android:color="#00000000" />
<stroke android:width="2dp" android:color="#000000"/>
</shape>
</item>
</layer-list>
then after set background of Linear/Relative layout
android:background="#drawable/imageee_color"

Categories

Resources