positioning a compoundDrawable within TextView?

I am setting a compound drawable to a TextView like so

txtView1.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon, 0, 0,0);

The drawable is drawn to the left most position of the TextView. However I want to move it a little bit so that it doesn’t sit on the left border of the TextView but it’s like 10dip inside. I did some padding by using:

  • Detect android webview URL location change
  • Is monodroid slower?
  • Is there a way to automatically update application on Android?
  • Notification deleteIntent does not work
  • What is the purpose of Applicaton class in android
  • error: The requested URL returned error: 403 Forbidden while accessing
  • android:paddingLeft="10dip"
    

    But this pushes the text to move along with the compound drawable to the right by 10dip.

    How do I move only the compound drawable by 10dip without affecting the text ?

    Related posts:

    Immutable bitmap crash error
    How to add source of 'Android Private Libraries' to ADT eclipse?
    My views are being reset on orientation change
    How to display input errors in popup?
    Android Enable Zoom and Pinch Controls in Webview
    Android - switch to activity without restarting it
  • how do I add a hyperlink in sms message in android phone?
  • Java.lang.ClassNotFoundException: net.sourceforge.jtds.jdbc.Driver On Android
  • Get checksum of the source codes in Android library
  • Dynamically Generating Dalvik Bytecode into a running Dalvik/Android application
  • How to implement the deprecated methods of Notification
  • SQLite rawQuery selectionArgs and Integers Fields
  • 3 Solutions collect form web for “positioning a compoundDrawable within TextView?”

    Its very simple. Just add the following piece code to your TextView

    android:drawablePadding="10dip"
    

    This adds padding between your text and the image in the TextView.

    If you want to do it dynamically, you can use the method:

    txtView1.setCompoundDrawablePadding (int pad);
    

    Quick fix – txtView1.setText(" "+<your text>);

    You can put 1/2 spaces.. or best is to extend textview and modify it the way you want. 🙂

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.