Android Rounded Edges for backgrounds - android

I was wondering if anyone knew how to make rounded background edges for only certain corners.
For example, in the lock screen of Android 2.2 there were the two slidingDrawers to unlock the phone and take a picture or something.
The slidingDrawers had a rounded corner with an icon within it. Anyone know how to make the corners of a background rounded?

There are lots of ways in achieving that.
You could use a 9patch drawable, and I'm guessing that's what they used.
You can also use a shape drawable, with proper corners.
Other than that, your question is too ambiguous. But this should provide enough for a good answer to your problem.

you can create a xml file inside the drawable folder with some code like this ,
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF"/>
<corners android:radius="5px"/>
<padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" />
</shape>
change the radius values for the amount of rounded edge you need. Apply this xml as the background of your textview or edittext or anything.

Related

Custom Shapes with text in Android

I am new to android. I want to draw a custom shape in android. When someone chooses a date(via date picker) I want to display the result in a square box rather than the regular way.How do I achieve this?
You can use a textview to show the selected date. Since you seem to know the coding for that, I focus on the design you can apply for your textview.
By default, a textview has a rectangular shape with sharp edges and points, but you can achieve a rounded-corner textview using a style , like this one :
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
android:width="1dp"
android:color="#color/red"/>
<corners android:radius="7dp" />
<padding
android:left="1dp"
android:right="1dp"
android:top="1dp"
android:bottom="1dp"/>
<solid android:color="#color/white"/></shape>
The corners tag, is responsible for the rounded corners, you can tweak th radius to make more or less rounded.
Stroke tag, defines the width and color of the border for the textbox.
and Solid tag determines the bakcground color of the view.
In order to use this style, you should save it as an XML file and save it in drawable folder (normally drawable-hdpi will do). Then , in your tetxview or edittext you should set the background property as the following :
android:background="#drawable/thesavedxmlfile"

rounded view's corners android, NOT round background

I know how to give round borders using XML in Android. I thought it would be cool to make the radius of one corner of my textView excessively large. The problem is, the text keep spilling out. Can I make my textview have a TRULY round corner? (Not just the background). If this was CSS this would be so easy. I am new to Android.
So in terms of CSS, I want to set my overflow to hidden so to speak.
Please help me.
In short, no. All Views are rectangular and fit in bounding boxes.
The best way to achieve rounded corners is the way you mention; using a shape drawable with corner radius set as the background to your TextView.
Like ataulm said, all Views are rectangular.
Create a shape drawable allows you to create a background with rounded corners. You'd have to use padding to make sure the text doesn't clip in certain areas.
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ffffffff"/>
<stroke android:width="4dp"
android:color="#ff0000"/>
<padding android:left="4dp"
android:top="4dp"
android:right="4dp"
android:bottom="4dp"/>
<corners android:bottomRightRadius="7dp"
android:bottomLeftRadius="7dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp"/>
</shape>

Shadow round corners Android

how can i make a shadow for round corners? I tried it with views for the bottom, right an the corner, but that works not really :D
I tried this xml for the corner:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#404040"
android:centerColor="#DBDBDB"
android:endColor="#color/shadow_end"
android:angle="315"
>
</gradient>
<corners
android:bottomRightRadius="5dp"
android:bottomLeftRadius="0dp"
android:topLeftRadius="0dp"
android:topRightRadius="0dp"/>
</shape>
Is it good to use views for shadows? What you are using?
Thanks for help :)
You can simply draw a 9-patch image with Photoshop drawing the shadows and put it into the drawable folders and set this in your View in xml:
android:background="#drawable/your_9patch_image"
I always do it by using Photoshop. For more details see this: Draw 9-patch
Or you can check in the default Android Studio drawables and see if there's one that you like. Check my answer here: my answer

Radial gradient in XML with parent size

Im trying to achieve a component to make custom shadows to buttons or other components, i know that it will be easier with a 9patch or a png with the shadow, but i want to change it color and size programmatically also in its states (pressed,etc), so i decided to try with 9 images, all in XML so the shadow shades start its gradient from the side of the component.
<!-- Left Shadow layer -->
<item>
<shape android:shape="rectangle" >
<gradient
android:angle="0"
android:endColor="#FFFF0000"
android:startColor="#00FF0000" />
</shape>
</item>
It looks good, the problem is on the corners and with the android:gradientRadius parameter now its set to a fixed size, but in the contextual help is said that can be set in a percentage of the base size 10% or parent size 10%p, what i want its to set a 100%p radius so the gradient will always go from the main color and disappear in the edge of the square.
-- EDIT --
The android doc about gradientRadius gradientRadius
<shape android:shape="rectangle" >
<gradient
android:endColor="#00FF0000"
android:startColor="#FFFF0000"
android:gradientRadius="18"
android:centerX="100%"
android:centerY="100%"
android:type="radial" />
</shape>
And thats where im now :( i do not know how can i set this size to fit its parent view.
Any help will be appreciated, when im finished with the component i will put the code in an answer :) so typical buttons can have customizable shadows in xml.
An image of the deserved component.
--Edit--
Im still interested in this :) no one has a clue?
I think you should give up with xml and implement drawable in code.
When you extend Drawable class you can get size as rectangle with getBounds(). Also you can dynamicaly recalculate in onBoundsChange method.
You can also easily construct gradients and use them in Paint object (setShader method)

How to add corners to a view

I added a full border around a view but I need to add just the corner as shown image below :
I mean the red corner only .
I tried to adjust the below border xml , but it didn't work :
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke android:width="10dp" android:height="10dp" android:color="#B22222" />
<solid android:color="#FCE6C9" />
<corners android:radius="20dp" />
</shape>
Any help will be appreciated
I don't think it's possible to do this using a ShapeDrawable, as it would require you to use some sort of margin or padding on the drawable itself. There actually is a padding attribute, but unfortunately that only has effect on the content of the View, and not the drawable itself.
That being said, an easy solution would be to create a 9-patch in stead and apply that as background to the TextView. Just for demonstration purposes: make the 9-patch look somewhat like this:
Edit:
On second thought, there's actually another option that relies on using a LayerDrawable to create the desired effect. It's a bit tedious to create and I have my doubts it'll be more efficient than using a 9-patch, but at least you don't have to render out images, which means that if you need to make e.g. a change in colours, it's more straightforward.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/rounded" />
<item android:drawable="#android:color/white" android:left="30dp"
android:right="30dp" />
<item android:bottom="30dp" android:drawable="#android:color/white"
android:top="30dp" />
<item android:bottom="30dp" android:left="10dp" android:right="10dp"
android:drawable="#color/pink" android:top="30dp" />
<item android:bottom="10dp" android:left="30dp" android:right="30dp"
android:drawable="#color/pink" android:top="10dp" />
</layer-list>
Some details: #drawable/rounded is the code snippet you posted yourself. The following two items are simply white rectangles with an offset, to create the white edges. Now, since these will also overlay the pink surface, we need two more pink rectangles (again with specific offsets) to counter that. The result is a background that looks exactly like what you're showing in your question.
Note that you might want to see if you can optimise this a bit. At the least I'd recommend not hardcoding the offsets (like I did for simplicity), but store them in a dimens.xml file so you can keep these values centralized and consistent by referencing them from both the ShapeDrawable and LayerDrawable.
Addendum: On pre-ICS (or perhaps pre-Honeycomb) devices, there appears to be an issue with directly referencing colours with the android:drawable attribute. You can however easily get around this by setting up another drawable (be it either a 9-patch or ShapeDrawable) to represent this colour. For example, in the snippet above, you would replace android:drawable="#color/pink" with android:drawable="#drawable/color_pink", where color_pink can simply be an xml file containing:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#FCE6C9" />
</shape>
Obviously you will need to do the same for all other colours referenced in the LayerDrawable. Tested on Gingerbread 2.3.7.
I would create a 9-patch file and set it to be the background of the main container. The steps to do that would be.
You create the background you would like roughly in fireworks, illustrator, or whatever image editing software you prefer.
Then crop the artwork so that there is only a 1 pixel border around the artwork. Save it as a png.
Open the draw9patch.bat file in your android sdk folder on your computer C:\Program Files (x86)\Android\android-sdk\tools.
Open your png file. You can then use your mouse to click on the outer 1 pixel border which will turn the clicked pixels black. The areas that you have black pixels on both the top and bottom or on the left and right will be the area that is stretched. In your case you just want to have the middle area where there is no red stretched.
My personal preference is to just open the file above and save it as a 9 patch file. Then open it in my photo editing software to create a 1 pixel thick line in the same fashion as above. It is quicker and more precise.
Finally add the file to your drawable folder. Then set the background of your main view container to the drawable.
That should be it. Hope that helps.

Categories

Resources