I have three imageButtons inside an horizontal linearlayout and I want to add one more imageButton right under the up left imageButton(when the up left imageButton starts and finishes, I want the same thing for the imageButton under that). Everything is in a vertital linearlayout. I cannot align the down imageButton vertically with the up one. How can I do that? Any ideas?
My code is this:
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp"
android:gravity="center">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton11"
android:background="@drawable/image"
android:layout_gravity="center"
android:layout_marginRight="30dp"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton12"
android:background="@drawable/image"
android:layout_gravity="center"
android:layout_marginRight="30dp"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton13"
android:background="@drawable/image"
android:layout_gravity="center"
android:layout_marginRight="30dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp"
android:gravity="left">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton14"
android:background="@drawable/image"
android:layout_marginRight="30dp"/>
</LinearLayout>
You can do this more easily with a RelativeLayout than via a TableLayout or a LinearLayout. If you are willing to use fixed widths for your imageButtons (Which should not be a problem for imageButtons), here is one layout which will solve your problem.
(I have used Buttons in the layout below for the sake of simplicity, but it will work the same for imageButtons as well.)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal">
<Button
android:layout_width="120dp"
android:layout_height="wrap_content"
android:id="@+id/btn1"
android:text="Button1" />
<Button
android:layout_width="120dp"
android:layout_height="wrap_content"
android:id="@+id/btn2"
android:layout_toRightOf="@id/btn1"
android:text="Button2" />
<Button
android:layout_width="120dp"
android:layout_height="wrap_content"
android:id="@+id/btn3"
android:layout_toRightOf="@id/btn2"
android:text="Button3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button4"
android:id="@+id/btn4"
android:layout_below="@id/btn1"
android:layout_alignLeft="@id/btn1"
android:layout_alignRight="@id/btn1"/>
</RelativeLayout>