Dynamic LinearLayout with same height as width - android

I'm trying to create a dynamic LinearLayout with the same height as width. With XML I can achieve this by setting the the height as 0px i.e. android:layout_height="0dp". But I'm not able to achieve the same thing while creating the layout from Java code.
LinearLayout parentLayout = layoutInflater.inflate();
LinearLayout childLayout = new LinearLayout(context);
LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
layoutParams.height = 0;
parentLayout.addView(childLayout, layoutParams);
I want the childLayout to be of the same height as width but the height appears to be set to WRAP_CONTENT instead, what am I missing?

layoutParams.weight = layoutParams.height

Related

Set View height to 0dp and weight to 1 programatically

When creating GridView via xml I use the below code, but how could I achieve that programmatically during runtime?
android:layout_height="0dp"
android:layout_weight="1"
Thanks.
use this
GridView YOUR_GRID_VIEW =(GridView )findViewById(R.id.GridView )
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
/*width*/ ViewGroup.LayoutParams.MATCH_PARENT,
/*height*/ 0,
/*weight*/ 1.0f
);
YOUR_GRID_VIEW.setLayoutParams(param);
LayoutParams.MATCH_PARENT is for width, 0 for Height and 1.0f is for weight
If you have layout_weight in xml, that means, that the parent layout of GridView is LinearLayout.
Having a reference to a GridView, that is already attached to view hierarchy, you can simply get LayoutParams of that view, cast it to LinearLayout.LayoutParams and perform necessary changes:
GridView gridView = ...;
LinearLayout.LayoutParams params = gridView.getLayoutParams();
params.weight = 1;
params.height = 0;
gridView.setLayoutParams(params);
Otherwise, if you are constructing GridView from scratch, you should create LinearLayout.LayoutParams on your own:
LinearLayout linearLayout = ...;
GridView gridView = new GridView(context);
LinearLayout.LayoutParams params =
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
/* height */ 0,
/* weight */ 1);
linearLayout.addView(gridView, params);

I programmatically created a TextView in TableLayout, it automatically sets the height and width

I programmatically created a TextView in TableLayout, it automatically sets the height and width.
Can I change the height and width of the TextView manually inside the TableLayout?
Use
TableLayout.LayoutParams params = new TableLayout.LayoutParams();
params.height = HEIGHT;
params.width = WIDTH;
textView.setLayoutParams(params);
same for other parameters like margin, padding etc
Use
LayoutParams lParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 1);
lParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);
You can set the height and width programmatically using the following code.
TableRow.LayoutParams param = new TableRow.LayoutParams();
paramColumn1.height = TableRow.LayoutParams.WRAP_CONTENT;
paramColumn1.width = TableRow.LayoutParams.WRAP_CONTENT;
textView1.setLayoutParams(param);
You can also set other properties using this way.

Android: Align Parent Bottom + Bottom margin programmatically

How can I programatically add a RelativeLayout that is aligned to the bottom of parent, and add a margin or padding in the same RelativeLayout?
Example:
Here's how you can do that:
// Get the parent layout
RelativeLayout parent = (RelativeLayout) findViewById(R.id.parent);
// Create your custom layout
RelativeLayout relativeLayout = new RelativeLayout(this);
// Create LayoutParams for it // Note 200 200 is width, height in pixels
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(200, 200);
// Align bottom-right, and add bottom-margin
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.bottomMargin = 100;
relativeLayout.setLayoutParams(params);
relativeLayout.setBackgroundColor(Color.BLUE);
// Add the custom layout to your parent layout
parent.addView(relativeLayout);
you can try this it work in my case:
RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) view.getLayoutParams();
// position on right bottom
rlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 0);
rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP,0);
rlp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);

Weight inside Scrollview

I'm having trouble calculating the weight inside of an scroll view on my android app. I want the app's content to resize proportionally to the screen size. How can I calculate the weight inside a scroll view?
Here some of my code:
LinearLayout layout_principal = (LinearLayout) findViewById(R.id.layout_items);// is horizontal layout and layout of Scrollview
LinearLayout layout_titulo_opcion = new LinearLayout(this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
0,
1f);
layout_titulo_opcion.setLayoutParams(layoutParams);
LinearLayout layout_titulo_opcion2 = new LinearLayout(this);
LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
0,
1f);
layout_titulo_opcion2.setLayoutParams(layoutParams2);
layout_principal.addView(layout_titulo_opcion);
layout_principal.addView(layout_titulo_opcion2);
To get/set the weight, use this:
LinearLayout.LayoutParams mylp = (LinearLayout.LayoutParams)myLayout.getLayoutParams();
int myWeight = mylp.weight; // get weight
mylp.weight = 4; // set weight
To set the weight sum, use this:
myLayout.setWeightSum(10);

Programmatically set button width to 50% of parent LinearLayout

I created a Dialog using LinearLayout and in this Dialog, I have a Button. I want to set its width to 50% of the Dialog's width, but no matter what I do, the button is still filling the entire width. The main problem seems to be that I cannot set the button's weight programmatically. I tried creating a second horizontal LinearLayout that would hold the button with weight set to 0.5f (the parent has a weightSum of 1), but that did not change anything. How can I set the button to be half the width?
I've looked at all the other questions but most of them were using xml so they were not relevant, and the one that solved it programmatically was using hardcoded values in pixels which is obviously an incorrect solution to the problem.
EDIT: Adding some code
LinearLayout linearLayout = new LinearLayout(mContext);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setWeightSum(1);
// I use these params when I call addContentView
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
testButton = new Button(mContext);
testButton.setText("Test");
testButton.setId(1);
testButton.setWidth(0);
testButton.setGravity(Gravity.CENTER);
LinearLayout buttonLayout = new LinearLayout(mContext);
butonLayout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 0.5f);
testButton.setLayoutParams(buttonParams);
buttonLayout.addView(testButton);
linearLayout.addView(buttonLayout);
...
this.addContentView(scrollView, layoutParams);
As I tried more and more options, the code became a mess. I decided to start from scratch and this code worked in the end:
LinearLayout forButton = new LinearLayout(mContext);
forButton.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams forButtonParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
forButton.setGravity(Gravity.CENTER);
DisplayMetrics dm = new DisplayMetrics();
this.getWindow().getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
forButton.addView(testButton);
testButton.getLayoutParams().width = width/2;
Are you setting the width value to 0dp as well? This is required for the layout engine to automatically to use layout weight respectively.
Edit
I think this line will get you the results you are looking for:
LayoutParams buttonParams = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 0.5f);
To find dialog width
DisplayMetrics dm = new DisplayMetrics();
dialog.getWindow().getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels ;
To Set Value programmatically
final LayoutParams lparams = new LayoutParams(Height,width/2); // Width , height
final Button button = new Button(this);
button.setLayoutParams(lparams);
EDIT
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(alertDialog.getWindow().getAttributes());
lp.width = 200;
lp.height = 200;
alertDialog.getWindow().setAttributes(lp);

Categories

Resources