Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

It is possible to create this simple shape in Android XML?

I am trying to create the following simple shape in Android XML:

Simple Shape

Just a rectangle combined with a triangle to form an arrow. The shape should be used as navigation button background.

On Windows Phone it would be no problem to create this shape in XAML but the XML drawing capabilities of Android seem to be much more limited.

I tried to create a layer-list drawable using a normal rectangle as "body" and rotated rectangle as triangle. Creating a triangle by rotating a rectangle works fine as long as the triangle is aligned with the border. As soon as I try to move/translate the triangle to the left (to the end of the body/box), the triangle is not triangle any more but of course just a rotated rectangle... Is there any way to clip off the one half of the rotated rectangle and move it to the end of the box in XML?

Of course this can be done in Code/Java, but I would like to know if it also possible XML.

I would need this kind of button in many different sizes and using a XML drawable would be much better (from my point of view) than using dozens of PNGs.

like image 410
Andrei Herford Avatar asked Jun 10 '14 12:06

Andrei Herford


People also ask

What is shape in XML?

The Shape Drawable is an XML file that defines a geometric shape, including colors and gradients. This is used to create a complex shape that can then be attached as the background of a layout or a view on screen.

How do you make a square in XML?

You can create a new XML file inside the drawable folder, and add the above code, then save it as rectangle. xml. To use it inside a layout you would set the android:background attribute to the new drawable shape.


1 Answers

try this custom Shape:

Shape shape = new Shape() {
    Path path = new Path();
    @Override
    protected void onResize(float width, float height) {
        path.reset();
        path.lineTo(width - height / 2, 0);
        path.lineTo(width, height / 2);
        path.lineTo(width - height / 2, height);
        path.lineTo(0, height);
        path.close();
    }
    @Override
    public void draw(Canvas canvas, Paint paint) {
        canvas.drawPath(path, paint);
    }
};
ShapeDrawable d = new ShapeDrawable(shape);
d.getPaint().setColor(0xff6699bb);

someView.setBackgroundDrawable(d);
like image 73
pskink Avatar answered Oct 06 '22 01:10

pskink