How to add views to LinearLayout, but from bottom upwards? - android

It is possible to add views to LinearLayout one after another in upward direction?

You can add it programmatically with:
LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
layout.addView(newView, index);
You just have to add always with index = 0
Is this the answer to your question?

If you want to add views to LinearLayout in upward direction(i.e. from bottom to up) use
android:gravity="bottom"
property in the xml of LinearLayout and then add views in the layout.
Hope this will solve your problem.

If you want to keep your views top, relativelayout is another choice.

Related

How to put an image on two different layouts

How can I achieve this layout?
Use a FrameLayout that act as a parent of a LinearLayout( say layout ll_par) & an ImageView..In your ll_par add your two remaining layout .& remember put the Imageview as the last child of FrameLayout & set its layout_gravity to right|center_vertical & give some margin to your Imageview according to the need ..

Remove marginetop a relative layout dynamically if another layout is generated in between two relative layout

I have two relative layout say relative layout1 & 2 having an edit text and a button. In order to provide spacing in between I specified margin top of 30dp to relative layout2. Another relative layout 3 is generated dynamically in between them when we click button of the relative layout1. If so I have to remove margin top of relative layout2 programatically.
snapshot is like below
In this case a provide padding in xml. But when another layout is added in between like this
the padding is not preferable. I have to remove padding programmatically. How can i do that by checking whether there is a layout in between and remove that padding. I am a beginner in android coding....
You can set margins, or anything else regarding a layout like this:
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)relativeLayout.getLayoutParams();
layoutParams.setMargins(80, 0, 0, 0); // left, top, right, bottom
relativeLayout.setLayoutParams(layoutParams);
You can find more Information on LayoutParams here:
http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html
If you want to remove padding call
relativeLayout.setPadding(0,0,0,0);

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 to set two layout one at top and anther at bottom

hi I have 2 layout inside a framelayout and i want one linearlayout at top and another at bottom where i will specify height for both the layouts
and how to set this parameter for the linearLayout "layout_gravity " by code not by XML
Use a surfaceView , the SurfaceView allows you to stack in the Z order.
Docs
make sure your surfaceView is under the views of the views you want to stack.
Hope this helps
first fetch the layout from id.
LinearLayout layout = (LinearLayout)findViewById(R.id.IDOFLAYOUT);
//the set the gravity by this method
layout.setGravity(Gravity.CENTER);

Can I add views to a scrollview without inflating/re-inflating layouts?

I would like to take an existing ScrollView with a view in it, and add more views, dynamically (at runtime) to the ScrollView container.
Is it possible to add these views without having to create a new layout and inflate it? If so, what's the general process for adding these views dynamically?
For the sake of this question, assume the views are TextView...
Thanks!
A ScrollView can only have one child, so it doesn't make sense to add more children to it directly. Lets say your ScrollView has a LinearLayout inside of it, then you can add more views to the LinearLayout:
LinearLayout layout = findViewById(R.id.my_linear_layout);
TextView textView = new TextView(this);
layout.addView(textView);

Categories

Resources