How to programmatically set drawableLeft on Android button?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In Android development, customizing the interface of buttons for a better user experience often involves using drawable resources. One common customization is the placement of drawables (such as icons) on buttons. Specifically, setting a drawable on the left side of a button can enhance user navigation by providing a visual cue. This article will guide you through the process of programmatically setting the drawableLeft attribute on an Android button using Java or Kotlin. We'll cover a detailed technical explanation and provide examples to illustrate the process.
Setting Drawable Programmatically
In Android, buttons are customizable widgets that can display text, images, or both. The drawable attributes (drawableLeft, drawableRight, drawableTop, and drawableBottom) allow you to place icons around the text of the button. When you programmatically set drawableLeft, you directly manipulate the button's drawable properties in your activity or fragment.
Key Methods
The Button widget in Android inherits from the TextView class, which provides several methods for managing drawables:
setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom): Sets drawables to the left, top, right, and bottom of the text.setCompoundDrawablesWithIntrinsicBounds(int leftRes, int topRes, int rightRes, int bottomRes): Uses drawable resources identified by resource IDs, automatically sizing them to their intrinsic dimensions.
Example: Java Implementation
Below is an example showing how to set drawableLeft on a button programmatically using Java:
Example: Kotlin Implementation
For Kotlin users, the syntax is more concise. Here is the equivalent code snippet:
Technical Explanation
ContextCompat
The ContextCompat.getDrawable() method is used to fetch the drawable in a backward-compatible manner. Direct usage of getDrawable() from resources is deprecated in later API levels, thus ContextCompat ensures cross-version compatibility.
Intrinsic Bounds
When using setCompoundDrawablesWithIntrinsicBounds, the drawable does not need explicit width or height because this method uses the intrinsic dimensions of the drawable, ensuring it appears at its intended size.
Null Parameters
Passing null for top, right, and bottom parameters ensures that only the left drawable is set. If those parameters were not null, it would replace any existing drawables at those positions.
Summary Table
| Operation | Java | Kotlin |
| Load Drawable | ContextCompat.getDrawable | ContextCompat.getDrawable |
| Set Drawable to Left | setCompoundDrawablesWithIntrinsicBounds | setCompoundDrawablesWithIntrinsicBounds |
| Other Parameters
:(null, null, null) | "Null indicates no drawable for 
that position, used for other slots." | Same as Java, uses null |
Additional Considerations
- Performance: Be mindful of drawable sizes, as using excessively large images can impact performance.
- Resource Management: Ensure that drawable resources are present in appropriate directories based on screen density requirements (i.e.,
drawable-hdpi,drawable-xhdpi, etc.). - Customization: Customize padding and other styling attributes to ensure visual alignment and appearance fit your app design.
By understanding and applying these techniques, developers can enhance the user interface of Android applications, contributing to intuitive and visually appealing app designs.
Related reading
- How to programmatically set the layout_align_parent_right attribute of a Button in Relative Layout?
- How to programmatically take a screenshot on Android?
- How to provide a localized description with an Error type in Swift?
- How to put a border around an Android TextView?
- How to put a border around an Android TextView?
- How to quit android application programmatically
- How to quit or close single simulator from opened multiple simulator in Xcode 9?
- How to re-sign the ipa file?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.