Adding Layout with views programmatically

Pauline Hicks

Pretty much I have 1 main layout being a Vertical LinearLayout, at the press of a button I want to be able to add a horizontal layout with 2 edit texts. What i've done here doesn't work, no errors but nothing happens.

public void addView(View v){
    LinearLayout mainLayout =(LinearLayout)findViewById(R.id.activity_main);

    LinearLayout h = new LinearLayout(this);

    h.setOrientation(LinearLayout.HORIZONTAL);
    h.addView(new EditText(this));
    h.addView(new EditText(this));

    mainLayout.addView(h);
}
lelloman

one thing that you should consider when creating a View programmatically is to set a LayoutParams, e.g.

LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WrapContent);
view.setLayoutParams(layoutParams);

this way you define the size of the view inside the layout, it's what you would usually set with layout_width and layout_height attributes in an xml layout

in your case you should add a LayoutParams for the LinearLayout and one for each the EditText

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Relative Layout adding views programmatically one below other not aligning correctly

Programmatically adding views to a UIStackView

Adding multiple custom views inside Layout programmatically using multi lines if required

Should repetitive views be drawn in xml layout or programmatically?

Setting layout constraints on a set of views programmatically

Add multiple custom views to layout programmatically

Android: Add views programmatically and layout is not obeyed

Adding views programmatically. displays nothing

Adding views programmatically to LinearLayout but they don't appear

Swift - Adding labels to stack views programmatically

Adding views programmatically cause change of size in Android

Adding Views. Storyboard VS. Programmatically

Modify/Adding element in layout from views

Adding views to the layout programatically (and inflating) is very slow

adding a fragments multiple times in linear layout programmatically

Programmatically adding a grid of buttons inside Relative layout

Adding views to LinearLayout programmatically not setting text on all but one

Scrolling with ScrollView not working after adding views programmatically to hierarchy

Views not displaying correctly after adding them to NSStackView programmatically

Adding programatically created views into scrollview vertically (Linear layout in iOS)

Adding subview to UICollectionView using auto layout programmatically not working

Auto Layout programmatically: Update view's vertical space moving all inner views

Adding constraints to views programmatically using ConstraintSet does not give expected result in Android?

Swift - StackView and Adding Constraints With Programmatically Added Views To Stack - Adjust Cell Hight After Load?

Adapter not creating views in programmatically created recycler view after adding item decoration

Programmatically generated views

Stacking views programmatically in UIStackView

Combining views programmatically - Android

Add constraint programmatically to Views