I am trying to make dynamic buttons of fixed dimensions. I am able to alter the height but unable to change the width. The width of the button seems to MATCH_PARENT (it takes up whole screen width. If there are two buttons, each button width is half screen width and when there is only one button, button width is screen width).
TableLayout table = (TableLayout)findViewById(r.id.table);
TableRow tableRow = new TableRow(this);
table.addView(tableRow);
Button button = new Button(this);
button.setLayoutParams(new TableRow.LayoutParams(30,30));
tableRow.addView(button);
Could anybody point out where I am going wrong.
Only This You have to do to change Button Dynamically
First I get Button
Button btn = (Button) findViewById(R.id.button1);
Then Button Click event I put This Code Only
TableRow.LayoutParams lp = new TableRow.LayoutParams(70, 70);
btn.setLayoutParams(lp);
And Works Fine. Try It. Will solve your Problem
Instated "button.setLayoutParams(new TableRow.LayoutParams(30,30));" change to following code change the button Height and Width.
LayoutParams lp = new LayoutParams(40, 40);
table.addView(button, lp);
Related
I am trying to make a game with multiple levels. My plan is to print 15 levels on 1 page. I have attached a photo of the simple design I am currently using.
As you can see in the picture the right border of a button is not showing. For the parent I am using this java code:
LinearLayout horl = new LinearLayout(this);
horl.setId(worldVar);
horl.setOrientation(LinearLayout.HORIZONTAL);
horl.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
And to set 3 buttons horizontally in this LinearLayout I use this code for the buttons. This code just has a simple for loop around to put 3 buttons in this LinearLayout:
Button button = new Button(this);
LinearLayout.LayoutParams params = new LinearLayout
.LayoutParams(getMarginInDp(100), getMarginInDp(100));
params.setMargins(getMarginInDp(1), getMarginInDp(25), getMarginInDp(1), 0);
button.setLayoutParams(params);
GradientDrawable gd = new GradientDrawable();
gd.setCornerRadius(5);
gd.setStroke(1, 0xFF000000);horl.addView(button);
The function getMarginInDp looks like this:
public int getMarginInDp(int dp){
return (int) TypedValue
.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics());
}
Does anybody have any idea how to make the border appear because I want to keep the buttons approximately this size and I may even slightly increase the margins?
You can set the weight to 0.33f for each button:
Button button = new Button(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(getMarginInDp(100), getMarginInDp(100), 0.33f);
so they are distributed equally in the width of the layout.
I am creating some buttons in a tablelayout. I am creating the buttons programmatically.
For example the following code is in the "onresume" method...
TableLayout.LayoutParams tableParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT);
TableRow.LayoutParams rowParams = new TableRow.LayoutParams(0,android.widget.TableRow.LayoutParams.MATCH_PARENT,3f);
TableLayout tableLayout = (TableLayout) findViewById(R.id.sometablelayoutivealreadydefinedinxml);
TableRow tableRow = new TableRow(this);
tableRow.setLayoutParams(tableParams);
Button btnOne = new Button(this);
btnOne.setId(100);
btnOne.setLayoutParams(rowParams);
btnTwo.setId(200);
btnTwo.setLayoutParams(rowParams);
tableRow.addView(btnOne);
tableRow.addView(btnTwo);
tableLayout.addView(tableRow);
I want to set the height of the button to be the SAME as the width. The width is determined by the size of the screen. The problem as I understand it,is that until the buttons are actually drawn to the screen they don't exist so any width returned will be zero. So I tried the following code.
Button yourBtn = (Button)findViewById(100);
int btnSize;
btnSize = yourBtn.getWidth();
yourBtn.setLayoutParams(new TableRow.LayoutParams(btnSize, btnSize));
Now the problem is that if I run this code immediately after creating the buttons the width of the button is returned as 0 as the button hasn't been drawn yet. However, if I assign the code to a test button then it resize the button as I want it to. However, I don't want the user to have to press a button after the screen has loaded to resize the buttons properly.
So, the question is when is the appropriate time to call this to resize the buttons properly when the activity is created but after the buttons have been drawn and how do I do this?
What is the basic difference ?
When I do like this
Button button = new Button();
button.setPrefWidth(50);
button.setMaxWidth(Double.MAX_VALUE);
any one will think that the button will be shown with width 50 and can be expandable up to infinite , but what,s happening is the button occupies the total width of the screen .
Does any one know what's their intention?
my border pane code :
borderPane.setPadding(new Insets(10,10,10,10));
borderPane.setCenter(button);
borderPane.getStyleClass().add("bpane");
//Region region = (Region) anyNode;
button.setStyle("-fx-background-color: red;");
I need a button with foreground transparent image and background color. So have use this code. The background color is going outside of the image. I need the button with the same size of the image.
Depending on the user interaction i have to change the foreground image and background color. I want add the image and background color separately so that i can change one of them at minimum cost. I have to use a lot of button in this UI so it will be done in java code.
layout = new TableLayout(this);
layout.setLayoutParams(new TableLayout.LayoutParams(8,7));
TableRow row2 = new TableRow(this);
buttonPlayer1 = new ImageButton(this);
buttonPlayer1.setImageDrawable(getResources().getDrawable(R.drawable.blankc4));
buttonPlayer1.setBackgroundColor(Color.GREEN);
row2.addView(buttonPlayer1);
layout.addView(row2);
If your only problem is that button background color is going outsize image and you need button size to be same as image size then get image Height and Width using getHeight() & getWidth() methods on image object and use these values to make the Button size same as Image size using setHeight() & setWidth() methods respectively on button object.
Sample Code with LinearLayout:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout btnLO = new LinearLayout(this);
btnLO.setOrientation(LinearLayout.VERTICAL);
ImageButton i1 = new ImageButton(this);
i1.setBackgroundColor(color.background_dark);
i1.setImageDrawable(getResources().getDrawable(R.drawable.rana));
btnLO.addView(i1, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
// btnLO.setGravity(Gravity.LEFT | Gravity.CENTER_HORIZONTAL);
this.addContentView(btnLO, new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
}
I have written is code below, but button isn't shown because i've set FILL_PARENT in bar layoutParametrs. How i could fixed it without using XML?
TableRow myLayout = new TableRow (this);
myLayout.setAlwaysDrawnWithCacheEnabled(true);
TextView text = new TextView (this);
text.setText("Test: ");
SeekBar bar = new SeekBar (this);
ToggleButton button = new ToggleButton (this);
myLayout.addView( text , new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
myLayout.addView( bar , new TableRow.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
myLayout.addView( button , new TableRow.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
If FILL_PARENT set for bar
If isn't
both variant is wrong
When I need to fill a row with multiple items, I typically use layout_weight and a width setting of fill_parent. That distributes the full width of the row by the proportions for which you set the weight values.
Since you're doing this in code and not xml, look at the constructor for LayoutParams that takes a third parameter. This corresponds to layout_weight:
LayoutParams - int, int, float
Try using WRAP_CONTENT and a weight parameter like this.
myLayout.addView( text , new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,5));
myLayout.addView( bar , new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,10));
myLayout.addView( button , new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,85));