1

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);
2

9 Answers 9

4

Use setImageResource() to set android:src to your ImageButton

setImageResource() Sets a drawable as the content of this ImageView.

SAMPLE CODE

btnBack.setImageResource(R.drawable.ic_camera);

Use setBackgroundResource() to set android:background to your ImageButton

SAMPLE CODE

btnBack.setBackgroundResource(R.color.colorAccent);
Sign up to request clarification or add additional context in comments.

Comments

2
imageView.setBackgroundResource(R.drawable.some_bg_res); // for setting background 'android:background'
imageView.setImageResource(R.drawable.some_res); // for setting src 'android:src'

Comments

2

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

Comments

0

setImageResource() is for android:src. ImageButton inherits it from ImageView.

btnBack.SetImageResource(Resource.Drawable.drawable_name)

Comments

0

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().

Comments

0

You can set image to ImageView programatically in Android please use bellow like of code.

imageView.setImageResource(R.drawable.android_image3);

Comments

0

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.

Comments

0

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

Comments

-1
ImageButton btn = (ImageButton)findViewbyId(R.id.img_btn)

btn.setImageResource(R.drawable.image)    
btn.setBackgroundResource(R.drawable.ripple) 

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.