I am using a linear layout to display a pretty light initial screen. It has 1 button that is supposed to centre in the screen both horizontally and vertically. However no
You also can Try this Code:
<LinearLayout
android:id="@+id/linear_layout"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_gravity="center"
android:orientation="horizontal"
android:weightSum="2.0"
android:background="@drawable/anyBackground" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical"
android:layout_weight="1" >
<ImageView
android:id="@+id/img_mail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/yourImage" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical"
android:layout_weight="1" >
<ImageView
android:id="@+id/img_save"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="@drawable/yourImage" />
</LinearLayout>
</LinearLayout>
Set to true android:layout_alignParentTop="true"
and android:layout_centerHorizontal="true"
in the Button, like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:id="@+id/switch_flashlight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/turn_on_flashlight"
android:textColor="@android:color/black"
android:onClick="action_trn"
android:background="@android:color/holo_green_light"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:padding="5dp" />
</RelativeLayout>
easy with this
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:visibility="visible"
android:gravity="center"
android:orientation="vertical" >
<ProgressBar
android:id="@+id/pbEndTrip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="Gettings" />
</LinearLayout>
just use ( to make it in the center of your layout)
android:layout_gravity="center"
and use
android:layout_marginBottom="80dp"
android:layout_marginTop="80dp"
to change postion
You can use the RelativeLayout
.
complete and working sample from my machine...
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity"
android:gravity="center"
android:textAlignment="center">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="My Apps!"
android:id="@+id/textView"
android:gravity="center"
android:layout_marginBottom="20dp"
/>
<Button
android:layout_width="220dp"
android:layout_height="wrap_content"
android:text="SPOTIFY STREAMER"
android:id="@+id/button_spotify"
android:gravity="center"
android:layout_below="@+id/textView"
android:padding="20dp"
/>
<Button
android:layout_width="220dp"
android:layout_height="wrap_content"
android:text="SCORES"
android:id="@+id/button_scores"
android:gravity="center"
android:layout_below="@+id/textView"
android:padding="20dp"
/>
<Button
android:layout_width="220dp"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="LIBRARY APP"
android:id="@+id/button_library"
android:gravity="center"
android:layout_below="@+id/textView"
android:padding="20dp"
/>
<Button
android:layout_width="220dp"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="BUILD IT BIGGER"
android:id="@+id/button_buildit"
android:gravity="center"
android:layout_below="@+id/textView"
android:padding="20dp"
/>
<Button
android:layout_width="220dp"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="BACON READER"
android:id="@+id/button_bacon"
android:gravity="center"
android:layout_below="@+id/textView"
android:padding="20dp"
/>
<Button
android:layout_width="220dp"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="CAPSTONE: MY OWN APP"
android:id="@+id/button_capstone"
android:gravity="center"
android:layout_below="@+id/textView"
android:padding="20dp"
/>
</LinearLayout>