Reputation: 3563
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:
The Button should look like this after setting its button image with label and background color :
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.
Upvotes: 2
Views: 3173
Reputation: 5152
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"
/>
Refer this link for further help:
Upvotes: 2