o7planning

Android Space Tutorial with Examples

  1. Android Space

1. Android Space

In Android, Space is a lightweight subclass of View class. It is normally placed between two components in the interface in order to create some space between these two components.
<Space
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"  />

<Space
    android:layout_width="10dp"
    android:layout_height="wrap_content" />
For example: A Space is put into a LinearLayout (horizontally) as the first child View with the aim of creating the blank space on the left side.
For example: Add two Space into a LinearLayout as the first and the last child View to align other child View(s) to the center of the LinearLayout.
For example, add Space into the interface components to create space among them:
Java code: Add a Space into a LinearLayout:
// linearLayout = new LinearLayout(MainActivity.this);
// linearLayout.setOrientation(LinearLayout.HORIZONTAL);

LinearLayout.LayoutParams layoutParams
        = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
        LinearLayout.LayoutParams.WRAP_CONTENT, 0);

LinearLayout.LayoutParams layoutParams2
        = new LinearLayout.LayoutParams(15,
        LinearLayout.LayoutParams.WRAP_CONTENT, 0);

Button button1 = new Button(this);
button1.setText("Button");
linearLayout.addView(button1, layoutParams);

// Create a Space View.
Space space = new Space(this);
space.setLayoutParams(layoutParams2);
linearLayout.addView(space);

Button button2 = new Button(this);
button2.setText("Long Button");
button2.setLayoutParams(layoutParams);
linearLayout.addView(button2);

Android Programming Tutorials

Show More