How can I build such button? - android

I need to implement such button for my Android app. It would be great to do this without using full image as button.
I've done almost the same button using <shape> with <gradient>. The only thing I need is to add bottom blue button shadow and include image.
Is it possible?
Thanks in advance.

You can do this by combining Shape Drawables inside a Layered Drawable. To do so, you'll have 3 xml files as below:
button.xml (the one you already have i guess)
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners android:radius="16dp" />
<gradient
android:angle="270"
android:centerColor="#FFFFFF"
android:endColor="#CAEBF8"
android:startColor="#FFFFFF"
android:type="linear" />
<padding
android:bottom="8dp"
android:left="8dp"
android:right="8dp"
android:top="8dp" />
<stroke
android:width="2dp"
android:color="#FFFFFF" />
</shape>
button_bg.xml (to add the blue border below the button)
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners android:radius="16dp" />
<solid android:color="#6FC8F1" />
</shape>
layered_button.xml (to combine the previous xml, and the drawable to use as background on your button)
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/button_bg"/>
<item
android:bottom="2dp"
android:drawable="#drawable/button"/>
</layer-list>
You'll find more information on the Layer List in the Drawables Documentation

Make your button's android:background attribute to point your desired image. Create your image with the text and the little person icon. It's not possible to put a text and an image at the same time inside a button.
For the blue shadow, you can either include it in your image or achieve the same result with the gradient attribute as you already said in your question.

For the image, you should use
android:drawableRight
As for the shadow I'm not sure how this is achieved.

Related

Custom background for Text View Using Xml drawable file

I want to make a textview background like in the screenshot. I have a xml file that can make rectangular background like in the screenshot but i am not able to do that below corner styling. I dont know what thing can change this.
Here is my xml file that can give a rectangular background to my textview.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke
android:width="1dp"
android:color="#color/orange" />
<solid android:color="#color/orange" />
<padding
android:left="1dp"
android:right="1dp"
android:top="1dp" />
<corners android:radius="5dp" />
</shape>
Your best bet here would be to make a nine-patch image. Roman Nurik has made a really nice nine-patch generator that you can use. Then you just set the image as the background and it will stretch like you think it should.

How to make a pill shaped button in android?

I am not sure what to call this... pill shaped maybe. Googling for rounded corners turns up lots of posts of people wanting a rectangular button with rounded corners. This is a little different but more like 2 circles and a rectangle which I tried to draw it as.
I would like to make a button shaped something like the first image below but with text and an icon image in it by using an xml drawable background on an android :
I have tried this which looks ok but if the button length varies it does not scale and you end up with a rectangle and some other strange stuff.
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:left="40dp" android:top="0dp" android:bottom="0dp">
<shape android:shape="oval" >
<solid android:color="#666666"/>
<size android:width="40dp" android:height="40dp"></size>
</shape>
</item>
<item android:left="20dp" android:right="20dp">
<shape android:shape="rectangle" >
<solid android:color="#666666"/>
<size android:width="20dp" android:height="20dp"></size>
</shape>
</item>
<item android:right="40dp">
<shape android:shape="oval" >
<solid android:color="#666666"/>
<size android:width="40dp" android:height="40dp"></size>
</shape>
</item>
</layer-list>
I have tried to create my xml drawable like this also:
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#666666" />
<corners android:radius="20dp"/>
<size android:height="20dp" android:width="60dp"></size>
</shape>
which looks like this:
I have looked at this persons example but when I do what he does my results are not completely rounded at the corners. They are more like the second image above.
You can try to save the following code to a drawable(e.g. pill_bg.xml):
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:dither="true"
android:shape="rectangle">
<corners android:radius="120dp" />
<solid android:color="#color/white" />
<stroke
android:width="1dp"
android:color="#efefef" />
<size
android:width="300dp"
android:height="120dp" />
</shape>
Take a look at size part and radius. Then apply the drawable to your view, for example:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="250dp"
android:layout_height="60dp"
android:background="#drawable/pill_bg" />
As you can see, your view can have different sizes, the background will be scaled.
As of v1.1.0-beta01 of the Material Components Android library you can use MaterialButton and a shapeAppearance style to easily define a pill button that doesn't require you to know the height of the button beforehand or rely on guessing a radius value high enough to go halfway down the side of the button.
Simply define a style like this:
<style name="PillShapeAppearance">
<item name="cornerFamily">rounded</item>
<item name="cornerSize">50%</item>
</style>
and set it on your MaterialButton like this:
<com.google.android.material.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
...
app:shapeAppearanceOverlay="#style/PillShapeAppearance"
/>
The key that every other answer I've looked at leaves out is to set cornerSize with a percent value so that the fraction value it supports is properly applied.
This resizes correctly for any view background.
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="1000dp" />
<solid android:color="#android:color/holo_red_dark" />
</shape>
Well you can make that button in any design program, also make one similar make it look like it is pressed then you make a selector drawable and assign it to the button background. That´s the way i do pretty awesome buttons

Programmatically change startColor of a border.xml that is used as a background in another xml?

I have an xml file called border.xml in the drawable folder.
In this xml I have
<!-- +++++++++++++++++++++ BORDER +++++++++++++++++++ -->
<item>
<shape android:shape="rectangle">
<solid
android:id="#+id/shape_border_color"
android:color="#color/black" />
<gradient
android:id="#+id/shapre_border_gradient"
android:startColor="#color/BurlyWood"
android:endColor="#color/Blue"
android:angle="270"
/>
<!-- ++++++++++++++++++++ ROUND CORNERS ++++++++++++++++++++++++++++ -->
<corners
android:id="#+id/shape_border_corners"
android:bottomRightRadius="7dp"
android:bottomLeftRadius="7dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp"
/>
</shape>
</item>
<!-- +++++++++++++++++++++++ BACKGROUND ++++++++++++++++++++++++++ -->
<item
android:left="5dp"
android:right="5dp"
android:top="5dp" >
<shape android:shape="rectangle">
<gradient
android:id="#+id/shape_background"
android:id="#+id/hr_design_background_gradient"
android:startColor="#color/DarkOrchid"
android:endColor="#color/LawnGreen"
android:angle="270"/>
</shape>
</item>
I use the above xml file as a background for a LinearLayout in my main.xml file using the following code
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:padding="0dp"
android:background="#drawable/border_o2">
In my Main.java activity I am interested to change the border's start and end colors as well as the background and Radius of corners
I know how to change a background of a certain id if i am using the a TextView for example. But I am unsure of how to approach changing the Item/Shape/Gradiant values.
Thanks
One approach would be to define a 2nd drawable resource, called drawable/alternate_background.xml. In this file you can code the alternate colors, radius values, etc.
Then you programatically load the alternate resource.
linearLayout.setBackgroundResource(R.drawable.alternate_background);
FWIW, one advantage of this approach is that your view is still externalized. Thus you can use the Eclipse layout editor to preview changes, rather than wait until runtime to see the effects of your changes.

Round image corner using custom widget

i want to get rounded corners for images in a custom list. I used the custom widget method mentioned in the following link:
http://wiresareobsolete.com/wordpress/2011/08/quick-rounded-corners/#comments
The image corner is getting blurred. Can any body give me a solution to have smooth corners using this mentod.
Make one file in drawable folder. Name any thing to it. And set the imageview background by using that file. It will give you a round corner.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke android:width="1dip"
android:color="#color/stroke_color"/>
<solid android:color="#color/white"/>
<padding
android:left="5dip"
android:top="5dip"
android:right="5dip"
android:bottom="5dip" />
<corners android:radius="5dip" />
</shape>

Can I get a hold of Button Location Data in my Android Application

In my application, I'm (still) trying to manipulate my Button Views to look the way I want. I'd like to attempt an experiment where I'll draw an Android Rect or RectF in the same location as the (transparent) button to highlight it's appearance.
However, I can't figure out how to get my mitts on that information. The buttons in question are defined in XML, in a Linear Layout, but something inside my Android has to know their sizes and locations. Right?
Any suggestions?
Thanks, R.
You should be able to set a background of the button to a custom xml drawable with the drawable containing only stroke element around the edge.
You will need to create this custom drawable: it would be just a few lines of XML.
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="1dp" android:color="#CCCCCC" />
<corners android:radius="10dp" />
</shape>
Edit: updated to include rounded corners.
This answer is a combination of my poking around and Aleks' suggestion to use a custom drawable. Ultimately, my custom drawable needed more than three lines, but it's still not very big:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient
android:startColor="#android:color/black"
android:endColor="#android:color/black" />
<stroke
android:width="1dp"
android:color="#cccccc" />
<corners
android:radius="5dp" />
<padding
android:left="4dp"
android:top="2dp"
android:right="4dp"
android:bottom="2dp" />
</shape>
</item>
</selector>
I needed to add all of the other detail to get the custom button to work the way I wanted -- and I'm probably not done, but if anybody is interested, this works pretty well...

Categories

Resources