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);
Cœur
  • 37,241
  • 25
  • 195
  • 267
innomotion media
  • 862
  • 11
  • 30
  • 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](https://stackoverflow.com/questions/12523005/how-set-background-drawable-programmatically-in-android) – Gautam Surani Jun 18 '18 at 10:45

9 Answers9

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);
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
2
imageView.setBackgroundResource(R.drawable.some_bg_res); // for setting background 'android:background'
imageView.setImageResource(R.drawable.some_res); // for setting src 'android:src'
Bhavesh Rangani
  • 1,490
  • 12
  • 23
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

Vidhi Dave
  • 5,614
  • 2
  • 33
  • 55
dariush
  • 216
  • 3
  • 10
0

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

btnBack.SetImageResource(Resource.Drawable.drawable_name)
Sergey Glotov
  • 20,200
  • 11
  • 84
  • 98
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().

Lars
  • 710
  • 5
  • 19
0

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

imageView.setImageResource(R.drawable.android_image3);
Lovekush Vishwakarma
  • 3,035
  • 23
  • 25
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.

Tom O
  • 2,495
  • 2
  • 19
  • 23
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

Sam
  • 31
  • 4
-1
ImageButton btn = (ImageButton)findViewbyId(R.id.img_btn)

btn.setImageResource(R.drawable.image)    
btn.setBackgroundResource(R.drawable.ripple) 
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
  • 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