Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android button with double border and gradient

I want to create a custom button. This button should have a gradient and a two pixel border, but the inner and outer edge should be in a different color (example: inner is red and outer is yellow).

My question: how do I program the double border (like in the image)?!

Image:

Example Button Image

I tried with an XML file with two strokes, but it doesn't work.

I could do this with a 9png file, but I want to do it with pure coding.

like image 367
Dancger Avatar asked Oct 17 '13 12:10

Dancger


2 Answers

btn_bg.xml

 <?xml version="1.0" encoding="utf-8"?>
 <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item > 
    <shape android:shape="rectangle">
                
       <padding android:left="3.5px"
            android:top="3.5px"
            android:right="3.5px"
            android:bottom="3.5px"/>
                    
       <solid android:color="#d4e23a"/>
                
    </shape>
</item>
<item > 
    <shape android:shape="rectangle">
                
       <padding android:left="4.5px"
            android:top="4.5px"
            android:right="4.5px"
            android:bottom="4.5px"/>
                    
        <solid android:color="#d4413a"/>
            
    </shape>
</item>
<item > 
    <shape android:shape="rectangle">
        <gradient android:startColor="#37c325"
            android:endColor="#2573c3"
            android:angle="-90"/>
                    
    </shape>
</item>


</layer-list>

set the above xml as button background.

           <Button
                
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=""
                android:background="@drawable/btn_bg"
                android:gravity="center"
                android:padding="10dp"
                android:textStyle="bold"
                >
                           
           </Button>

Result:

Image

like image 186
TheFlash Avatar answered Oct 16 '22 22:10

TheFlash


If you want to go by plain Java code then you need to create a class which extends the button, write all your logic in

public void onDraw(Canvas iCanvas).

I have pasted small code snippet from one of my project. Give it a try. thought I have not created the gradient, I have used plain colors.

public class MyButton extends Button {

    private Paint m_paint1 = new Paint();
    private Paint m_paint2 = new Paint();
    private int m_color1 = 0XFF92C84D; // LIKE AN OLIVE GREEN..
    private int m_color2 = 0XFFFF0000; // LIKE AN OLIVE GREEN..

    private RectF innerRect1, innerRect2;

    public MyButton(Context context) {
        super(context);
        setBackgroundColor(Color.BLACK);

    }

    public void onDraw(Canvas iCanvas) {
        // draw the button background
        m_paint1.setColor(m_color1);
        m_paint2.setColor(m_color2);
        innerRect1 = new RectF(5, 5, getWidth() - 5, getHeight() - 5);
        innerRect2 = new RectF(10, 10, getWidth() - 10, getHeight() - 10);
        iCanvas.drawRoundRect(innerRect1, 0, 0, m_paint1);
        iCanvas.drawRoundRect(innerRect2, 0, 0, m_paint2);
    }

    public static RelativeLayout.LayoutParams GetRelativeParam(int iLeft,
            int iTop, int iWidth, int iHeight) {
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                iHeight, iWidth);
        params.leftMargin = iLeft;
        params.topMargin = iTop;
        return params;
    }
}

and

RelativeLayout relLay = new RelativeLayout(this);

        MyButton m_button = new MyButton(this);
        setContentView(relLay);

        relLay.addView(m_button, MyButton.GetRelativeParam(0, 0, 100, 500));
like image 32
Hardik Trivedi Avatar answered Oct 16 '22 23:10

Hardik Trivedi