Geethu February 2016

Custom Linear layout with round corner

I tried to draw a custom linear layout, But the problem I faced is I am not getting the round corner for the linear layout

 public class RoundLinearLayout extends LinearLayout {


private float radius;
private Path path = new Path();
private RectF rect = new RectF();


public RoundLinearLayout(Context context)
{
    super(context);
    radius = 20;
   // setWillNotDraw(false);
}


public RoundLinearLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
   // init(context);
}

public RoundLinearLayout(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
   // init(context);
}
@Override
protected void onDraw(Canvas canvas) {
    path.reset();
    rect.set(0, 0, canvas.getWidth(), canvas.getHeight());
    path.addRoundRect(rect, radius, radius, Path.Direction.CCW);
    // Add 1px border RED here ?
    path.close();
    canvas.clipPath(path);
}
}

I really donno what went wrong.. Some please help me to sort this out.

Answers


user5716019 February 2016

Use below xml in Drawables.

 <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#FFFFFF" />
    <stroke
        android:width="2dp"
        android:color="#000" />
    <padding
        android:bottom="1dp"
        android:left="1dp"
        android:right="1dp"
        android:top="1dp" />
    <corners
        android:bottomLeftRadius="8dp"
        android:bottomRightRadius="8dp"
        android:radius="8dp"
        android:topLeftRadius="8dp"
        android:topRightRadius="8dp" />
</shape>

and set it as a Background of your LinearLayout.


N J February 2016

I will suggest you to use simple CardView

use compile dependency

compile 'com.android.support:cardview-v7:21.0.+'

Example

<android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
 <LinearLayout
.....
your other child layout goes here
</LinearLayout>
</android.support.v7.widget.CardView>


Manoj Srivastava February 2016

You can choose your color and set background to your linear layout.

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="4dp" />
    <solid android:color="#80000000" />
</shape>

Post Status

Asked in February 2016
Viewed 2,634 times
Voted 8
Answered 3 times

Search




Leave an answer