set transparent button image and background color to button

I have a transparent button image with white border and without label. I want to create a button which will have its label, its background transparent image and want to set a background color to button dynamically.

The transparent button image:
enter image description here

The Button should look like this after setting its button image with label and background color :

enter image description here

I have tried to implement this by using FrameLayout, TextView and Button. As in framelayout the first child will be textview and second will be button. Setting a transparent btn image to button and textview will have label and background color which has to set dynamically. I am almost able to do this but textview size should be little smaller than button image, this thing i have to calculate dynamically. currently textview background color sometimes goes outside the button and also round shape is not coming.

The xml:

<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginBottom="15dip" >

    <TextView
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:textSize="20sp" 
        android:textColor="#FFFFFFFF"
        android:id="@+id/btn_bacground"
        android:gravity="center"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="5dip"
        android:layout_marginRight="1dip"
        android:layout_marginLeft="0dip" />

        <Button 
            android:id="@+id/button_img" 
            android:layout_height="wrap_content" 
            android:layout_width="wrap_content" 
            android:background="@drawable/btn_transparent_img"/>

</FrameLayout>

Edit: Setting the background color, size and label to Textview. background image to Button.

buttonClick = (Button) buttonClickFrame.findViewById(R.id.button_img);
buttonBg = (TextView) buttonClickFrame.findViewById(R.id.btn_bacground);
buttonBg.setBackgroundColor(Color.parseColor(BG_COLOR));
buttonBg.setText("Click");
buttonPhone.setBackgroundDrawable(getResources().getDrawable(R.drawable.btn_transparent_img));
setBackgroundDimentions(buttonClick, buttonBg);
buttonClick.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        onPhoneClick(v);
    }
});

private void setBackgroundDimentions(Button btn, TextView bckView) {
     final Button mBtn = btw;
     final TextView mBckView = bckView;
     ViewTreeObserver vto = mBtn.getViewTreeObserver();
     vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        public boolean onPreDraw() {
             int finalHeight = mBtn.getMeasuredHeight();
             int finalWidth = mBtn.getMeasuredWidth();
             mBckView.setWidth(finalWidth - 14);
             mBckView.setHeight(finalHeight - 15);
             return true;
        }
     });
}

Please suggest me how can i achieve this.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值