-7

I want to make a rounded view as below

enter image description here

I expect to create that view with programmatically changed radius, color. Not from xml configured

Uni
  • 187
  • 2
  • 7
  • 18
  • 1
    You want to make it? Cool... – cs95 Jun 17 '17 at 15:48
  • Yes. Maybe my poor English doesn't expose my idea. I want t build a java class to draw a view like above and it's transparent inside. I don't know what is wrong with my question? – Uni Jun 17 '17 at 15:54
  • 1
    `I expect to create that view` We also expect **you** to create it. Or, at least, to try to. – Phantômaxx Jun 17 '17 at 16:35
  • In fact, I tried some ways like this: https://stackoverflow.com/questions/26074784/how-to-make-a-view-in-android-with-rounded-corners However the result seems not same thing I want. I have no idea of keyword to move on. So I decided to ask here. It may sound stupid but I'm struggling... – Uni Jun 17 '17 at 16:41
  • 1
    There's no evidence of your efforts. – Phantômaxx Jun 17 '17 at 16:47

1 Answers1

3

Make new class

ShapeMaker.class

public class ShapeMaker {

public ShapeMaker() {

}

public GradientDrawable circle (int backgroundColor, int borderColor, int storke, int radius) {
    GradientDrawable shape = new GradientDrawable();
    shape.setShape(GradientDrawable.RECTANGLE);
    shape.setColor(backgroundColor);
    shape.setStroke(storke, borderColor);
    shape.setCornerRadius(radius);

    return shape;
}
}

Call it

 myLayout.setBackground(new ShapeMaker().circle(Color.BLUE, Color.BLACK, 1, 10));

EDIT

Layout example:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_green_light"
tools:context="net.netbox4u.gradientcolor.MainActivity">

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginTop="8dp"
    android:elevation="8dp"
    android:text="Imagine"
    android:textSize="30sp"
    android:textStyle="bold"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<LinearLayout
    android:id="@+id/shape_background"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginBottom="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginTop="8dp"
    android:orientation="vertical"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="1.0"></LinearLayout>
</android.support.constraint.ConstraintLayout>

MainActivity example:

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LinearLayout shapeBackgroundLayout = (LinearLayout)findViewById(R.id.shape_background);

        shapeBackgroundLayout.setBackground(new ShapeMaker().circle(Color.WHITE, Color.TRANSPARENT, 0, 50));
    }
}
Milos Lulic
  • 627
  • 5
  • 22
  • it's not transparent inside – pskink Jun 17 '17 at 16:05
  • You can cusomize it! myLayout.setBackground(new ShapeMaker().circle(Color.TRANSPARENT, Color.BLACK, 1, 10)); – Milos Lulic Jun 17 '17 at 16:09
  • no, no, no, see OP image, he wants green area customizable (different colors) with the transparent "hole" inside - something like a frame with rounded inner cormers – pskink Jun 17 '17 at 16:11
  • ShapeMaker().circle(background_color, storke_color, storke_width, corner_radius)) – Milos Lulic Jun 17 '17 at 16:12
  • Thank you Milos Lulic. I'm trying your code. I see that storke_width value doesn't expand the color but contract. I want to make a frame like pskink said. Could you plz review your example one more time? – Uni Jun 17 '17 at 16:24
  • I just edited my answer. Hope it helps – Milos Lulic Jun 17 '17 at 16:40
  • Your edition result is same the picture above green frame and white inside) but how to the side become transparent? I want shape_background has a green not its parent ConstraintLayout – Uni Jun 17 '17 at 17:02