Adding a text and image on a button programmatically in android
I am trying to have an text and image on a button appear dynamically - the text needs to appear on the left and the image to appear on the right. To give an example, here is an image I would like to get.
After searching through a few other examples on StackOverflow, tried the following code in my activity but it doesnt seem to be working. Can anyone please suggest what I am doing wrong ?
Code :
btn.setTextColor(Color.parseColor("#000000"));
btn.setText("SomeText");
btn.setTextSize(TypedValue.COMPLEX_UNIT_PX, mediumTextSize);
Drawable icon= getApplicationContext().getResources().getDrawable( R.drawable.cal);
icon.setBounds(0, 0, 0, 0); //Left,Top,Right,Bottom
btn.setCompoundDrawablesWithIntrinsicBounds( null, null, icon, null );
android android-layout android-button
|
show 4 more comments
I am trying to have an text and image on a button appear dynamically - the text needs to appear on the left and the image to appear on the right. To give an example, here is an image I would like to get.
After searching through a few other examples on StackOverflow, tried the following code in my activity but it doesnt seem to be working. Can anyone please suggest what I am doing wrong ?
Code :
btn.setTextColor(Color.parseColor("#000000"));
btn.setText("SomeText");
btn.setTextSize(TypedValue.COMPLEX_UNIT_PX, mediumTextSize);
Drawable icon= getApplicationContext().getResources().getDrawable( R.drawable.cal);
icon.setBounds(0, 0, 0, 0); //Left,Top,Right,Bottom
btn.setCompoundDrawablesWithIntrinsicBounds( null, null, icon, null );
android android-layout android-button
Do you want to set image on button ?
– Code Kadiya
Dec 29 '18 at 18:41
I suggest to useImageButton
– Code Kadiya
Dec 29 '18 at 18:42
Else you can createLinearLayout
and apply style
– Code Kadiya
Dec 29 '18 at 18:44
How about leaving out the two lines before the last one and just usingbtn.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.cal, 0);
?
– 0X0nosugar
Dec 29 '18 at 18:48
1
The linked version of the method will take int values (resource identifiers)
– 0X0nosugar
Dec 29 '18 at 19:08
|
show 4 more comments
I am trying to have an text and image on a button appear dynamically - the text needs to appear on the left and the image to appear on the right. To give an example, here is an image I would like to get.
After searching through a few other examples on StackOverflow, tried the following code in my activity but it doesnt seem to be working. Can anyone please suggest what I am doing wrong ?
Code :
btn.setTextColor(Color.parseColor("#000000"));
btn.setText("SomeText");
btn.setTextSize(TypedValue.COMPLEX_UNIT_PX, mediumTextSize);
Drawable icon= getApplicationContext().getResources().getDrawable( R.drawable.cal);
icon.setBounds(0, 0, 0, 0); //Left,Top,Right,Bottom
btn.setCompoundDrawablesWithIntrinsicBounds( null, null, icon, null );
android android-layout android-button
I am trying to have an text and image on a button appear dynamically - the text needs to appear on the left and the image to appear on the right. To give an example, here is an image I would like to get.
After searching through a few other examples on StackOverflow, tried the following code in my activity but it doesnt seem to be working. Can anyone please suggest what I am doing wrong ?
Code :
btn.setTextColor(Color.parseColor("#000000"));
btn.setText("SomeText");
btn.setTextSize(TypedValue.COMPLEX_UNIT_PX, mediumTextSize);
Drawable icon= getApplicationContext().getResources().getDrawable( R.drawable.cal);
icon.setBounds(0, 0, 0, 0); //Left,Top,Right,Bottom
btn.setCompoundDrawablesWithIntrinsicBounds( null, null, icon, null );
android android-layout android-button
android android-layout android-button
asked Dec 29 '18 at 18:37
PrashanthPrashanth
1,07911018
1,07911018
Do you want to set image on button ?
– Code Kadiya
Dec 29 '18 at 18:41
I suggest to useImageButton
– Code Kadiya
Dec 29 '18 at 18:42
Else you can createLinearLayout
and apply style
– Code Kadiya
Dec 29 '18 at 18:44
How about leaving out the two lines before the last one and just usingbtn.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.cal, 0);
?
– 0X0nosugar
Dec 29 '18 at 18:48
1
The linked version of the method will take int values (resource identifiers)
– 0X0nosugar
Dec 29 '18 at 19:08
|
show 4 more comments
Do you want to set image on button ?
– Code Kadiya
Dec 29 '18 at 18:41
I suggest to useImageButton
– Code Kadiya
Dec 29 '18 at 18:42
Else you can createLinearLayout
and apply style
– Code Kadiya
Dec 29 '18 at 18:44
How about leaving out the two lines before the last one and just usingbtn.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.cal, 0);
?
– 0X0nosugar
Dec 29 '18 at 18:48
1
The linked version of the method will take int values (resource identifiers)
– 0X0nosugar
Dec 29 '18 at 19:08
Do you want to set image on button ?
– Code Kadiya
Dec 29 '18 at 18:41
Do you want to set image on button ?
– Code Kadiya
Dec 29 '18 at 18:41
I suggest to use
ImageButton
– Code Kadiya
Dec 29 '18 at 18:42
I suggest to use
ImageButton
– Code Kadiya
Dec 29 '18 at 18:42
Else you can create
LinearLayout
and apply style– Code Kadiya
Dec 29 '18 at 18:44
Else you can create
LinearLayout
and apply style– Code Kadiya
Dec 29 '18 at 18:44
How about leaving out the two lines before the last one and just using
btn.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.cal, 0);
?– 0X0nosugar
Dec 29 '18 at 18:48
How about leaving out the two lines before the last one and just using
btn.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.cal, 0);
?– 0X0nosugar
Dec 29 '18 at 18:48
1
1
The linked version of the method will take int values (resource identifiers)
– 0X0nosugar
Dec 29 '18 at 19:08
The linked version of the method will take int values (resource identifiers)
– 0X0nosugar
Dec 29 '18 at 19:08
|
show 4 more comments
2 Answers
2
active
oldest
votes
I noticed that the size of the image is very important when using it on other objects - when I adjusted the image to a smaller size, my code worked fine....
The same code that I was trying earlier worked for me after resizing the image...
Button btn = new Button(this);
btn.setTextColor(Color.parseColor("#000000"));
btn.setText("SomeText");
btn.setTextSize(TypedValue.COMPLEX_UNIT_PX, mediumTextSize);
btn.setGravity(Gravity.CENTER | Gravity.LEFT);
Drawable icon= getApplicationContext().getResources().getDrawable(R.drawable.cal);
btn.setBackgroundResource(R.drawable.light_bg);
icon.setBounds(0, 0, 0, 0); //Left,Top,Right,Bottom
btn.setCompoundDrawablesWithIntrinsicBounds( null, null, icon, null);
Thank you all for your help and suggestions !
add a comment |
Try this
btn.setText("Some Text");
btn.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
btn.setGravity(Gravity.CENTER | Gravity.LEFT);
Drawable myDrawable =
getResources().getDrawable(R.drawable.ic_cancel_black_24dp);
btn.setCompoundDrawablesWithIntrinsicBounds(null,null,myDrawable,null);
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53972324%2fadding-a-text-and-image-on-a-button-programmatically-in-android%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I noticed that the size of the image is very important when using it on other objects - when I adjusted the image to a smaller size, my code worked fine....
The same code that I was trying earlier worked for me after resizing the image...
Button btn = new Button(this);
btn.setTextColor(Color.parseColor("#000000"));
btn.setText("SomeText");
btn.setTextSize(TypedValue.COMPLEX_UNIT_PX, mediumTextSize);
btn.setGravity(Gravity.CENTER | Gravity.LEFT);
Drawable icon= getApplicationContext().getResources().getDrawable(R.drawable.cal);
btn.setBackgroundResource(R.drawable.light_bg);
icon.setBounds(0, 0, 0, 0); //Left,Top,Right,Bottom
btn.setCompoundDrawablesWithIntrinsicBounds( null, null, icon, null);
Thank you all for your help and suggestions !
add a comment |
I noticed that the size of the image is very important when using it on other objects - when I adjusted the image to a smaller size, my code worked fine....
The same code that I was trying earlier worked for me after resizing the image...
Button btn = new Button(this);
btn.setTextColor(Color.parseColor("#000000"));
btn.setText("SomeText");
btn.setTextSize(TypedValue.COMPLEX_UNIT_PX, mediumTextSize);
btn.setGravity(Gravity.CENTER | Gravity.LEFT);
Drawable icon= getApplicationContext().getResources().getDrawable(R.drawable.cal);
btn.setBackgroundResource(R.drawable.light_bg);
icon.setBounds(0, 0, 0, 0); //Left,Top,Right,Bottom
btn.setCompoundDrawablesWithIntrinsicBounds( null, null, icon, null);
Thank you all for your help and suggestions !
add a comment |
I noticed that the size of the image is very important when using it on other objects - when I adjusted the image to a smaller size, my code worked fine....
The same code that I was trying earlier worked for me after resizing the image...
Button btn = new Button(this);
btn.setTextColor(Color.parseColor("#000000"));
btn.setText("SomeText");
btn.setTextSize(TypedValue.COMPLEX_UNIT_PX, mediumTextSize);
btn.setGravity(Gravity.CENTER | Gravity.LEFT);
Drawable icon= getApplicationContext().getResources().getDrawable(R.drawable.cal);
btn.setBackgroundResource(R.drawable.light_bg);
icon.setBounds(0, 0, 0, 0); //Left,Top,Right,Bottom
btn.setCompoundDrawablesWithIntrinsicBounds( null, null, icon, null);
Thank you all for your help and suggestions !
I noticed that the size of the image is very important when using it on other objects - when I adjusted the image to a smaller size, my code worked fine....
The same code that I was trying earlier worked for me after resizing the image...
Button btn = new Button(this);
btn.setTextColor(Color.parseColor("#000000"));
btn.setText("SomeText");
btn.setTextSize(TypedValue.COMPLEX_UNIT_PX, mediumTextSize);
btn.setGravity(Gravity.CENTER | Gravity.LEFT);
Drawable icon= getApplicationContext().getResources().getDrawable(R.drawable.cal);
btn.setBackgroundResource(R.drawable.light_bg);
icon.setBounds(0, 0, 0, 0); //Left,Top,Right,Bottom
btn.setCompoundDrawablesWithIntrinsicBounds( null, null, icon, null);
Thank you all for your help and suggestions !
answered Dec 29 '18 at 20:36
PrashanthPrashanth
1,07911018
1,07911018
add a comment |
add a comment |
Try this
btn.setText("Some Text");
btn.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
btn.setGravity(Gravity.CENTER | Gravity.LEFT);
Drawable myDrawable =
getResources().getDrawable(R.drawable.ic_cancel_black_24dp);
btn.setCompoundDrawablesWithIntrinsicBounds(null,null,myDrawable,null);
add a comment |
Try this
btn.setText("Some Text");
btn.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
btn.setGravity(Gravity.CENTER | Gravity.LEFT);
Drawable myDrawable =
getResources().getDrawable(R.drawable.ic_cancel_black_24dp);
btn.setCompoundDrawablesWithIntrinsicBounds(null,null,myDrawable,null);
add a comment |
Try this
btn.setText("Some Text");
btn.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
btn.setGravity(Gravity.CENTER | Gravity.LEFT);
Drawable myDrawable =
getResources().getDrawable(R.drawable.ic_cancel_black_24dp);
btn.setCompoundDrawablesWithIntrinsicBounds(null,null,myDrawable,null);
Try this
btn.setText("Some Text");
btn.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
btn.setGravity(Gravity.CENTER | Gravity.LEFT);
Drawable myDrawable =
getResources().getDrawable(R.drawable.ic_cancel_black_24dp);
btn.setCompoundDrawablesWithIntrinsicBounds(null,null,myDrawable,null);
answered Jan 2 at 5:19
Android GeekAndroid Geek
4,4791823
4,4791823
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53972324%2fadding-a-text-and-image-on-a-button-programmatically-in-android%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Do you want to set image on button ?
– Code Kadiya
Dec 29 '18 at 18:41
I suggest to use
ImageButton
– Code Kadiya
Dec 29 '18 at 18:42
Else you can create
LinearLayout
and apply style– Code Kadiya
Dec 29 '18 at 18:44
How about leaving out the two lines before the last one and just using
btn.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.cal, 0);
?– 0X0nosugar
Dec 29 '18 at 18:48
1
The linked version of the method will take int values (resource identifiers)
– 0X0nosugar
Dec 29 '18 at 19:08