Android: Retrieve layout_marginBottom programmatically? - android

In my a LinearLayout I have set android:layout_marginBottom in my XML. I just want to retrieve that value.
Just LayoutParams doesn't give me access to bottomMargin . I tried to typecast :
row1 = (LinearLayout) findViewById(R.id.mainrow1);
LinearLayout.LayoutParams row1Params = (android.widget.LinearLayout.LayoutParams) row1.getLayoutParams();
but this gives me Runtime exception ClassCastException.
The main aim is I got buttons in nested LinearLayouts & I want to set height/width of LinearLayout programmatically that will inturn auto set height of buttons. I have added bottomMargin after 1st layout - so there is space between 2 rows.

Change Your layout of Button according to following logic,
set Layout_Width to 0dip
set Layout_Weight to 1
set Layout_Height to Wrap_Content

try this. When you get layout, R.layout. is best option. It is working for me.
row1 = (LinearLayout) findViewById(R.layout.mainrow1);

Related

Create buttons side by side dynamically

I have the following method which I call a few times to create a list of buttons. It works well and creates the buttons.
public void CreateButton(int i) {
LinearLayout btnLayout = (LinearLayout)findViewById(R.id.btnLayout);
Button btn = new Button(this);
btn.setId(i);
btn.setText(String.valueOf(i+1));
btnLayout.addView(btn);
}
But each created button is fitting the screen in width, and I would want it to stay side by side, two buttons per row. I managed to set the button to half the screen size using this:
int displaywidth= getResources().getDisplayMetrics().widthPixels;
btn.setLayoutParams(new LayoutParams((int)(displaywidth/2), LayoutParams.WRAP_CONTENT));
It makes the button's width to be half the screen's size, but I can't figure out how to place them side by side. Any help is appreciated.
Change your orientation in your LinearLayout to horizontal.
<LinearLayout
...
android:orientation="horizontal"
... >
...
</LinearLayout>
If you only have a single LinearLayout that is to hold multiple side-by-side buttons you can make horizontal LinearLayouts to hold your button pairs and either nest them in the main veritical layout or utilize another layout, for example RelativeLayout, to get the desired results.
Try using weight property of LinearLayout. If in each row you want only two buttons then give
android:weightSum="1"
to LinearLayout and
android:layout_weight="0.5"
to each button or you can set weight dynamically in your code by
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT, xf);
where x represents the float value of layout weight of your button.
for more details about layout weight http://developer.android.com/guide/topics/ui/layout/linear.html
One way to solve the problem is by using their weight and width. Make sure the Linear Layout has a horizontal orientation. Then, use Layout_params to set their width to "Wrap_Content" and their weight to "1". Then both will automatically take up the same amount of space.

Setting RelativeLayout.ALIGN_PARENT_LEFT to LinearLayout

How can I set the RelativeLayout.ALIGN_PARENT_LEFT to LinearLayout in code? I have a RelativeLayout which child is LinearLayout and I need to set the RelativeLayout.ALIGN_PARENT_LEFT in the child LinearLayout instance. The problem is that when I set it, I will lose the child LinearLayout parameters (I will lose the weight). My understanding is that the only way to set the RelativeLayout.ALIGN_PARENT_LEFT to child element is to use RelativeLayout.Layoutparams when setting the child view layout parameters.
Thanks in advance!!
You have to set the width height parms and weight along with your aligh left rule in programtically
layoutparms = new LayoutParams(WindowManager.LayoutParams.FILL_PARENT,
WindowManager.LayoutParams.FILL_PARENT, weightthere);
layoutparms.addRule(RelativeLayout.LEFT_OF, leftisdeview);
yourview.setLayoutParams(layoutparms);
You just have to give android:gravity="left" to the parent linear layout.Remember that it is gravity not layout_gravity.

edit layout of existing child in RelativeLayout

I have a RelativeLayout with a number of children. The RelativeLayout is the main layout of my activity. I need to add to one of the children the layout value layout_alignParentBottom="true". I need to do this programmatically. the link How to set the android property layout_alignParentBottom to a button dynamically is not working for me because, basically, I don't want to have to inflate the RelativeLayout as I already have it at the setContentView(...). Any ideas how to do this?
I figured it out:
RelativeLayout.LayoutParams layout = (LayoutParams) theChild.getLayoutParams();
layout.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);

How can I set in the code height for inner LinearLayouts to be exactly 1/5 of its parent LinearLayout?

I define all GUI components in the code without xml layout file. I have main linear layout and 5 inner layouts into it. The code:
LinearLayout llMain = new LinearLayout (this);
llMain.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT ));
llMain. setOrientation (LinearLayout.VERTICAL);
llMain.setWeightSum(1.0f);
LinearLayout ll1 = new LinearLayout(this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
lp.weight = 0.2f
ll1.setLayoutParams(lp);
llMain.addView(ll1);
....
....
But it doesn't give the expected result. The ll1 inner layout has about 70% of the height of llMain layout instead of 20%. So, how can I set height of inner ll1 layout to be exactly 1/5 of the height of its parent llMain layout? Thanks.
I think it should be
lp.weight = 0.2f;
Can you try this and let us know the result?
If I dont' remember wrong there's a really cool trick about this. I remember it from Google I/O talks. You have to give to all members of the content layout_height equals 0 and then you have to use layout_weight to give the correct percentage (in this case layout_weight=20).
Try to do this trick :)

Why my edit text is not taking gravity...?

I am adding one edit text pro-grammatically, in that i am setting the gravity but its not reflecting.
code:
EditText bcc = new EditText(getApplicationContext());
LayoutParams para = new LayoutParams(LayoutParams.FILL_PARENT, 45);
//bcc.setBackgroundColor(Color.parseColor("#00000000"));
bcc.setTextColor(Color.parseColor("#000000"));
bcc.setSingleLine(true);
para.setMargins(0, 0, 0, 5); // left, top, right, bottom.
bcc.setTextSize(15);
bcc.setGravity(Gravity.BOTTOM);
bcc.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
bcc.setId(100);
bcc.setLayoutParams(para);
This gravity bcc.setGravity(Gravity.BOTTOM); marks only how text should lay inside EditText.
If parent of EditText is RelativeLayout you can provide rules inside RelativeLayout.LayoutParams.
set gravity of parent of the view. If view parent is layout then the code will be like the following
((LinearLayout) bcc.getParent()).setGravity(Gravity.CENTER_VERTICAL);
Eventually you would be adding this EditText bcc to a view group? Depending on what type of ViewGroup the parent is, you would need to do the following:
LinearLayout:
via XML:
You have to set android:layout_gravity="center_vertical".
via code:
LinearLayout.LayoutParams lp = viewGroup.getLayoutParams();
lp.gravity = Gravity.CENTER_VERTICAL;
viewGroup.setLayoutParams(lp);
The code will be different for different parent layout types.
When you creating EditText programmatically, you must at first set setKeyListener(TextKeyListener.getInstance());
Otherwise your view will always be aligned with Gravity.TOP.
I don't know real reason, but before you specifiy any other parameter to EditText, you must set setKeyListener(TextKeyListener.getInstance());
Correction : It only work if you create you custom widget by extending EditText and defining your widget in XML. Only tested on Android 5.0.1

Categories

Resources