How to add Margin/padding in LinearLayout programmatically?
How to add Margin/padding in LinearLayout programmatically?
I am trying to set padding to my linear layouts so that when I press 'create row' they have a gap between them each.
I tried adding marginTop and marginBottom to my LinearLayout but when I run the code, the layout is still spawn in together. not with a gap from the margin I set.
marginTop
marginBottom
LinearLayout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/verticalLayout"
android:layout_below="@id/button"
android:background="@color/colorPrimaryDark"
android:layout_marginTop="50dp"
>

(the above image shows 3 linear layouts I added by pressing create row)
Edit: I tried adding a marginRight to it, just to see if anything worked on it, and it did.
marginRight
public class MainActivity extends AppCompatActivity
LinearLayout verticallayout,horizontalLayout; //got
@Override
protected void onCreate(@Nullable Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
verticallayout = findViewById(R.id.verticalLayout); //got
public void createRow(View view)
createRow(); //got
private void createRow() //got
horizontalLayout = new LinearLayout(this);
LinearLayout.LayoutParams horizontalParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
horizontalLayout.setOrientation(LinearLayout.HORIZONTAL);
horizontalParams.setMargins(15, 5, 15, 5); // LEFT, TOP, RIGHT, BOTTOM
horizontalLayout.setLayoutParams(horizontalParams);
createSpinner();
createCheckbox();
createEditText();
verticallayout.addView(horizontalLayout, horizontalParams);
private void createEditText()
EditText editText = new EditText(this);
LinearLayout.LayoutParams editParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
editParams.setMargins(10, 10, 10, 10);
editText.setLayoutParams(editParams);
horizontalLayout.addView(editText);
private void createCheckbox() //done
CheckBox checkBox = new CheckBox(this);
LinearLayout.LayoutParams checkParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
checkParams.setMargins(10, 10, 10, 10);
checkParams.gravity = Gravity.LEFT;
checkBox.setLayoutParams(checkParams);
horizontalLayout.addView(checkBox);
private void createSpinner()
Spinner spinner = new Spinner(this);
//ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item, spinnerArray);
//spinner.setAdapter(spinnerArrayAdapter);
LinearLayout.LayoutParams spinnerParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
spinnerParams.setMargins(10, 10, 10, 10);
spinnerParams.gravity = Gravity.LEFT;
spinner.setLayoutParams(spinnerParams);
horizontalLayout.addView(spinner);
Edited in post .
– user8341034
Sep 6 '18 at 6:07
You are creating a new
LinearLayout, and not inflating from the xml.– ColonD
Sep 6 '18 at 6:07
LinearLayout
You aren't adding any margin when you add the views programatically. You realize that adding a margin on a view puts margin on the view within its parent, right? So applying a margin to verticalLayout will NOT add margin to its children
– Gabe Sechan
Sep 6 '18 at 6:08
Margin should be added to the child view, and not the parent
– ColonD
Sep 6 '18 at 6:09
2 Answers
2
Try this one code
private void createRow() //got
horizontalLayout = new LinearLayout(this);
LinearLayout.LayoutParams horizontalParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
horizontalLayout.setOrientation(LinearLayout.HORIZONTAL);
horizontalParams.setMargins(15, 5, 15, 5); // LEFT, TOP, RIGHT, BOTTOM
horizontalLayout.setLayoutParams(horizontalParams);
horizontalLayout.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.colorPrimaryDark));
createSpinner();
createCheckbox();
createEditText();
verticallayout.addView(horizontalLayout, horizontalParams);
hope so it will be useful for you. :)
hmm no that didn't change the spacing between the linear layouts..
– user8341034
Sep 6 '18 at 6:12
@Magic_Whizz please add createSpinner(); createCheckbox(); createEditText(); these function code also
– X-Byte Technolabs pvt. ltd.
Sep 6 '18 at 6:20
@Magic_Whizz plz check my edited answer
– X-Byte Technolabs pvt. ltd.
Sep 6 '18 at 6:27
yes bro i added code for that in my answer check my answer you will find new line setBackgroundColor
– X-Byte Technolabs pvt. ltd.
Sep 6 '18 at 6:44
@Magic_Whizz you welcome
– X-Byte Technolabs pvt. ltd.
Sep 6 '18 at 6:53
Add margin to horizontal layout not in Vertical layout. check following code:
private void createRow()
horizontalLayout = new LinearLayout(this);
LinearLayout.LayoutParams horizontalParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
horizontalParams.topMargin = 20; //add margin here.
horizontalLayout.setOrientation(LinearLayout.HORIZONTAL);
horizontalLayout.setLayoutParams(horizontalParams);
createSpinner();
createCheckbox();
createEditText();
verticallayout.addView(horizontalLayout);
that didn't work either :/ the layouts are still joined together. no space between them
– user8341034
Sep 6 '18 at 6:14
post new screen shot.
– Abhay Koradiya
Sep 6 '18 at 6:17
imgur.com/a/jzlcwcb
– user8341034
Sep 6 '18 at 6:20
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
add method which you use to add view programmatically.
– Abhay Koradiya
Sep 6 '18 at 6:06