This is my code:
ConstraintLayout.LayoutParams buttonParams = new ConstraintLayout.LayoutParams(40,105);
buttonParams.setMargins(8,16,24,32);
secretCodeFields = findViewById(R.id.secretCodeFields);
for (int i = 0; i < 4; i++) {
Button b = new Button(this);
vID[i]=View.generateViewId();
b.setId(vID[i]);
b.setLayoutParams(buttonParams);
secretCodeFields.addView(b);
}
set.clone(secretCodeFields);
set.createHorizontalChain(
secretCodeFields.getId(),ConstraintSet.LEFT,
secretCodeFields.getId(),ConstraintSet.RIGHT,
vID,null,ConstraintSet.CHAIN_PACKED
);
set.applyTo(secretCodeFields);
Buttons are added in the same place (so only last one is visible). The same resultat if I delete "b.setLayoutParams(buttonParams);" line and use
secretCodeFields.addView(b,buttonParams);
But if I add button with width and height params it works, but there are no more margins:
secretCodeFields.addView(b,90,90);
What I do wrong?
EDIT:
secretCodeFields is ConstraintLayout
I found solution. I added margins AFTER set.applyTo... :
for (int i = 0; i < codeLenght; i++) {
Button b = (Button)findViewById(vID[i]);
ConstraintLayout.LayoutParams p = (ConstraintLayout.LayoutParams) b.getLayoutParams();
p.setMargins(6,0,6,0);
b.setLayoutParams(p);
}
It works, but I don't know if it is the best solution.
I know this is a late answer but here it is
the ConstraintSet.CHAIN_PACKED will remove all applied margins to your views
you should first create the chain in Constraint Set then after that apply margins using the ConstrainSet connect(int startID, int startSide, int endID, int endSide, int margin) method to indicate margins.
Related
I'm trying to create a number of edit texts programmatically without using XML, but I faced some problems while trying this out. Here is a piece of code:
private void createEditText(int l, int topMargin){
ViewGroup.MarginLayoutParams vlp = (ViewGroup.MarginLayoutParams) editText.getLayoutParams();
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(vlp.width, vlp.height);
layoutParams.setMargins((vlp.leftMargin + l) - vlp.width - vlp.height - 25, topMargin, vlp.rightMargin, vlp.bottomMargin);
final EditText newEditText = new EditText(this);
newEditText.setBackgroundResource(R.drawable.rounded_edittext);
newEditText.setLayoutParams(layoutParams);
linearLayout.addView(newEditText);
}
private void numberOfEditTexts(){
String[] words = Answers.answers[getCurrentLevel()].split(" ");
int l = 0, topMargin = 450;
for(int i = 0; i < words.length - 1; i++){
createEditText(l, topMargin);
l += editText.getLayoutParams().width + 5;
if(i == 0) topMargin -= 560;
}
}
The first function creates an edit text and I send 2 parameteres to it, of which the topMargin is the variable that defines the altitude coordinates of the edit text. So the problem is that this edit text will appear differently on different devices because of their screen sizes.
How can I fix it? How can I avoid setting the position manually?
Thanks in advance.
I think you need to get the screen width and length. Use the percentage of screen size as your margin size.
to get the screen size by pixels,
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
Your margin can be some percentage of the height: like 5% or 10% of the screen height. Then it is adjustable.
I am working on coding a MineSweeper game using JavaFx. I am having an issue with changing the Buttons to just include an image and not have text. A portion of my code is as follows:
ImageView bomb;
Image bombImage = new Image(MineSweeper.class.getResourceAsStream("images/bomb.png"));
bomb = new ImageView(bombImage);
boolean[][] mineField = new boolean[row][column];
for (int i = 0; i < numMines; i++) {
int indexRow = isMine.nextInt(row);
int indexCol = isMine.nextInt(column);
System.out.println("row: " + indexRow + ", column: " + indexCol);
mineField[indexRow][indexCol] = true;
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
System.out.println("" + mineField[i][j]);
if (mineField[i][j] == true) {
board[i][j].setText("");
board[i][j].setGraphic(bomb);
} else {
board[i][j].setText("Nope!");
}
}
}
This is not how the actual game will work. But I was wanting to check if I could add the image of the bomb to the buttons that contain a mine. When I run the code there is only one image of a mine that shows up and the other buttons just have empty text or say "Nope!." If I cannot figure out how to add an image to a button then I will not be able to actually continue with the programming of the game. I have decided that I want to build this game from scratch and not use Scene Builder. I appreciate any advice.
The reason for only one image is displayed that you cannot use the same ImageView as graphic for two Button objects.
An optional icon for the Labeled. This can be positioned relative to
the text by using
setContentDisplay(javafx.scene.control.ContentDisplay). The node
specified for this variable cannot appear elsewhere in the scene
graph, otherwise the IllegalArgumentException is thrown. See the class
description of Node for more detail.
Modify this line ...
board[i][j].setGraphic(bomb);
... to ...
board[i][j].setGraphic(new ImageView(bombImage ));
This will create a new ImageView object for all of your Buttons.
on xml layout, I have a FrameLayout with no child. Then I add a list buttons programatically into this framelayout by this code below:
private void addNumbers(){ // Numbers is set with random position
for (int i = 1; i < 20; i++) {
Button btn = new Button(this);
btn.setText("" + i);
btn.setId(i);
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(50, 50);
int leftMargin = new Random().nextInt(widthScreen - btnSize );
int topMargin = new Random().nextInt(heightScreen - btnSize);
lp.leftMargin = leftMargin;
lp.topMargin = topMargin;
btn.setLayoutParams(lp);
framelayout1.addView(btn);
//framelayout2.addView(btn);
}
}
And some buttons overlap together, I do not want to happen this. How to avoid this problem ? Thanks in advance.
P/s: Sorry I can not upload image from my company, all upload > 4K is reject, please tell me if the question is not clear.
In android, I am trying to add buttons programatically, but all the buttons that are added are overlapping. The code I am using is somewhat like this:
for(int i = (int) 'a'; i <= (int) 'z'; i++)
{
Button button = new Button(this);
char letter = (char)i;
String letterOnButton = Character.toString(letter);
button.setText(letterOnButton);
RelativeLayout rl = (RelativeLayout)findViewById(R.id.dynbuttons);
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
rl.addView(button,lp);
}
It does not throw a button, but I only get to see the "z" button.
Any idea on how to fix this?
As mentioned above LinearLayout would be the best solution, but if u still want to use RelativeLayout, try setting an id to each button and inflate the subsequent with the parameter RIGHT_OF/BELOW..as suggested above, parameter "layout_alignLeft" will produce the same effect, i.e inflate all buttons in the same position
RelativeLayout rl = (RelativeLayout) findViewById(R.id.layout);
int id = 0;
for (int i = (int) 'a'; i <= (int) 'z'; i++) {
Button button = new Button(this);
char letter = (char) i;
String letterOnButton = Character.toString(letter);
button.setText(letterOnButton);
button.setId(i);
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.BELOW, id);
rl.addView(button, lp);
id = i;
}
I would go with a LinearLayout instead of a RelativeLayout, something like this:
LinearLayout ll = (LinearLayout)findViewById(R.id.dynbuttons);
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
for(int i = (int) 'a'; i <= (int) 'z'; i++) {
Button button = new Button(this);
char letter = (char)i;
String letterOnButton = Character.toString(letter);
button.setText(letterOnButton);
ll.addView(button,lp);
}
If you still want to use a RelativeLayout you have to set the parameter layout_alignLeft for each button.
I want to set margin of Imageview. What I wanted to achieve is to move one image up to some distance. The content of screen is RelativeLayout, in that there is ImageView at left and button at right aligned. Now I want to move image from left to right within the gap between Image and Button in 100 click of button. The code I am following is as below:
private static int count= 1 ;
public void onClick(View view) {
RelativeLayout lay = (RelativeLayout)findViewById(R.id.layout1);
ImageView i1= (ImageView)findViewById(R.id.img1);
ImageButton btn= (ImageButton)findViewById(R.id.iBtn);
int i1Width = i1.getWidth();
int btnWidth = btn.getWidth();
int totalMragine = lay.getWidth() - i1Width - btnWidth ; //the total margine image will move.
int stepSize = (lay.getWidth() - i1Width - btnWidth ) / 100;
int step = stepSize * count++;
Log.i("i1Width ", ""+i1Width );
Log.i("btnWidth ", ""+btnWidth );
Log.i("lay.getWidth()", ""+lay.getWidth());
Log.i("stepSize", ""+stepSize);
Log.i("count", ""+count);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(step, 0, 0, 0);
i1.setLayoutParams(lp);
LinearLayout.LayoutParams lp1 = (LinearLayout.LayoutParams) horse.getLayoutParams();
Log.i("currentMargine", ""+lp1.leftMargin);
if(lp1.leftMargin >= totalMragine ){
Log.i("Result", "maringe over");
}
}
What I wanted to know that the margin I am setting is in Integer but the value for the step is in float. So How to set margin float in LayoutParams? or is there any other way to do this?
you can do one thing calculate distance to move every time as when going to move
distance to move = the rest of length/rest of clicks
of in final step (100th step) it will reach to end with error < . 5px and . 5px is too small )
like in
1st click distance_to_move = 370/100; as round off come 4
2nd click distance_to_move = 366/99; as round off come 4
3rd click distance_to_move = 362/98; as round off come 4
so movemnt will be as in px 44444...3444...3444..3444...