I am new to Android development and I've made myself a Matrix of buttons in Android Studio. The problem is when I am trying to set the size of the buttons, they won't show up in the app. Works pretty fine without setting the size, but they don't fit in my TableLayout. If I added the buttons manually 9 per row, and 9 rows, they showed up and worked with my dimensions.
Here's the part of the code where I am creating the Buttons.
Button[][] btnTag = new Button[9][9];
private void createb()
{
int k=0;
for (int i = 0; i < 9; i++) {
TableRow row = new TableRow(this);
row.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,TableRow.LayoutParams.WRAP_CONTENT));
for (int j = 0; j < 9; j++) {
btnTag[i][j] = new Button(this);
btnTag[i][j].setText("");
k++;
btnTag[i][j].setId(k);
row.addView( btnTag[i][j],30,30);
}
layout.addView(row);
}
}
Can you help me to set the size to 30x30dp and still show up ? Thanks in advance.
The proper way to set the width and height of the Button would be something similar to this:
btnTag[i][j].setLayoutParams(new LinearLayout.LayoutParams(30, 30));
And then
row.addView(btnTag[i][j])
add your xml.The error to set size might be in the xml file itself.
Found out by myself. Looks like the dimmension here row.addView( btnTag[i][j],30,30); is in px and had to put it a little higher (100,100) . Thanks a lot for your time guys!
Related
the actual wondering is the following : I have a full-screen GridLayout which has half of it visible. To see the second part we have to scroll.
I tried some ways and looked around for answers but none of whichs I found gave me what I need. In my grid there are square cells and each one contains a view, and I need to get the x and the y of these views.
Problem is that I could get the coordinates of the views that were put into the visible cells, the views that are not displayed have a x and a y set to 0... Whereas they were drawn..
Here it is, hope some of you guys could help! :)
private void createGrid(){
//I call the function several times
int gridsLength = 19*48*grids;
for(int i = 0; i < 19; i++) {
for (int j = 0; j < 48; j++) {
GridLayout.LayoutParams layoutParams = new GridLayout.LayoutParams();
layoutParams.width = 40;
layoutParams.height = 40;
View view = new View(this);
view.setLayoutParams(layoutParams);
Util.setDrawableBackground(view, R.drawable.border_black, this);
view.setOnTouchListener(newViewListener());
gridViews.add(view);
//glMain is my gridLayout that i put into global variable
glMain.addView(view, i + j + gridsLength);
}
}
grids++;
}
EDIT : I finally found out that I was not always accessing the views AFTER the grid were drawn so I changed that and it's working... Figured out that I was just a fool ^^
I recently started making a chess-like game for android. The first thing I needed was to make the 8 by 8 board. I figured adding 64 buttons and organizing them in the XML wouldn't be much efficient, so I found a way to create them programmatically using a simple 8x8 matrix of buttons. Until this point, everything worked as intended, and I had this:
The next thing I tried was to change the colors of the buttons to match a chessboard. On the internet I found some ways of doing it, but pretty much all of them just made my buttons invisible, and did not change their color.
Here's the onCreate method (the only thing I modified so far):
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_menu);
//GETTING SCREEN DIMENSIONS
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
//SETTING THE BOARD
int TILESIZE = width/8;
LinearLayout back = (LinearLayout) findViewById(R.id.back);
LinearLayout[] rows = new LinearLayout[8];
Button[][] tiles = new Button[8][8];
for(int i = 0; i < 8; i++){
rows[i] = new LinearLayout(this);
back.addView(rows[i]);
for(int j = 0; j < 8; j++){
tiles[i][j] = new Button(this);
tiles[i][j].setWidth(TILESIZE);
tiles[i][j].setHeight(tiles[i][j].getWidth());
if((i + j) % 2 == 0){
tiles[i][j].setBackgroundColor(0xFFFFFFFF);
tiles[i][j].invalidate();
}
else{
//TODO: Make tiles black
}
rows[i].addView(tiles[i][j]);
}
}
}
The XML file contains a single vertical linear layout called back.
My question is how can I make the buttons change color, and where am I doing something wrong. I would also gladly accept alternative (or better) ways to make the board.
Change your loop like this and try
for(int i = 0; i < 8; i++){
rows[i] = new LinearLayout(this);
for(int j = 0; j < 8; j++){
tiles[i][j] = new Button(this);
tiles[i][j].setWidth(TILESIZE);
tiles[i][j].setHeight(tiles[i][j].getWidth());
if((i + j) % 2 == 0){
tiles[i][j].setBackgroundColor(0xFFFFFFFF);
tiles[i][j].invalidate();
}
else{
//TODO: Make tiles black
}
rows[i].addView(tiles[i][j]);
}
back.addView(rows[i]);
}
Change your if loop like this:
if((i + j) % 2 == 0)
tiles[i][j].setBackgroundColor(android.R.color.holo_blue_dark);
else
tiles[i][j].setBackgroundColor(android.R.color.holo_red_dark);
You can define black and white colors in your color.xml file and add them instead using tiles[i][j].setBackgroundColor(getResources().getColor(R.color.white)); and similarly for black.
But make sure, you use a different background so they are clearly visible.
I want to place Check-boxes next to each other dynamically in android and when the width is over then the check boxes need to be aligned from the next line.
I have number of check-boxes which are initializing in an array. I have used the following code. It correctly place only one check-box. Other one gets placed in between. I have used the following code.
LinearLayout layout = (LinearLayout)findViewById(R.id.linearLayout_symptoms_checkboxes);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);;
params.setMargins(0, 10, 0, 0);
for(int i = 0; i < arraySymptoms.length; i++) {
CheckBox cb = new CheckBox(getApplicationContext());
cb.setText(arraySymptoms[i]);
cb.setId(i);
if(i==0)
{
cb.setLayoutParams(params);
layout.addView(cb);
}
else
{
params.addRule(RelativeLayout.ALIGN_RIGHT, i-1);
cb.setLayoutParams(params);
layout.addView(cb);
}
}
Please guide me through this. Your help will be greatly appreciated. Thank you in advance people :)
Just use LinearLayout with horizontal orientation instead of RelativeLayout
I have created a TableLayout and then I created TableRow dynamically in my java code, and added some buttons in the form of an 8x8 grid. But I want to reduce the space between the buttons. I tried setting LayoutParam for the TableRow , but when I do this , the output shows just a blank screen. Here's my code:
LayoutParams param= new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
field=new Button[8][8];
tb=new TableLayout(this);
param.setMargins(10, 2, 10, 2);
for (int i = 0; i < field.length; i++) {
TableRow current=new TableRow(this);
for (int j = 0; j < field[i].length; j++) {
Button button=new Button(this);
field[i][j]=button;
button.setWidth(40);
button.setHeight(40);
button.setLayoutParams(param);
current.addView(button);
}
tb.addView(current);
}
t.addView(tb);
But when I don't write button.setLayoutParams(param)
I get an output like this:
which is the normal output except that I want the space between the buttons reduced.
The spacing you're seeing is padding built into the standard Android button background asset. You can see that your layout is correct by turning on "Show Layout Bounds" in Settings > Developer Options. You just need to make your own button asset, or if a simple color is all that is needed, then just set the button background to be a color.
In the param.setMargins() call, use negative values as necessary to get past what seems to be some natural spacing. You will also want to give the same layout margins to the table layout, and use WRAP_CONTENT for both the width and height. I am not sure if variable "t" is needed as I created the buttons without it using a TableLayout in an XML file. (I also did a 5x5 grid to fit onto my screen.)
LayoutParams param= new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
field=new Button[5][5];
tb=new TableLayout(this);
// these are the two important changes
param.setMargins(-5, -5, -5, -5);
tb.setLayoutParams(param);
for (int i = 0; i < field.length; i++) {
TableRow current=new TableRow(this);
for (int j = 0; j < field[i].length; j++) {
Button button=new Button(this);
field[i][j]=button;
button.setWidth(40);
button.setHeight(40);
button.setLayoutParams(param);
current.addView(button);
}
tb.addView(current);
}
t.addView(tb);
I am currently playing around a bit with code generated Layouts.
There I recogniced a problem in this part of code:
dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int dp_x = (dm.widthPixels - 32) / 10;
for(int x = 0; x < 9; x++) {
TableRow tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
for(int y = 0; y < 9; y++) {
ImageView i = new ImageView(this);
i.setClickable(true);
i.setLayoutParams(new LayoutParams(dp_x, dp_x));
i.setScaleType(ScaleType.FIT_XY);
i.setImageDrawable(getResources().getDrawable(R.drawable.i0));
iv[x][y] = i;
tr.addView(iv[x][y]);
}
fieldLayout.addView(tr);
}
Especially this line seems to cause the problem:
i.setLayoutParams(new LayoutParams(dp_x, dp_x));
When this line is commented out everything is displayed correctly. But as soon as I uncomment it not a single one of the ImageViews is displayed.
I looked into debugging and every value seems to be added correctly to the ImageView.
Any Idea, what causes this error?
Another thing is - I know DisplayMetrics and their width/height are discouraged to use.
Here I use it to scale the ImageViews in dependence to the display.
Can I achieve this in another way, too?
Assuming your dp_x and dp_y values are correct :
TableRow.LayoutParams layoutParams = new TableRow.LayoutParams(dp_x, dp_y);
i.setLayoutParams(layoutParams);
It seems like it doesn't change anything but it worked for me ! Maybe because the LayoutParams are more specific (TableRow.LayoutParams)
Hope this helps !