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 !
Related
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!
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'm trying to create a TableLayout programmatically, in order to make a grid-like layout with a configurable row and column count. This would likely be trivial to do in a layout file, but because each child element will be nearly identical and I want the row and column count to be configurable, I would like to do it programmatically if possible.
So far this is what I have.
MainActivity.java
private void displayBoard() {
TableLayout l = (TableLayout) findViewById(R.id.GameGrid);
l.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
for (int y = 0; y < b.height(); y++) {
TableRow r = new TableRow(this);
r.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, 15));
for (int x = 0; x < b.width(); x++) {
r.addView(b.getSpaceAt(x, y));
}
l.addView(r);
}
}
Board.java
for (int y = 0; y < spaces.length; y++) {
for (int x = 0; x < spaces[0].length; x++) {
spaces[y][x] = new BoardSpace(context, x, y);
}
}
BoardSpace.java
public class BoardSpace extends Button {
private ReversiPiece piece;
protected int x, y;
private BoardSpace(Context c) {
super(c);
}
public BoardSpace(Context c, int x, int y) {
super(c);
// this.setWidth(25); // Doesn't make a difference, either
// this.setHeight(15); // Doesn't make a difference, either
piece = null;
this.x = x;
this.y = y;
}
public ReversiPiece piece() {
return piece;
}
public void setPiece(ReversiPiece p) {
piece = p;
}
}
I'm basically trying to create a grid of buttons, but so far I cannot get the size of the buttons to change so I can make sure they all fit on the screen. At the moment they appear at a standard size, and any changes I make to the LayoutParams definitions do not make a difference.
I suspect this issue has to do with my usage of LayoutParams. There are a lot of LayoutParams classes associated with each View type, so perhaps I'm not using the right one? Any help would be appreciated.
EDIT: Now creating a new TableLayout.LayoutParams object for each TableRow based on cyanide's recommendation, but the buttons are still displaying at default size.
You are right, that's about LayoutParams, which must not be shared between views: each view should have its own copy of LayoutParams.
In other words, this is what you need:
r.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, 15));
About getSpaceAt (there is not code for it). Does it create a view programmatically? Remember to set LayoutParams and again it should be separate for each view.
Hopefully it will help.
Got it! I was trying to manipulate the LayoutParams of my TableLayout and TableRows when in reality I needed to set TableRow.LayoutParams on the buttons themselves, when I add them to TableRow. This involves specifying the weight of each child element (Button) as well.
private void displayBoard() {
TableLayout l = (TableLayout) findViewById(R.id.GameGrid);
l.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
for (int y = 0; y < b.height(); y++) {
TableRow r = new TableRow(this);
r.setWeightSum(b.width());
for (int x = 0; x < b.width(); x++) {
BoardSpace s = b.getSpaceAt(x, y);
s.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.MATCH_PARENT, 1.0f));
r.addView(s);
}
l.addView(r);
}
}
TableRow.setWeightSum() is used to set the weight sum of the row to the number of elements in the row, then passing a weight of 1 to the LayoutParams for the button causes the buttons to adjust in size to fit on the screen, as intended.
Surprisingly this answer was for a more generic question but this is what led me to the solution:
How to set layout_weight attribute dynamically from code?
I am using a TableLayout to print nine pictures. For some reason, I am getting a big gap between rows as shown in the image below. I set the background to green so the gaps are easy to see. My TableLayout is created programmatically. How do I fix this problem so that the gap between rows is not so big?
I have already tried tableRowParams.setMargins(0,0,0,0).
BTW: No I don't want to use ListView, etc.
I have been messing around with the code a lot trying to fix the problem. Below is simply the current state of the code:
EDIT: CORRECT IMAGE:
EDIT: the code now will work fine (thanks to #Guian):
public class FacialExpressionImagesTable extends TableLayout {
public FacialExpressionImagesTable(Context context, List<Bitmap> imageList, int sideDimension, int tableWidth, int tableHeight) {
super(context);
setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
setBackgroundColor(Color.BLUE);
setContent(imageList, context, sideDimension);
}
private void setContent(List<Bitmap> imageList, Context context, final int sideDimension) {
final int iHeight = imageList.get(0).getHeight();
final int iWidth = imageList.get(0).getWidth();
int ndx = 0;
for (int r = 0; r < sideDimension; r++) {
TableRow tableRow = new TableRow(context);
TableLayout.LayoutParams forRow = new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
tableRow.setLayoutParams(forRow);
tableRow.setBackgroundColor(Color.RED);
TableRow.LayoutParams elementLayout = new TableRow.LayoutParams(iWidth, LayoutParams.WRAP_CONTENT, 1);
tableRow.requestLayout();
for (int c = 0; c < sideDimension; c++) {
ImageView element = new ImageView(context);
element.setLayoutParams(elementLayout);
element.setAdjustViewBounds(true);
element.setPadding(0, 0, 3, 3);
element.setBackgroundColor(Color.GREEN);
element.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
element.setImageBitmap(imageList.get(ndx++));
element.requestLayout();
tableRow.addView(element);
}
addView(tableRow);
}
}
}
first : be aware that you exchange width and height in :
new TableRow.LayoutParams(iHeight, iWidth);
But anyway, you can't give your table itesm the size of the bitmap's getHeight and getWidth since they will be resized ( depending on the screen size, screen density etc ... you would have to compute the new size according to density... )
here I think they are reduced. that's why the height of the row is too big.
set your layout params so the element take wrap_content in height and 0dip with a layout_weight to 1 in width;
TableRow.LayoutParams elementLayout = new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1 );
then the table row take wrap content as height :
TableLayout.LayoutParams forRow = new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
if image are not scaled as needed, you'll may have to set a scale type to your ImageViews : ( using setScaleType )
elementLayout.setScaleType(ScaleType.CENTER_INSIDE); // or
FIT_CENTER... not quite sure
It should be good, tell if its not.
hope that helps.
Also try setting padding to 0 so that there is no padding inside each row of your table
I have created two imageViews promatically as shown below:
public void createImageViews(Integer count){
ImageView[] imageViewArray = new ImageView[count];
for (int i = 0; i < count; i++ ) {
imageViewArray[i] = new ImageView(getBaseContext());
imageViewArray[i].setId(i); // unique property for every imageView
if(i==0){
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
imageViewArray[i].setLayoutParams(params);
imageViewArray[i].setBackgroundResource(imagesForIv[i]);
_UIRLParent.addView(imageViewArray[i]);
Log.v("first", "first"+i);
}
else if(i < 3){
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.RIGHT_OF,imageViewArray[i].getId());
imageViewArray[i].setBackgroundResource(imagesForIv[i]);
_UIRLParent.addView(imageViewArray[i],params);
Log.v("second", "second"+i);
}
}
I just need to place the second imageView toRightOf first imageView. Can someone help me. This is eating away a lot of my time.
try https://stackoverflow.com/a/5191159/1436931
you are using wrong index values.
at line
params.addRule(RelativeLayout.RIGHT_OF,imageViewArray[i].getId());
you aligning current image RIGHT of current image. :)
you need to track the index of last imageView id i.e left Image view
you need something like this
params.addRule(RelativeLayout.RIGHT_OF,imageViewArray[i-1].getId());