tags:

views:

118

answers:

3

Hi,

Can you please tell me how can I place an background image to a the left upper corner of TextView in android? I would like the image not to be scaled by android.

I have tried

Resources res = getResources(); setCompoundDrawables(res.getDrawable(R.drawable.icon48x48_1), null, null, null);

Nothing is shown.

And I have tried setBackground(R.drawable.icon48x48_1);

But it stretches the image.

Thank you for any help.

+1  A: 

Have you tried setting the DrawableLeft in you xml layout of the textview?

android:drawableLeft="@+id/img">
sadboy
Yeah, but it tires to center the drawable vertically in my TextView. I want it to layout in the top left corner.
hap497
A: 

You could always make your new background image a 9-patch and put the stretchable areas on the right and bottom in a transparent area.

fiXedd
A: 

This is really a dirty hack so use it only as your last resource.

If this is what you want

alt text

you can do something like

    final TextView tv = (TextView) findViewById(R.id.TextView01);
    tv.setTransformationMethod(new TransformationMethod() {
        private static final String INDENT = "       ";

        @Override
        public void onFocusChanged(View view, CharSequence sourceText,
                boolean focused, int direction, Rect previouslyFocusedRect) {
            // TODO Auto-generated method stub

        }

        @Override
        public CharSequence getTransformation(CharSequence source, View view) {
            return INDENT + source;
        }
    });

using a FrameLayout containing both the ImageView and TextView. Notice that if the icon spans more than one line it's not going to work.

dtmilano