Setting android:background / android:src programmatically
I need to set two attributes like in a xml:
<ImageButton
android:id="@+id/btn_friendsMainMenu"
android:src="@drawable/general_btn_header_friendlist"
android:background="@drawable/ripple"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
As you see, there is a background and an src attribute. How do I set BOTH programmatically?
I only know of one: Which one is it? And what is the other one?
btnBack.SetBackgroundResource(Resource.Drawable.thebook_backbutton);
android
add a comment |
I need to set two attributes like in a xml:
<ImageButton
android:id="@+id/btn_friendsMainMenu"
android:src="@drawable/general_btn_header_friendlist"
android:background="@drawable/ripple"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
As you see, there is a background and an src attribute. How do I set BOTH programmatically?
I only know of one: Which one is it? And what is the other one?
btnBack.SetBackgroundResource(Resource.Drawable.thebook_backbutton);
android
2
You can use this methods: imageButton.setImageResource(); imageButton.setBackgroundResource();
– Arti patel
Jun 18 '18 at 10:30
Possible duplicate of How set background drawable programmatically in Android
– Gautam Surani
Jun 18 '18 at 10:45
add a comment |
I need to set two attributes like in a xml:
<ImageButton
android:id="@+id/btn_friendsMainMenu"
android:src="@drawable/general_btn_header_friendlist"
android:background="@drawable/ripple"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
As you see, there is a background and an src attribute. How do I set BOTH programmatically?
I only know of one: Which one is it? And what is the other one?
btnBack.SetBackgroundResource(Resource.Drawable.thebook_backbutton);
android
I need to set two attributes like in a xml:
<ImageButton
android:id="@+id/btn_friendsMainMenu"
android:src="@drawable/general_btn_header_friendlist"
android:background="@drawable/ripple"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
As you see, there is a background and an src attribute. How do I set BOTH programmatically?
I only know of one: Which one is it? And what is the other one?
btnBack.SetBackgroundResource(Resource.Drawable.thebook_backbutton);
android
android
edited Dec 29 '18 at 2:42
Cœur
17.6k9105145
17.6k9105145
asked Jun 18 '18 at 10:25
innomotion mediainnomotion media
356110
356110
2
You can use this methods: imageButton.setImageResource(); imageButton.setBackgroundResource();
– Arti patel
Jun 18 '18 at 10:30
Possible duplicate of How set background drawable programmatically in Android
– Gautam Surani
Jun 18 '18 at 10:45
add a comment |
2
You can use this methods: imageButton.setImageResource(); imageButton.setBackgroundResource();
– Arti patel
Jun 18 '18 at 10:30
Possible duplicate of How set background drawable programmatically in Android
– Gautam Surani
Jun 18 '18 at 10:45
2
2
You can use this methods: imageButton.setImageResource(); imageButton.setBackgroundResource();
– Arti patel
Jun 18 '18 at 10:30
You can use this methods: imageButton.setImageResource(); imageButton.setBackgroundResource();
– Arti patel
Jun 18 '18 at 10:30
Possible duplicate of How set background drawable programmatically in Android
– Gautam Surani
Jun 18 '18 at 10:45
Possible duplicate of How set background drawable programmatically in Android
– Gautam Surani
Jun 18 '18 at 10:45
add a comment |
9 Answers
9
active
oldest
votes
Use setImageResource()
to set android:src
to your ImageButton
setImageResource()
Sets adrawable
as the content of thisImageView
.
SAMPLE CODE
btnBack.setImageResource(R.drawable.ic_camera);
Use setBackgroundResource()
to set android:background
to your ImageButton
SAMPLE CODE
btnBack.setBackgroundResource(R.color.colorAccent);
1
absolutely perfect! thanks a lot.
– innomotion media
Jun 18 '18 at 10:46
@innomotionmedia welcome happy to help you
– Nilesh Rathod
Jun 18 '18 at 10:47
add a comment |
you can do it by this code:
private void initView() {
rootLayout =new LinearLayout(this);
rootLayout.setBackgroundDrawable(getResources().getDrawable(R.drawable.layer_5));
imgLogo=new ImageView(this);
imgLogo.setImageDrawable(getResources().getDrawable(R.drawable.splash_logo));
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
350,
350);
imgLogo.setLayoutParams(params);
rootLayout.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
rootLayout.addView(imgLogo);
setContentView(rootLayout);
}
and call initView method in your onCreate
add a comment |
imageView.setBackgroundResource(R.drawable.some_bg_res); // for setting background 'android:background'
imageView.setImageResource(R.drawable.some_res); // for setting src 'android:src'
add a comment |
setImageResource()
is for android:src
. ImageButton inherits it from ImageView.
btnBack.SetImageResource(Resource.Drawable.drawable_name)
add a comment |
setBackgroundResource()
sets the background - it differs from setBackground()
by taking a ressource id (as int) as input.
I am fairly sure setImage()
is the method setting the 'src' attribute in xml. It also comes in some different variants. If you want to set a drawable as in your example use setImageDrawable()
.
add a comment |
You can set image to ImageView programatically in Android please use bellow like of code.
imageView.setImageResource(R.drawable.android_image3);
add a comment |
android:src is set with setImageResource()
and
android:background is set with setBackgroundResource()
In code
ImageButton btn = (ImageButton)findViewById(R.id.btn_friendsMainMenu);
btn.setImageResource(R.drawable.general_btn_header_friendlist)
btn.setBackgroundResource(R.drawable.ripple)
ImagResource will be on top of the BackgroundResource.
add a comment |
There are already true answers but a better approach would be to put this attributes in styles.xml to and give that style to the buttons you want to use to increase clarity and reduce the number of lines you have to write. When your application gets bigger, setting everything from activity/fragment will become unmaintainable.
If your only goal is to change it in the code then take a look at this.
How to programmatically set style attribute in a view
add a comment |
ImageButton btn = (ImageButton)findViewbyId(R.id.img_btn)
btn.setImageResource(R.drawable.image)
btn.setBackgroundResource(R.drawable.ripple)
3
While this code may answer the question, providing additional context regarding how and why it solves the problem would improve the answer's long-term value.
– Alexander
Jun 18 '18 at 13:02
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%2f50907384%2fsetting-androidbackground-androidsrc-programmatically%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
9 Answers
9
active
oldest
votes
9 Answers
9
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use setImageResource()
to set android:src
to your ImageButton
setImageResource()
Sets adrawable
as the content of thisImageView
.
SAMPLE CODE
btnBack.setImageResource(R.drawable.ic_camera);
Use setBackgroundResource()
to set android:background
to your ImageButton
SAMPLE CODE
btnBack.setBackgroundResource(R.color.colorAccent);
1
absolutely perfect! thanks a lot.
– innomotion media
Jun 18 '18 at 10:46
@innomotionmedia welcome happy to help you
– Nilesh Rathod
Jun 18 '18 at 10:47
add a comment |
Use setImageResource()
to set android:src
to your ImageButton
setImageResource()
Sets adrawable
as the content of thisImageView
.
SAMPLE CODE
btnBack.setImageResource(R.drawable.ic_camera);
Use setBackgroundResource()
to set android:background
to your ImageButton
SAMPLE CODE
btnBack.setBackgroundResource(R.color.colorAccent);
1
absolutely perfect! thanks a lot.
– innomotion media
Jun 18 '18 at 10:46
@innomotionmedia welcome happy to help you
– Nilesh Rathod
Jun 18 '18 at 10:47
add a comment |
Use setImageResource()
to set android:src
to your ImageButton
setImageResource()
Sets adrawable
as the content of thisImageView
.
SAMPLE CODE
btnBack.setImageResource(R.drawable.ic_camera);
Use setBackgroundResource()
to set android:background
to your ImageButton
SAMPLE CODE
btnBack.setBackgroundResource(R.color.colorAccent);
Use setImageResource()
to set android:src
to your ImageButton
setImageResource()
Sets adrawable
as the content of thisImageView
.
SAMPLE CODE
btnBack.setImageResource(R.drawable.ic_camera);
Use setBackgroundResource()
to set android:background
to your ImageButton
SAMPLE CODE
btnBack.setBackgroundResource(R.color.colorAccent);
edited Jun 18 '18 at 10:38
answered Jun 18 '18 at 10:32
Nilesh RathodNilesh Rathod
30.6k82956
30.6k82956
1
absolutely perfect! thanks a lot.
– innomotion media
Jun 18 '18 at 10:46
@innomotionmedia welcome happy to help you
– Nilesh Rathod
Jun 18 '18 at 10:47
add a comment |
1
absolutely perfect! thanks a lot.
– innomotion media
Jun 18 '18 at 10:46
@innomotionmedia welcome happy to help you
– Nilesh Rathod
Jun 18 '18 at 10:47
1
1
absolutely perfect! thanks a lot.
– innomotion media
Jun 18 '18 at 10:46
absolutely perfect! thanks a lot.
– innomotion media
Jun 18 '18 at 10:46
@innomotionmedia welcome happy to help you
– Nilesh Rathod
Jun 18 '18 at 10:47
@innomotionmedia welcome happy to help you
– Nilesh Rathod
Jun 18 '18 at 10:47
add a comment |
you can do it by this code:
private void initView() {
rootLayout =new LinearLayout(this);
rootLayout.setBackgroundDrawable(getResources().getDrawable(R.drawable.layer_5));
imgLogo=new ImageView(this);
imgLogo.setImageDrawable(getResources().getDrawable(R.drawable.splash_logo));
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
350,
350);
imgLogo.setLayoutParams(params);
rootLayout.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
rootLayout.addView(imgLogo);
setContentView(rootLayout);
}
and call initView method in your onCreate
add a comment |
you can do it by this code:
private void initView() {
rootLayout =new LinearLayout(this);
rootLayout.setBackgroundDrawable(getResources().getDrawable(R.drawable.layer_5));
imgLogo=new ImageView(this);
imgLogo.setImageDrawable(getResources().getDrawable(R.drawable.splash_logo));
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
350,
350);
imgLogo.setLayoutParams(params);
rootLayout.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
rootLayout.addView(imgLogo);
setContentView(rootLayout);
}
and call initView method in your onCreate
add a comment |
you can do it by this code:
private void initView() {
rootLayout =new LinearLayout(this);
rootLayout.setBackgroundDrawable(getResources().getDrawable(R.drawable.layer_5));
imgLogo=new ImageView(this);
imgLogo.setImageDrawable(getResources().getDrawable(R.drawable.splash_logo));
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
350,
350);
imgLogo.setLayoutParams(params);
rootLayout.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
rootLayout.addView(imgLogo);
setContentView(rootLayout);
}
and call initView method in your onCreate
you can do it by this code:
private void initView() {
rootLayout =new LinearLayout(this);
rootLayout.setBackgroundDrawable(getResources().getDrawable(R.drawable.layer_5));
imgLogo=new ImageView(this);
imgLogo.setImageDrawable(getResources().getDrawable(R.drawable.splash_logo));
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
350,
350);
imgLogo.setLayoutParams(params);
rootLayout.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
rootLayout.addView(imgLogo);
setContentView(rootLayout);
}
and call initView method in your onCreate
edited Jun 18 '18 at 10:36
Vishva Dave
3,79621840
3,79621840
answered Jun 18 '18 at 10:36
dariushdariush
8118
8118
add a comment |
add a comment |
imageView.setBackgroundResource(R.drawable.some_bg_res); // for setting background 'android:background'
imageView.setImageResource(R.drawable.some_res); // for setting src 'android:src'
add a comment |
imageView.setBackgroundResource(R.drawable.some_bg_res); // for setting background 'android:background'
imageView.setImageResource(R.drawable.some_res); // for setting src 'android:src'
add a comment |
imageView.setBackgroundResource(R.drawable.some_bg_res); // for setting background 'android:background'
imageView.setImageResource(R.drawable.some_res); // for setting src 'android:src'
imageView.setBackgroundResource(R.drawable.some_bg_res); // for setting background 'android:background'
imageView.setImageResource(R.drawable.some_res); // for setting src 'android:src'
answered Jun 18 '18 at 10:31
Bhavesh RanganiBhavesh Rangani
915717
915717
add a comment |
add a comment |
setImageResource()
is for android:src
. ImageButton inherits it from ImageView.
btnBack.SetImageResource(Resource.Drawable.drawable_name)
add a comment |
setImageResource()
is for android:src
. ImageButton inherits it from ImageView.
btnBack.SetImageResource(Resource.Drawable.drawable_name)
add a comment |
setImageResource()
is for android:src
. ImageButton inherits it from ImageView.
btnBack.SetImageResource(Resource.Drawable.drawable_name)
setImageResource()
is for android:src
. ImageButton inherits it from ImageView.
btnBack.SetImageResource(Resource.Drawable.drawable_name)
answered Jun 18 '18 at 10:30
Sergey GlotovSergey Glotov
16.8k117288
16.8k117288
add a comment |
add a comment |
setBackgroundResource()
sets the background - it differs from setBackground()
by taking a ressource id (as int) as input.
I am fairly sure setImage()
is the method setting the 'src' attribute in xml. It also comes in some different variants. If you want to set a drawable as in your example use setImageDrawable()
.
add a comment |
setBackgroundResource()
sets the background - it differs from setBackground()
by taking a ressource id (as int) as input.
I am fairly sure setImage()
is the method setting the 'src' attribute in xml. It also comes in some different variants. If you want to set a drawable as in your example use setImageDrawable()
.
add a comment |
setBackgroundResource()
sets the background - it differs from setBackground()
by taking a ressource id (as int) as input.
I am fairly sure setImage()
is the method setting the 'src' attribute in xml. It also comes in some different variants. If you want to set a drawable as in your example use setImageDrawable()
.
setBackgroundResource()
sets the background - it differs from setBackground()
by taking a ressource id (as int) as input.
I am fairly sure setImage()
is the method setting the 'src' attribute in xml. It also comes in some different variants. If you want to set a drawable as in your example use setImageDrawable()
.
answered Jun 18 '18 at 10:31
LarsLars
12810
12810
add a comment |
add a comment |
You can set image to ImageView programatically in Android please use bellow like of code.
imageView.setImageResource(R.drawable.android_image3);
add a comment |
You can set image to ImageView programatically in Android please use bellow like of code.
imageView.setImageResource(R.drawable.android_image3);
add a comment |
You can set image to ImageView programatically in Android please use bellow like of code.
imageView.setImageResource(R.drawable.android_image3);
You can set image to ImageView programatically in Android please use bellow like of code.
imageView.setImageResource(R.drawable.android_image3);
answered Jun 18 '18 at 10:33
Lovekush VishwakarmaLovekush Vishwakarma
985816
985816
add a comment |
add a comment |
android:src is set with setImageResource()
and
android:background is set with setBackgroundResource()
In code
ImageButton btn = (ImageButton)findViewById(R.id.btn_friendsMainMenu);
btn.setImageResource(R.drawable.general_btn_header_friendlist)
btn.setBackgroundResource(R.drawable.ripple)
ImagResource will be on top of the BackgroundResource.
add a comment |
android:src is set with setImageResource()
and
android:background is set with setBackgroundResource()
In code
ImageButton btn = (ImageButton)findViewById(R.id.btn_friendsMainMenu);
btn.setImageResource(R.drawable.general_btn_header_friendlist)
btn.setBackgroundResource(R.drawable.ripple)
ImagResource will be on top of the BackgroundResource.
add a comment |
android:src is set with setImageResource()
and
android:background is set with setBackgroundResource()
In code
ImageButton btn = (ImageButton)findViewById(R.id.btn_friendsMainMenu);
btn.setImageResource(R.drawable.general_btn_header_friendlist)
btn.setBackgroundResource(R.drawable.ripple)
ImagResource will be on top of the BackgroundResource.
android:src is set with setImageResource()
and
android:background is set with setBackgroundResource()
In code
ImageButton btn = (ImageButton)findViewById(R.id.btn_friendsMainMenu);
btn.setImageResource(R.drawable.general_btn_header_friendlist)
btn.setBackgroundResource(R.drawable.ripple)
ImagResource will be on top of the BackgroundResource.
answered Jun 18 '18 at 10:41
Tom OTom O
2817
2817
add a comment |
add a comment |
There are already true answers but a better approach would be to put this attributes in styles.xml to and give that style to the buttons you want to use to increase clarity and reduce the number of lines you have to write. When your application gets bigger, setting everything from activity/fragment will become unmaintainable.
If your only goal is to change it in the code then take a look at this.
How to programmatically set style attribute in a view
add a comment |
There are already true answers but a better approach would be to put this attributes in styles.xml to and give that style to the buttons you want to use to increase clarity and reduce the number of lines you have to write. When your application gets bigger, setting everything from activity/fragment will become unmaintainable.
If your only goal is to change it in the code then take a look at this.
How to programmatically set style attribute in a view
add a comment |
There are already true answers but a better approach would be to put this attributes in styles.xml to and give that style to the buttons you want to use to increase clarity and reduce the number of lines you have to write. When your application gets bigger, setting everything from activity/fragment will become unmaintainable.
If your only goal is to change it in the code then take a look at this.
How to programmatically set style attribute in a view
There are already true answers but a better approach would be to put this attributes in styles.xml to and give that style to the buttons you want to use to increase clarity and reduce the number of lines you have to write. When your application gets bigger, setting everything from activity/fragment will become unmaintainable.
If your only goal is to change it in the code then take a look at this.
How to programmatically set style attribute in a view
answered Jun 18 '18 at 11:38
SamSam
314
314
add a comment |
add a comment |
ImageButton btn = (ImageButton)findViewbyId(R.id.img_btn)
btn.setImageResource(R.drawable.image)
btn.setBackgroundResource(R.drawable.ripple)
3
While this code may answer the question, providing additional context regarding how and why it solves the problem would improve the answer's long-term value.
– Alexander
Jun 18 '18 at 13:02
add a comment |
ImageButton btn = (ImageButton)findViewbyId(R.id.img_btn)
btn.setImageResource(R.drawable.image)
btn.setBackgroundResource(R.drawable.ripple)
3
While this code may answer the question, providing additional context regarding how and why it solves the problem would improve the answer's long-term value.
– Alexander
Jun 18 '18 at 13:02
add a comment |
ImageButton btn = (ImageButton)findViewbyId(R.id.img_btn)
btn.setImageResource(R.drawable.image)
btn.setBackgroundResource(R.drawable.ripple)
ImageButton btn = (ImageButton)findViewbyId(R.id.img_btn)
btn.setImageResource(R.drawable.image)
btn.setBackgroundResource(R.drawable.ripple)
edited Jun 18 '18 at 12:10
Nilesh Rathod
30.6k82956
30.6k82956
answered Jun 18 '18 at 11:07
Khushi GajjarKhushi Gajjar
13
13
3
While this code may answer the question, providing additional context regarding how and why it solves the problem would improve the answer's long-term value.
– Alexander
Jun 18 '18 at 13:02
add a comment |
3
While this code may answer the question, providing additional context regarding how and why it solves the problem would improve the answer's long-term value.
– Alexander
Jun 18 '18 at 13:02
3
3
While this code may answer the question, providing additional context regarding how and why it solves the problem would improve the answer's long-term value.
– Alexander
Jun 18 '18 at 13:02
While this code may answer the question, providing additional context regarding how and why it solves the problem would improve the answer's long-term value.
– Alexander
Jun 18 '18 at 13:02
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%2f50907384%2fsetting-androidbackground-androidsrc-programmatically%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
2
You can use this methods: imageButton.setImageResource(); imageButton.setBackgroundResource();
– Arti patel
Jun 18 '18 at 10:30
Possible duplicate of How set background drawable programmatically in Android
– Gautam Surani
Jun 18 '18 at 10:45