haimlit
haimlit

Reputation: 2582

Rounded corners on android - which implementation is better?

There are two main approaches for rounded corners on android:

What I don't understand is why should I ever prefer using 9 patch instead of layout definition? Is there any benefit for it performance wise or for some other aspect?

Upvotes: 0

Views: 79

Answers (2)

weston
weston

Reputation: 54801

I would choose the simplest tool that gives me what I need. Especially knowing I can switch later easily. Xml if simple shape is all I need and 9-patch if I need something more complex or have been supplied an asset to use.

A 9-patch can do far more than simple rounding in an xml shape, it stretches the original asset maintaining the four corners in their aspect ratio, they might be round they might not, they could be as complex as you like.

Odd-shape

I should think the performance is not a reason for making either choice in most sensible use cases.

Upvotes: 1

arsent
arsent

Reputation: 7183

There is also the 3rd option with CardView which is much cleaner and easier to maintain.

<android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:layout_width="200dp"
        android:layout_height="200dp"
        card_view:cardBackgroundColor="@color/green"
        card_view:cardCornerRadius="4dp"
        card_view:cardElevation="0dp" >
</android.support.v7.widget.CardView>
  1. Use XML if you want to make rounded corners to a View (Button, TextView etc.)
  2. Use CardView if you want to make rounded corners to a ViewGroup (LinearLayout, FrameLayout etc.)
  3. Use 9 patch if you can't achieve what you want with any of above 2

Upvotes: 1

Related Questions