I'm building a app that have a grid of imageButtons inside of it, I'm setting it from a method, here's the code:
public class BoardLoader {
public void createEmptyGrid(Context context, GridLayout gridLayout, Image image) {
LayoutInflater inflater = LayoutInflater.from(context);
int counter = 0;
for (int i = 0; i < image.getWidth(); i++) {
for (int j = 0; j < image.getHeight(); j++) {
View pixelView = inflater.inflate(R.layout.pixel_item_no_tint, gridLayout, false);
GridLayout.Spec rowSpec = GridLayout.spec(i);
GridLayout.Spec colSpec = GridLayout.spec(j);
GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowSpec, colSpec);
pixelView.setId(counter);
counter++;
pixelView.setLayoutParams(params);
gridLayout.addView(pixelView);
}
}
}
}
The grid is showing correctly, so the next step is to add event listener to all of the imageButtons, I don't want to do it one by one, instead using a method, this is the one that I think might work:
private void setGridListeners(GridLayout gridLayout) {
for (int i = 0; i < gridLayout.getChildCount(); i++) {
View pixelView = gridLayout.getChildAt(i);
pixelView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("testing", "hope it works");
}
});
}
}
But when I see in Logcat is not showing the log, I try in many ways to achieve the goal, like use tags instead of id's, add the listeners inside the createEmptyGrid method, even try with another imageButton outside to verify that was not error of the method, etc... But none of them works
Here's how it looks, the buttons show feedback so is not error of buttons not working or not receiving the click
If someone figure out what's happening will help me so much
Thanks in advance
Steven
UPDATE: Here's the mainActivity content:
public class MainActivity extends AppCompatActivity {
private GridLayout gridLayout;
private ImageAdapter imageAdapter;
private BoardLoader boardLoader;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageAdapter = new ImageAdapter();
boardLoader = new BoardLoader();
Controller control = new Controller();
control.imagesList(this);
int n = control.imagesList(this).get(0).getHeight();
gridLayout = findViewById(R.id.gridLayout);
gridLayout.setRowCount(n);
gridLayout.setColumnCount(n);
imageAdapter.createReferenceImage(this, gridLayout, control.imagesList(this).get(0));
boardLoader.createEmptyGrid(this, gridLayout, control.imagesList(this).get(0));
setGridListeners(gridLayout);
}
private void setGridListeners(GridLayout gridLayout) {
for (int i = 0; i < gridLayout.getChildCount(); i++) {
View pixelView = gridLayout.getChildAt(i);
pixelView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("testing", "hope it works");
}
});
}
}
}
Okay, I figured out how to solve the problem and I'll post here my solution if is helpful for someone.
Basically was a bunch of "beginner" mistakes, and I will list them:
1. Inflating incorrect layout (or, in other words, layout was wrong)
So, I use to have my ImageButtons inside a constraintLayout (Cause I was not knowing how to position it correctly), so that was the first mistake, cause when I was doing this:
View pixelView = inflater.inflate(R.layout.pixel_item_no_tint, gridLayout, false);
I was inflating the constraintLayout, not the imageButton, that's why I wasn't able to add the event listeners, I verify that making a validation and logging the class type of the pixelView:
Log.e("View Class", "Class of pixelView: " + pixelView.getClass().getName());
if (pixelView instanceof ImageButton) {
Log.e("Testing", "" + pixelView.getTag());
}
Then for solve the problem I delete the constraintLayout of the ImageButton and it starts adding the event listeners, however other problem comes:
2. Not setting the width and height GridLayout Params inside the code
So, after I figured out what was happening I stop seeing my buttons on the screen, after a lot of debugging I found that the problem was that the imageButtons wasn't taking the atributes of width and height, even when I have them inside my xml, like this:
android:layout_width="35dp"
android:layout_height="35dp"
So after a lot of attempts I figured out that I have to specify that property inside the params of the imageButton inside the gridLayout (when been added), like this:
GridLayout.Spec rowSpec = GridLayout.spec(i);
GridLayout.Spec colSpec = GridLayout.spec(j);
GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowSpec, colSpec);
params.width = 50;
params.height = 50;
pixelView.setId(counter);
pixelView.setClickable(true);
pixelView.setLayoutParams(params);
And with that I was able to see the gridLayout and test that the eventListeners are working correctly, then you can change each imageButton width and height inside of it programmatically. Hope it help someone, thanks for the persons who comment the post too.