D
D
de43gy2015-07-15 12:48:32
Java
de43gy, 2015-07-15 12:48:32

Programmatically creating an ImageButton and giving it an image (Android)?

I created my own class with the ImageButton parent and now I want to pass an image to it.
I have recently studied android and still do not understand the features of working with resources.
I got it this way -
ViewGroup.LayoutParams lpView = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
CubeButton cubeBtn = new CubeButton(this); //this is the button itself, the successor of ImageButton
cubeBtn.setImageDrawable(getDrawable(R.drawable.grass_01)); //this should work like this
linLayout.addView(cubeBtn, lpView);
I take the drawing from the file loaded in drawable. As I understand it, a wrapper class is created for the files placed there, this same Drawable. (This is a hidden question, if everything is wrong, please correct
it) Question (main) - am I doing it right and how can I do it again (IntelliJ IDEA swears that this option can be used with 21 api, and my minimum is 19 )?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
X
xotta6bl4, 2015-07-23
@xotta6bl4

setImageResource(R.drawable.grass_01)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question