7

I've been working on my android calculator and i can't seem to insert an image instead of text on a button. For example sqrt I don't want to have sqrt written on my button i want the image of the symbol on it, the same goes for x^y and alot of others. I have my custom background of the button working just fine. Thank you for your help in advance :)

EDIT: Thats not what i wanted i did manage to get my custom button and thats fine. On my button since its a calculator button i have android:text on it saying sqrt. I want to remove that and insert another png on my button showing the symbol of the square root instead of that text. Thank you for quick responses.

1
  • 1
    So change from Button to ImageButton and you can set background or src attribute to your image Commented Oct 18, 2012 at 10:33

5 Answers 5

6
<Button 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:drawableTop="@drawable/imageName"/>

You can have both text and image (or no text) when using the attribute drawableTop, drawableBottom, drawableLeft and drawableRight.

And for positioning the image how you want, consider using: paddingTop, paddingBottom, paddingLeft and paddingRight.

Sign up to request clarification or add additional context in comments.

1 Comment

If i use that the symbol will be not in the center :(
1

You can do this by ImageButton:

        <ImageButton
            android:id="@+id/submitEmailButton"
            android:layout_height="wrap_content" android:layout_width="wrap_content"
            android:layout_gravity="right" android:layout_marginRight="15dip"
            android:background="@drawable/ic_submit" />

Here ic_submit is the image you want to show as a button.

Comments

0

I used this tutorial and it worked perfectly for me: http://www.mkyong.com/android/android-imagebutton-selector-example/

I haven't seen your code but basically you should remove the "text" tag from your xml and it should appear the image without text then.

I don't know if that's exactly what you were looking for or not, but I reccomend you to follow the tutorial and for sure it will work for you ;)

Comments

0

There are a number of alternatives you can try to implement to build your SQRT button:

  • draw the button as a 9-patch drawable so your SQRT symbol won't get distorted;
  • use a RelativeLayout with a button inside and implement a selector

Comments

0

You can simply set background for the Button and keep its text blank.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.