Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make layout with rounded corners..?

How can I make a layout with rounded corners? I want to apply rounded corners to my LinearLayout.

like image 560
Addy Avatar asked Apr 23 '13 05:04

Addy


People also ask

How do I round corners in XML?

To do that right click on drawable (under resources), choose New, choose Drawable Resource File. Name the resource rounded_blue_border and make the root element shape. We will use the attributes stroke, corners and padding. The stroke determines the width and color of the border.

How do you make rounded corners on Android?

xml file and add an attribute to that TextView, for which you want to add rounded corners. The attribute is android: background=”@drawable/rounded_corner_view”.


2 Answers

1: Define layout_bg.xml in drawables:

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

2: Add layout_bg.xml as background to your layout

android:background="@drawable/layout_bg" 
like image 83
Gaurav Arora Avatar answered Sep 21 '22 13:09

Gaurav Arora


For API 21+, Use Clip Views

Rounded outline clipping was added to the View class in API 21. See this training doc or this reference for more info.

This in-built feature makes rounded corners very easy to implement. It works on any view or layout and supports proper clipping.

Here's What To Do:

  • Create a rounded shape drawable and set it as your view's background: android:background="@drawable/round_outline"
  • Clip to outline in code: setClipToOutline(true)

The documentation used to say that you can set android:clipToOutline="true" the XML, but this bug is now finally resolved and the documentation now correctly states that you can only do this in code.

What It Looks Like:

examples with and without clipToOutline

Special Note About ImageViews

setClipToOutline() only works when the View's background is set to a shape drawable. If this background shape exists, View treats the background's outline as the borders for clipping and shadowing purposes.

This means that if you want to round the corners on an ImageView with setClipToOutline(), your image must come from android:src instead of android:background (since background is used for the rounded shape). If you MUST use background to set your image instead of src, you can use this nested views workaround:

  • Create an outer layout with its background set to your shape drawable
  • Wrap that layout around your ImageView (with no padding)
  • The ImageView (including anything else in the layout) will now be clipped to the outer layout's rounded shape.
like image 28
hungryghost Avatar answered Sep 25 '22 13:09

hungryghost