Draw a shape in android studio

dal

dal

Associate
Joined
10 Sep 2005
Posts
900
Location
Lincolnshire
Hi, so all I want to do is draw a rectangle or square in android studio and fill it with a colour use RGB values without having to spend what feels like 2 years reading up on how to do it. I've tried looking at
THIS but can't get any of the code snippets to work ( even with trying to import stuff )
So if anyone could show me I would really appreciate it, thanks in advance.
 
Associate
Joined
1 Dec 2008
Posts
267
The simplest way to do it right now, in a brand new project, would be to use Jetpack Compose to draw a box, clip it to the Rectangle Shape and set the color to your RGB color like so:

Code:
val color = Color(0, 255, 0, 255)
Box(modifier = Modifier.size(100.dp).clip(RectangleShape).background(color))

Thats the bare minimum and you'll see a preview like this:
te4j5kD.png


Reason I said brand new project is because if you're wanting to add a rectangle to a view in an existing project then your project might use XML views opposed to Jetpack Compose since it's relatively new. I'd recommend looking at this stackoverflow post which creates a drawable, then you create a view in your XML which background uses the drawable.
 
Last edited:
  • Like
Reactions: dal
Back
Top Bottom