Android Space Tutorial with Examples

View more Tutorials:

Websites to learn foreign languages for free:
Follow us on our fanpages to receive notifications every time there are new articles. Facebook Twitter

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.

    android:layout_height="wrap_content"  />

    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);
linearLayout.addView(button1, layoutParams);

// Create a Space View.
Space space = new Space(this);

Button button2 = new Button(this);
button2.setText("Long Button");

View more Tutorials: