2
votes

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.

1
how you set background color? and in which control you set? and can you provide that code here? - Pratik
If you need focus, click or longclick behaviors, you'll need a selector… if that is the case, I'll try to provide an example later this afternoon. - Renaud
I get the transparent btn width/height before draw and set that to textview and the color to it in my onCreate method. I have added that code above. - amsiddh
@Renaud The btn click/selector img is not a probl. the main thing is dynamically to set its background color properly. - amsiddh
@BlackDevil - No i don't need. The dynamic way of getting btn size dint worked. I have fixed the size of button and background textview and it worked. But it gives alignment issue on 2.1, works fine on 2.2 and above. - amsiddh

1 Answers

2
votes

why don't you use Image Button.!

like this:

  <ImageButton
            android:id="@+id/imageButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/btn_transparent_img"
            android:background="#000000"
     />
  • use imageButton
  • add your transparent image as src
  • change background colour according to your need

Refer this link for further help: