Lets start with a creating a custom button class that extends the android Button class. Name it as InvertedButton.java.
You will need to override it's onDraw() method to rotate the canvas, so that before any drawing is done, you rotate it by 180 degrees. To make the text-alignment perfect, you will also need to do a few calculations.
@OverrideThere you go. You can now add this button anywhere in your XML layouts and you will always get an inverted button.
protected void onDraw(Canvas canvas) {
int left = getPaddingLeft();
int top = getPaddingTop();
int right = getPaddingRight();
int bottom = getPaddingBottom();
int width = getWidth() - left - right;
int height = getHeight() - top - bottom;
int saveCount = canvas.getSaveCount();
canvas.translate(left + width / 2, top + height / 2);
canvas.rotate(-180);
canvas.translate((-width / 2)-left, (-height / 2)-top);
canvas.restoreToCount(saveCount);
super.onDraw(canvas);
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:text="Button" android:id="@+id/button1"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|top" android:textStyle="bold"></Button>
<com.beanie.examples.invertedbutton.InvertedButton
android:text="Button" android:id="@+id/button2" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_gravity="center_horizontal|bottom"
android:textStyle="bold"></com.beanie.examples.invertedbutton.InvertedButton>
</LinearLayout>
You can find the source code here. Happy coding!!!!
ok thanks
ReplyDeleteyou're my hero. Thanks
ReplyDelete