Showing posts with label Drawable. Show all posts
Showing posts with label Drawable. Show all posts
How to Declare and Set a Drawable to an Integer object
1. Add a drawable to your project named myicon.
2. In the MainActivity.java file, add the below line to the onCreate method.
int myIcon = R.drawable.myicon;
3. Compile and run!
Next Recommended Article:
setImageDrawable - Set a Drawable to an ImageView
setImageDrawable() was added in API Level 1
Sets a drawable as the content of this ImageView.
1. Set a Drawable Object Variable with a drawable named myDrawable.
2. Find an ImageView using ID, named myImageView.
3. In the MainActivity.java file, add the following code to the onCreate method. This will set the Drawable to the ImageView.
myImageView.setImageDrawable(myDrawable);
4. Compile and run!
Resources:
http://developer.android.com/reference/android/widget/ImageView.html#setImageDrawable(android.graphics.drawable.Drawable)
Sets a drawable as the content of this ImageView.
1. Set a Drawable Object Variable with a drawable named myDrawable.
2. Find an ImageView using ID, named myImageView.
3. In the MainActivity.java file, add the following code to the onCreate method. This will set the Drawable to the ImageView.
myImageView.setImageDrawable(myDrawable);
4. Compile and run!
Resources:
http://developer.android.com/reference/android/widget/ImageView.html#setImageDrawable(android.graphics.drawable.Drawable)
getDrawable - Set a Drawable Object Variable
Drawable was added in API Level 1
1. Create an Drawable Variable named myDrawable.
2. In the MainActivity.java file, add the following line. This will get an instance of a Drawable. This will set the myDrawable to the drawable named R.drawable.myImage.
try {
myDrawable = getResources().getDrawable(R.drawable.myImage);
} catch { Resources.NotFoundException e)
e.printStackTrace();
}
3. Compile and run!
Next Recommended Article: Set a Drawable to an ImageView
Resources:
developer.android.com/reference/android/content/Context.html#getResources()
http://developer.android.com/reference/android/content/res/Resources.html#getDrawable(int)
1. Create an Drawable Variable named myDrawable.
2. In the MainActivity.java file, add the following line. This will get an instance of a Drawable. This will set the myDrawable to the drawable named R.drawable.myImage.
try {
myDrawable = getResources().getDrawable(R.drawable.myImage);
} catch { Resources.NotFoundException e)
e.printStackTrace();
}
3. Compile and run!
Next Recommended Article: Set a Drawable to an ImageView
Resources:
developer.android.com/reference/android/content/Context.html#getResources()
http://developer.android.com/reference/android/content/res/Resources.html#getDrawable(int)
Create a Drawable Object Variable
This will declare and name an Drawable object variable
Drawable was added in API Level 1
A Drawable is a general abstraction for "something that can be drawn." Most often you will deal with Drawable as the type of resource retrieved for drawing things to the screen; the Drawable class provides a generic API for dealing with an underlying visual resource that may take a variety of forms. Unlike a View, a Drawable does not have any facility to receive events or otherwise interact with the user.
1. Create an Android project, if you don't already have one.
Next Recommended Article: Set a Drawable
Drawable was added in API Level 1
A Drawable is a general abstraction for "something that can be drawn." Most often you will deal with Drawable as the type of resource retrieved for drawing things to the screen; the Drawable class provides a generic API for dealing with an underlying visual resource that may take a variety of forms. Unlike a View, a Drawable does not have any facility to receive events or otherwise interact with the user.
1. Create an Android project, if you don't already have one.
2.In the MainActivity.java file, add the code below to the onCreate method. This will create a Drawable named myDrawable.
Drawable myDrawable;
3. Compile and run!
Resources:
http://developer.android.com/reference/android/graphics/drawable/Drawable.html
http://developer.android.com/guide/topics/resources/drawable-resource.html
http://developer.android.com/guide/topics/resources/drawable-resource.html
setCompoundDrawablesWithIntrinsicBounds/android:drawableTop - Add a Drawable Above the Text in a TextView, in Java
setCompoundDrawablesWithIntrinsicBounds - How to Add a Drawable Above the Text in a TextView, in Java
setCompoundDrawablesWithIntrinsicBounds was added in API Level 3
Related XML Attribute: android:drawableTop
setCompoundDrawablesWithIntrinsicBounds (int left, int top, int right, int bottom)
Sets the Drawables (if any) to appear to the left of, above, to the right of, and below the text. Use 0 if you do not want a Drawable there. The Drawables' bounds will be set to their intrinsic bounds.
The drawable (image, picture, etc) will be displayed above the text in a TextView.
Related XML Attribute: android:drawableTop
setCompoundDrawablesWithIntrinsicBounds (int left, int top, int right, int bottom)
Sets the Drawables (if any) to appear to the left of, above, to the right of, and below the text. Use 0 if you do not want a Drawable there. The Drawables' bounds will be set to their intrinsic bounds.
The drawable (image, picture, etc) will be displayed above the text in a TextView.
1. Ensure to change the minSDKVersion to 3, or greater.
2. Add a drawable named myImage
6. Add the code below to the onCreate method.
myTextView.setCompoundDrawablesWithIntrinsicBounds(0,R.drawable.myImage,0,0);
7. At this point, the code will work. But to easier see the defines perimeter of the TextView, let's change the background color of the TextView to Gray. Hex: #808080
8. Compile and run!
![]() |
| This is using android:layout = "wrap_content" |
9. You can also change the android:layout_width to match_parent, and you get the result below.
![]() |
| This is using android:layout = "match_parent" |
Related Articles:
- android:drawableTop - Set a Drawable Above Text, in a TextView, in XML
- android:drawableRight - Set a Drawable to the right of the text, in TextView, in XML
- android:drawableBottom - Set an Image to Background of a TextView, using drawableBottom
- android:drawableLeft - Set a Drawable to the left of text, in a TextView, in XML
android:drawableStart - Set a Drawable at start of Text, in a TextView, in XML
How to Set a Drawable at the start of Text, in a TextView, in XML
Set a Drawable at start of Text, in a TextView, in XMLandroid:drawableEnd was added in API Level 14
Related Method: setCompoundDrawablesRelativeWithIntrinsicBounds- Add a Drawable to the Start of Text, in a TextView, in Java
The drawable to be drawn to the start of the text.
1. Add a TextView.
2. Add a drawable named myImage.
3. Ensure to change the minSDKVersion to 14, or greater.
<TextView
....
android:drawableStart = "@drawable/myImage"
/>
5. Compile and run!
Related Articles
- setCompoundDrawablesRelativeWithIntrinsicBounds - Add a Drawable to the Start of Text, in a TextView, in Java
- setCompoundDrawablesWithIntrinsicBounds - Add a Drawable Below a TextView
- Set an Image to Background of a TextView, using background
http://developer.android.com/reference/android/widget/TextView.html#attr_android:drawableStart
http://developer.android.com/reference/android/R.attr.html#drawableStart
setCompoundDrawablesWithIntrinsicBounds/android:drawableRight - Add a Drawable to the Right of the text in a TextView, in Java
setCompoundDrawablesWithIntrinsicBounds - How to Add a Drawable to the Right of the text in a TextView, in Java
setCompoundDrawablesWithIntrinsicBounds was added in API Level 3
Related XML Attribute: android:drawableRight
setCompoundDrawablesWithIntrinsicBounds (int left, int top, int right, int bottom)
Sets the Drawables (if any) to appear to the left of, above, to the right of, and below the text. Use 0 if you do not want a Drawable there. The Drawables' bounds will be set to their intrinsic bounds.
The drawable (image, picture, etc) will be displayed to the right of the text
Related XML Attribute: android:drawableRight
setCompoundDrawablesWithIntrinsicBounds (int left, int top, int right, int bottom)
Sets the Drawables (if any) to appear to the left of, above, to the right of, and below the text. Use 0 if you do not want a Drawable there. The Drawables' bounds will be set to their intrinsic bounds.
The drawable (image, picture, etc) will be displayed to the right of the text
1. Ensure to change the minSDKVersion to 3, or greater.
2. Add a drawable named myImage
6. Add the code below to the onCreate method.
myTextView.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.myImage,0);
7. Compile and run!
![]() |
| This is using android:layout_width = "wrap_content" |
8. You can also change the android:layout_width to match_parent, and you get the result below.
![]() |
| This is using android:layout_width = "match_parent" |
Related Articles:
android:drawablePadding - Add Padding to a Drawable in a TextView, in XML
How to Add Padding to a Drawable in a TextView, in XML
android:drawablePadding was added in API Level 1Related Method: setCompoundDrawablePadding - Add Padding to a Drawable in a TextView, using Java
1. Add a TextView in your main.xml file.
2. Add the following text to the TextView: "This is an example with a drawable set to the left in a TextView."
3. Add a drawable to the TextView.
Examples include:
android:drawableBottom, android:drawableEnd, android:drawableLeft, android:drawableRight.
(My example will use android:drawableLeft)
4. In the main.xml file, add the code below.
<TextView
...
android:drawablePadding = "15dip"
/>
5. Compile and run!
6. You can compare the image above to without android:drawablePadding here, using android:drawableLeft.
Related Articles:
Resources:
setCompoundDrawablesWithIntrinsicBounds/android:drawableLeft - Add a Drawable to the Left of the text in a TextView, in Java
setCompoundDrawablesWithIntrinsicBounds - How to Add a Drawable to the Left of the text in a TextView, in Java
setCompoundDrawablesWithIntrinsicBounds was added in API Level 3
Related XML Attribute: android:drawableLeft
setCompoundDrawablesWithIntrinsicBounds (int left, int top, int right, int bottom)
Sets the Drawables (if any) to appear to the left of, above, to the right of, and below the text. Use 0 if you do not want a Drawable there. The Drawables' bounds will be set to their intrinsic bounds.
The drawable (image, picture, etc) will be displayed to the left of the text
Related XML Attribute: android:drawableLeft
setCompoundDrawablesWithIntrinsicBounds (int left, int top, int right, int bottom)
Sets the Drawables (if any) to appear to the left of, above, to the right of, and below the text. Use 0 if you do not want a Drawable there. The Drawables' bounds will be set to their intrinsic bounds.
The drawable (image, picture, etc) will be displayed to the left of the text
1. Ensure to change the minSDKVersion to 3, or greater.
2. Add the following text to the TextView: "This is an example with a drawable set to the left in a TextView."
5. Add the code below to the onCreate method.
myTextView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.myImage,0,0,0);
6. Compile and run!
Related Articles:
android:drawableLeft - Set a Drawable to the left of text, in a TextView, in XML
How to Set a Drawable to the left of the text, in TextView, in XML
android:drawableLeft was added in API Level 1Related Method: setCompoundDrawablesWithIntrinsicBounds - Add a Drawable to the Left of the text in a TextView, in Java
The drawable to be drawn to the left of the text.
1. Add a TextView.
2. Add the following text to the TextView: "This is an example with a drawable set to the left in a TextView."
3. Add a drawable named myImage.
4. In the main.xml file, add the below line
<TextView
....
android:drawableLeft = "@drawable/myImage"
/>
5. Compile and run!
Related Articles
- setCompoundDrawablesWithIntrinsicBounds - Add a Drawable Below a TextView
- Set an Image to Background of a TextView, using background
http://developer.android.com/reference/android/widget/TextView.html#attr_android:drawableLeft
http://developer.android.com/reference/android/R.attr.html#drawableLeft
setCompoundDrawablesRelativeWithIntrinsicBounds/android:drawableEnd - Add a Drawable to the End of Text, in a TextView, in Java
setCompoundDrawablesRelativeWithIntrinsicBounds - How to Add a Drawable to the end of Text, in a TextView, in Java
setCompoundDrawablesRelativeWithIntrinsicBounds was added in API Level 17
Related XML Attribute: android:drawableEnd
setCompoundDrawablesRelativeWithIntrinsicBounds (int start, int top, int end, int bottom)Sets the Drawables (if any) to appear to the start of, above, to the end of, and below the text. Use 0 if you do not want a Drawable there. The Drawables' bounds will be set to their intrinsic bounds.
Related XML Attribute: android:drawableEnd
setCompoundDrawablesRelativeWithIntrinsicBounds (int start, int top, int end, int bottom)Sets the Drawables (if any) to appear to the start of, above, to the end of, and below the text. Use 0 if you do not want a Drawable there. The Drawables' bounds will be set to their intrinsic bounds.
1. Ensure to change the minSDKVersion to 17, or greater.
4. Add the code below to the onCreate method. This will set a drawable to be drawn to the end of the text.
- myTextView.setCompoundDrawablesRelativeWithIntrinsicBounds(0,0,R.drawable.myImage,0);
Related Articles:
android:drawableEnd - Set drawable to the end of the text, in TextView, in XML
Set drawable to the end of the text, in TextView, in XML
android:drawableEnd was added in API Level 14Related Method:
setCompoundDrawablesRelativeWithIntrinsicBounds - Add a Drawable to the End of Text, in a TextView, in Java
The drawable to be drawn to the end of the text.
1. Add a TextView.
2. Add a drawable named myImage.
3. Ensure to change the minSDKVersion to 14, or greater.
<TextView
....
android:drawableEnd = "@drawable/myImage"
/>
5. Compile and run!
Related Articles
- setCompoundDrawablesWithIntrinsicBounds - Add a Drawable Below a TextView
- Set an Image to Background of a TextView, using background
developer.android.com/reference/android/widget/TextView.html#attr_android:drawableEnd
http://developer.android.com/reference/android/R.attr.html#drawableEnd
setCompoundDrawablesWithIntrinsicBounds/android:drawableBottom - Add a Drawable Below a TextView
setCompoundDrawablesWithIntrinsicBounds - How to Add a Drawable Below a TextView
setCompoundDrawablesWithIntrinsicBounds was added in API Level 3
Related XML Attribute: android:drawableBottom
setCompoundDrawablesWithIntrinsicBounds (int left, int top, int right, int bottom)
Sets the Drawables (if any) to appear to the left of, above, to the right of, and below the text. Use 0 if you do not want a Drawable there. The Drawables' bounds will be set to their intrinsic bounds.
The drawable (image, picture, etc) will be displayed centered, below the text
Related XML Attribute: android:drawableBottom
setCompoundDrawablesWithIntrinsicBounds (int left, int top, int right, int bottom)
Sets the Drawables (if any) to appear to the left of, above, to the right of, and below the text. Use 0 if you do not want a Drawable there. The Drawables' bounds will be set to their intrinsic bounds.
The drawable (image, picture, etc) will be displayed centered, below the text
1. Ensure to change the minSDKVersion to 3, or greater.
4. Add the code below to the onCreate method.
myTextView.setCompoundDrawablesWithIntrinsicBounds(0,0,0,R.drawable.myImage);
5. Compile and run!
Related Articles:
Add a Drawable to your Project
How to Add a Drawable to your Project
A drawable resource is a general concept for a graphic that can be drawn to the screenThere are several types of drawables Choose a type from below you'd like to add to your project.
Bitmap File, Nine-Patch File, Layer List, State List, Level List, Transition Drawable, Inset Drawable, Clip Drawable, Scale Drawable, and Shape Drawable.
Related Articles:
Resources:
http://developer.android.com/guide/topics/resources/drawable-resource.html
Add a Bitmap Drawable to your Project
How to Add a Bitmap Drawable to your Project
A bitmap graphic file (.png, .jpg, or .gif).1. Add the bitmap graphic file (.png, .jpg, or .gif) to the res/drawable directory. If the res/drawable directory does not exist, create a drawable directory.
example: mypic.png
2. The file will now be available for use in your project.
Previous Recommended Article: Add a Drawable to your Project
Resources:
http://developer.android.com/guide/topics/resources/drawable-resource.html
android:drawableBottom - Set Drawable below text, in a TextView, in XML
How to Set a Drawable below Text, in a TextView, in XML
android:drawableBottom was added in API Level 1Related Method: setCompoundDrawablesWithIntrinsicBounds - Add a Drawable Below a TextView
The drawable to be drawn below the text.
1. Add a TextView.
2. Add a drawable named myImage.
3. In the main.xml file, add the below line
<TextView
....
android:drawableBottom = "@drawable/myImage"
/>
4. Compile and run!
Related Articles
- setCompoundDrawablesWithIntrinsicBounds - Add a Drawable Below a TextView
- Set an Image to Background of a TextView, using background
http://developer.android.com/reference/android/widget/TextView.html#attr_android:drawableBottom
http://developer.android.com/reference/android/R.attr.html#drawableBottom
Subscribe to:
Posts (Atom)
Popular Posts
- Solution: Running Android Lint has encountered a problem Failed
- Perform an Action after Enter keypress on EditText
- How to Declare a URLConnection object
- How to Get a new HttpURLConnection
- How to Create a ProGuard Exception
- How to Get a new URL Connection
- How to Reference a Library in your Project
- How to Register an Activity that Responds to NFC tags for URI's that point to a URL
- How to Import a Library
- How to Open the SDK Manager






