user2111639
user2111639

Reputation: 297

Using two spaces to place an object in the middle of the screen in a horizontal linearlayout

I saw this link and answer of bakwarte. Center two buttons horizontally

Toni Gamez said, This is for API >= 14.

I tried and saw that it works for level 10, for example. So could this be the right way for all API levels?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_margin="10dp">
        <Space
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1" />
        <ImageView
            android:id="@+id/imageView1"
            android:background="@drawable/Icon"
            android:layout_width="100dp"
            android:layout_height="wrap_content" />
        <Space
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1" />
    </LinearLayout>
</LinearLayout>

Upvotes: 0

Views: 35

Answers (1)

Cheticamp
Cheticamp

Reputation: 62831

Toni Gamez doesn't say why the answer is only for API >= 14, but it is probably because the Space widget was first introduced in API 14.

This technique will work for you on API 10, and other APIs, if you are picking up the Space widget from the support libraries or Androidx.

Upvotes: 1

Related Questions