Is it ok to use png as a button background? - android

Is there is any drawback of using png as a background for a Button or EditText. Or should I write a code for XML for background of Button or EditText. I want to know which is the better way.

You can use color,drawable file or image as background
Color
android:background="#2F6699"
Drawable
android:background="#drawable/edittext_background"
Image
android:background="#drawable/ic_launcher"
Drawable file
<?xml version="1.0" encoding="utf-8" ?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:thickness="0dp"
android:shape="rectangle">
<stroke android:width="2dp"
android:color="#2F6699"/>
<corners android:radius="3dp" />
<gradient android:startColor="#C8C8C8"
android:endColor="#FFFFFF"
android:type="linear"
android:angle="270"/>
</shape>

I think you can try 9 patch image for button and edittext background.
9 Patch:
9-Patch images are stretchable, repeatable images reduced to their
smallest size; users draw a right and bottom, solid black 1 pixel
border to tell the system how to place the content within the image.
For more information visit this post:
Utility of android nine patch

just put png image into drawable, and than set it in xml via android:background="#drawable/image.png"

Use it in button properties
android:background="#drawable/imageName"

Related

How can I put my image in a circle without it coming out on Android Studio?

I have a problem with an ImageView, I would like to put this image in a circle that contains only a border with the transparent inside so that the image takes up all the space, but without leaving the circle.
What is currently not working! The image goes beyond the circle and I'd like to fix it! Do you have any idea?
This is my circle :
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="6dp"
android:color="#color/colorRedLogoDark"/>
<corners android:radius="1000dp"/>
This is my ImageView :
<ImageView
android:id="#+id/icon_about"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginTop="10dp"
android:layout_gravity="center"
android:src="#drawable/thermometer"
android:background="#drawable/background_about"
/>
It's possible or I have to got a round image ?
EDIT : Actually, I would like to got an image with a border-radius in color !
There are several approaches to tackle this.
1- You can wrap your image in a card view and adjust the corners to look like a circle. The padding in that case will act as a border
To achieve a circular shape using Card view you can set the shape property, android:shape="ring".
app:cardCornerRadius should be set to half the width or height of the ImageView
2- Use a library that does exactly that such as CircleImageView
3- Crop your image through java
I would like you to modify your background_abtout.xml file and simply add this lines of code
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadius="0dp"
android:shape="ring"
android:thicknessRatio="1.9"
android:useLevel="false" >
<solid android:color="#android:color/transparent" />
<stroke
android:width="6dp"
android:color="#color/colorRedLogoDark"/>
I think this will work perfectly fine

How to set background color of an image, but within the border?

My goal is to display a circular image, and allow the user to set its foreground and background colors respectively, for example:
My attempt was to create an image asset with transparent background, then use ImageView::setColorFilter to change its foreground, and use ImageView::setBackgroundColor to set its background. The image asset looks like this:
My problem is that pixels outside of what we humans call 'border' are also transparent, so the result looks like this:
How do people usually deal with this issue? Although I was doing Android development, but any ideas or code snippets in any language are appreciated!
at the first, you must create a new drawable file and write below code for creating the circle with yellow color with blue border:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#cab816" />
<size android:width="50dp"
android:height="50dp"/>
<stroke android:width="2dp"
android:color="#color/darkBlue"/>
</shape>
and now you must set this drawable to the background of your imageview and set your image with src.
if your image covered all of this circle, use padding for decrease image size.

blur edges of 9-patch image of editbox

i have generated nine patch image using 9-Patch generator
but its not showing desired result , following is my core image and i have use this in a 9-patch image generator.
current result is
desired result is
you can clearly see the difference that edges of 9 patched image is quite thin and blur. how do i resolve it
Edits
Region
Padding
Optical
Make sure, while generating 9-patch image all the curves are not selected.
Check out the below image.
Also before downloading there is option "Interactive Preview", check it out as per the name it will show how your image will show for your current selection.
I don't see a reason to go for 9 patch png mess for simple layouts like this.
You can easily achieve it using a drawable and set it as background for your EditText.
Use the below code for your drawable, eg. blue_border.xml :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<corners android:radius="4dp" />
<stroke android:width="1dp" android:color="#2979ff"/>
<solid android:color="#color/colorWhite"/>
</shape>
</item>
</selector>
Use the above blue_border.xml as background to EditText in main layout.
<EditText
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:maxLines="1"
android:maxLength="10"
android:background="#drawable/rounded_layout_ex"
android:hint="Mobile Number"/>
The outcome you'll get from above code:
You can always change the border color, width, background or corner radius by changing the attributes in your drawable blue_border.xml.

Android enlarge transparent background of icon drawable

I have many different dynamic icons in my Android app and I would like to put a rectangular frame around all of them. I tried to use LayerDrawable but I think it scales the smaller drawable to the size of the larger one so in the end the icons overlap with the frame instead of within it. (the icon drawables are 64x64 while the frame drawable is 96x96). Is there an way to enlarge the transparent background of the icon drawables to the same size as the frame drawable without scaling the actual icon?
Any help would be appreciated!
You should be able to use a LayerDrawable for this; just tested it myself and it seems to work just fine. Define the frame first, then add another item with the specified insets.
<?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="#FF0000"/>
</shape>
</item>
<item android:left="20dp" android:top="20dp" android:bottom="20dp" android:right="20dp">
<bitmap android:src="#drawable/ic_launcher"/>
</item>
</layer-list>

How to control the gradient on a shape?

I have a screen with a label on top. This label is done with a TextView.
As a background I'd like to have a vertical gradient, starting with color1, changing to color2 and back to color1.
At the moment I have:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke android:width="1px" android:color="#000000" />
<gradient
android:startColor="#FFFFFFFF"
android:centerColor="#FF8800"
android:endColor="#FFFFFFFF"
android:type="linear"
android:angle="270"
/>
<corners android:radius="10dp"/>
</shape>
My problem is that the centerColor line is too thin. I want it to ocupy all the letters space.
I can't find any way to make the gradient to be faster.
I've already tried to use a layout-list but with no success.
Any idea?
This is a much more different track to follow but have you considered working with 9-patch image files? This will allow you to stretch the parts of the gradient that you wish to and make the centre line as big as needed by segmenting up the background properly.
Here are some great tutorials I have used to learn about them:
draw 9 patch tutorial
And from Google's own Android mouth:
Android 9 patch
All Drawable resources for Android

Categories

Resources