Answer the question
In order to leave comments, you need to log in
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question