How to align buttons in Android Studio - java

I have just started learning to programme for Android in Java and as my first program, I decided to write a simple TicTacToe game. Since I am new to Android Studio I don't know how the alignment works here.
I am perfectly satisfied with the alignment of buttons in content_main.xml but for some reason when I run the code in the emulator they get shifted to the left.
Just in case if it might come in handy I attach the xml code to this post
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.anar.testnewest.MainActivity"
tools:showIn="#layout/activity_main">
<RelativeLayout
android:layout_width="368dp"
android:layout_height="442dp"
android:layout_centerInParent="true"
android:textAlignment="center"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="34dp">
<Button
android:id="#+id/btn1"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="99dp"
android:onClick="btn1" />
<Button
android:id="#+id/btn2"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/btn1"
android:layout_alignBottom="#+id/btn1"
android:layout_toLeftOf="#+id/btn3"
android:layout_toStartOf="#+id/btn3"
android:onClick="btn2" />
<Button
android:id="#+id/btn3"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/btn2"
android:layout_alignBottom="#+id/btn2"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:onClick="btn3" />
<Button
android:id="#+id/btn4"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/btn1"
android:layout_marginTop="26dp"
android:onClick="btn4" />
<Button
android:id="#+id/btn5"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/btn4"
android:layout_alignBottom="#+id/btn4"
android:layout_centerHorizontal="true"
android:onClick="btn5" />
<Button
android:id="#+id/btn6"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/btn5"
android:layout_alignEnd="#+id/btn3"
android:layout_alignRight="#+id/btn3"
android:onClick="btn6" />
<Button
android:id="#+id/btn7"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/btn4"
android:layout_alignRight="#+id/btn4"
android:layout_below="#+id/btn4"
android:layout_marginTop="22dp"
android:onClick="btn7" />
<Button
android:id="#+id/btn8"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/btn7"
android:layout_alignBottom="#+id/btn7"
android:layout_alignLeft="#+id/btn5"
android:layout_alignStart="#+id/btn5"
android:onClick="btn8" />
<Button
android:id="#+id/btn9"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/btn8"
android:layout_alignBottom="#+id/btn8"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:onClick="btn9" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="70dp"
android:text="TicTacToe"
android:textColor="#color/colorAccent"
android:textSize="40sp" />
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
Thanks in advance for your time and help guys :)

If you want to the right layout .
Just add app:layout_constraintLeft_toLeftOf="parent" and app:layout_constraintTop_toTopOf="parent" to your RelativeLayout .
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<RelativeLayout
android:layout_width="368dp"
android:layout_height="442dp"
android:layout_centerInParent="true"
android:textAlignment="center"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent">
<Button
android:id="#+id/btn1"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="99dp"
android:onClick="btn1"/>
<Button
android:id="#+id/btn2"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/btn1"
android:layout_alignBottom="#+id/btn1"
android:layout_toLeftOf="#+id/btn3"
android:layout_toStartOf="#+id/btn3"
android:onClick="btn2"/>
<Button
android:id="#+id/btn3"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/btn2"
android:layout_alignBottom="#+id/btn2"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:onClick="btn3"/>
<Button
android:id="#+id/btn4"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/btn1"
android:layout_marginTop="26dp"
android:onClick="btn4"/>
<Button
android:id="#+id/btn5"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/btn4"
android:layout_alignBottom="#+id/btn4"
android:layout_centerHorizontal="true"
android:onClick="btn5"/>
<Button
android:id="#+id/btn6"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/btn5"
android:layout_alignEnd="#+id/btn3"
android:layout_alignRight="#+id/btn3"
android:onClick="btn6"/>
<Button
android:id="#+id/btn7"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/btn4"
android:layout_alignRight="#+id/btn4"
android:layout_below="#+id/btn4"
android:layout_marginTop="22dp"
android:onClick="btn7"/>
<Button
android:id="#+id/btn8"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/btn7"
android:layout_alignBottom="#+id/btn7"
android:layout_alignLeft="#+id/btn5"
android:layout_alignStart="#+id/btn5"
android:onClick="btn8"/>
<Button
android:id="#+id/btn9"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/btn8"
android:layout_alignBottom="#+id/btn8"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:onClick="btn9"/>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="70dp"
android:text="TicTacToe"
android:textColor="#color/colorAccent"
android:textSize="40sp"/>
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
Output

Just add these properties to RelativeLayout,
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
It is better to have Start and End than Left and Right properties. B'cos it also supports RTL layouts.

Related

android studio unable to start activity ComponentInfo

java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.joystick.myapplication/com.example.joystick.myapplication.MainActivity}:
android.view.InflateException: Binary XML file line #2: Binary XML
file line #2: Error inflating class
android.support.constraint.ConstraintLayout
i keep getting this error
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp">
<Button
android:id="#+id/sendButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/writeMsg"
android:layout_alignBottom="#+id/writeMsg"
android:layout_toEndOf="#+id/onOff"
android:layout_toRightOf="#+id/onOff"
android:text="Send"
tools:layout_editor_absoluteX="139dp"
tools:layout_editor_absoluteY="111dp" />
<ListView
android:id="#+id/peerListView"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="260dp" />
<EditText
android:id="#+id/writeMsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginLeft="63dp"
android:layout_marginStart="63dp"
android:layout_toEndOf="#+id/back"
android:layout_toRightOf="#+id/back"
android:ems="10"
android:inputType="textPersonName"
tools:layout_editor_absoluteX="35dp"
tools:layout_editor_absoluteY="429dp" />
<Button
android:id="#+id/onOff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/connectionStatus"
android:layout_marginLeft="75dp"
android:layout_marginStart="75dp"
android:layout_toEndOf="#+id/readMsg"
android:layout_toRightOf="#+id/readMsg"
android:text="Wifi On"
tools:layout_editor_absoluteX="73dp"
tools:layout_editor_absoluteY="77dp" />
<Button
android:id="#+id/discover"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/readMsg"
android:layout_alignRight="#+id/readMsg"
android:layout_below="#+id/connectionStatus"
android:text="discover"
tools:layout_editor_absoluteX="155dp"
tools:layout_editor_absoluteY="0dp" />
<TextView
android:id="#+id/readMsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/writeMsg"
android:layout_alignEnd="#+id/writeMsg"
android:layout_alignRight="#+id/writeMsg"
android:layout_marginBottom="24dp"
android:layout_marginEnd="65dp"
android:layout_marginRight="65dp"
android:text="Message"
android:textAlignment="center"
android:textSize="20sp"
android:textStyle="italic"
tools:layout_editor_absoluteX="132dp"
tools:layout_editor_absoluteY="373dp" />
<TextView
android:id="#+id/connectionStatus"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:text="Connection Status"
android:textAlignment="center"
android:textColor="#android:color/holo_blue_dark"
android:textSize="18sp"
android:textStyle="italic"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="16dp" />
<Button
android:id="#+id/back"
style="#style/Widget.AppCompat.Button"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="#drawable/back"
android:visibility="visible" />
</RelativeLayout>
It is a long shot but i think that you are trying to load a big(size in MB) picture. What I can recommend is to reduce the size of the picture with 50% and I think your problem it will be fixed.
Goodluck :)
EDIT:
Here is how it looks when i runt it on 5.5 inch device:
It is running good also oon my phone. So please check the MB size of the following pictures:
"#drawable/drone
and
"#drawable/image_button_bg"
EDIT:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/sendButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/writeMsg"
android:layout_alignBottom="#+id/writeMsg"
android:layout_toEndOf="#+id/onOff"
android:text="Send"/>
<ListView
android:id="#+id/peerListView"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"/>
<EditText
android:id="#+id/writeMsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginStart="63dp"
android:layout_toEndOf="#+id/back"
android:ems="10"
android:inputType="textPersonName"/>
<Button
android:id="#+id/onOff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/connectionStatus"
android:layout_marginStart="75dp"
android:layout_toEndOf="#+id/readMsg"
android:text="Wifi On" />
<Button
android:id="#+id/discover"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/readMsg"
android:layout_below="#+id/connectionStatus"
android:text="discover"/>
<TextView
android:id="#+id/readMsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/writeMsg"
android:layout_alignEnd="#+id/writeMsg"
android:layout_marginBottom="24dp"
android:layout_marginEnd="65dp"
android:text="Message"
android:textAlignment="center"
android:textSize="20sp"
android:textStyle="italic"/>
<TextView
android:id="#+id/connectionStatus"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:text="Connection Status"
android:textAlignment="center"
android:textColor="#android:color/holo_blue_dark"
android:textSize="18sp"
android:textStyle="italic"/>
<Button
android:id="#+id/back"
style="#style/Widget.AppCompat.Button"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="#drawable/back"
android:visibility="visible" />
</RelativeLayout>
Ty this as XML and see what happens. If you still getting the error it means that the problem is somewhere else.

java.lang.IllegalStateException: Circular dependencies cannot exist in RelativeLayout

Helo All,
I am stuck in an error regarding circular dependencies for Relative Layout. Could anyone help me where I went wrong?
I am using multiple activity xmls but the error is occuring when 1st activity is being executed.
Here is my Relative layout code :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#5fb0c9"
android:orientation="vertical"
android:scrollbarAlwaysDrawVerticalTrack="true">
<TextView
android:id="#+id/login_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginTop="22dp"
android:gravity="center_horizontal"
android:text="Account Login"
android:textColor="#fff"
android:textSize="26sp"
android:textStyle="bold" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/login_title"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="70dp"
android:background="#fff"
android:elevation="4dp"
android:orientation="vertical"
android:padding="20dp"
android:id="#+id/relativeLayout">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="30dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:weightSum="1">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/editText_username"
android:layout_gravity="center_horizontal"
android:layout_weight="0.14"
android:hint="USER NAME"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/editText_location"
android:layout_weight="0.17"
android:hint="LOCATION"/>
<Button
android:id="#+id/user_confirm"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="22dp"
android:background="#d67601"
android:text="CONFIRM"
android:textAllCaps="false"
android:textColor="#fff"
android:textSize="18sp" />
</LinearLayout>
</RelativeLayout>
<ImageButton
android:id="#+id/user_profile_photo"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#drawable/user_profile_image_background"
android:elevation="6dp"
android:src="#drawable/pro"
android:layout_below="#+id/login_title"
android:layout_centerHorizontal="true" />
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/images"
android:orientation="vertical"
android:scrollbarAlwaysDrawVerticalTrack="true">
<Button
android:id ="#+id/node_scan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="NODE SCAN"
android:onClick="Scan"
android:background="#drawable/button_bg_rounded_corners"
android:padding="15dp"
android:shadowColor="#ffffff"
android:textColor="#ffffff"
android:layout_marginTop="105dp"
android:layout_below="#+id/contents"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="true" />
<Button
android:id ="#+id/post"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="POST"
android:onClick="post"
android:background="#drawable/button_bg_rounded_corners"
android:padding="15dp"
android:shadowColor="#ffffff"
android:textColor="#ffffff"
android:layout_alignTop="#+id/next"
android:layout_alignParentEnd="true" />
<Button
android:id ="#+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="NEXT"
android:onClick="next"
android:background="#drawable/button_bg_rounded_corners"
android:padding="15dp"
android:shadowColor="#ffffff"
android:textColor="#ffffff"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="73dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/contents"
android:layout_gravity="center_horizontal"
android:layout_weight="0.14"
android:hint=""
android:background="#fcfbfb"
android:layout_alignParentStart="true"
android:layout_above="#+id/post"
android:layout_below="#+id/node_scan" />
<Button
android:id ="#+id/quit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="QUIT"
android:onClick="quit"
android:background="#drawable/button_bg_rounded_corners"
android:padding="15dp"
android:shadowColor="#ffffff"
android:textColor="#ffffff"
android:layout_alignTop="#+id/post"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Thanks in Advance,
Alby
android:layout_below="#+id/login_title"
Here should be "#id/..." (whithout a plus sign), because it is a reference to an existing ID.
https://developer.android.com/guide/topics/ui/declaring-layout.html#id
The error was fixed after removing the below from Edit Text field: android:layout_above="#+id/post" android:layout_below="#+id/node_scan"

Android Java : Linear Layout vanishes when placed on another Linear Layout

I am working on the horizontal view for a basic Android calculator, and I have a LinearLayout called linearLayout2 that I am trying to place on top of linearLayout3, which contains the numbers 1, 2, 3, +, -, and (. The problem is that when I try to move linearLayout2 on top of linearLayout3, it immedietly vanishes to the top with 0 height.
Here is the XML code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:weightSum="1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/linearLayout">
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/editText"
android:hint="#string/hint"
android:textSize="14dp"
android:background="#00000000" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/linearLayout3"
android:orientation="horizontal"
android:weightSum="1"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<Button
android:text="1"
android:layout_width="0dp"
android:layout_weight=".16667"
android:textAllCaps="false"
android:id="#+id/button37"
android:layout_height="wrap_content" />
<Button
android:text="2"
android:layout_width="0dp"
android:layout_weight=".16667"
android:textAllCaps="false"
android:id="#+id/button39"
android:layout_height="wrap_content" />
<Button
android:text="3"
android:layout_width="0dp"
android:layout_weight=".16667"
android:textAllCaps="false"
android:id="#+id/button40"
android:layout_height="wrap_content" />
<Button
android:text="+"
android:layout_width="0dp"
android:layout_weight=".16667"
android:textAllCaps="false"
android:id="#+id/button46"
android:layout_height="wrap_content" />
<Button
android:text="-"
android:layout_width="0dp"
android:layout_weight=".16667"
android:textAllCaps="false"
android:id="#+id/button47"
android:layout_height="wrap_content" />
<Button
android:text="("
android:layout_width="0dp"
android:layout_weight=".16667"
android:textAllCaps="false"
android:id="#+id/button49"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/linearLayout4"
android:orientation="horizontal"
android:weightSum="1"
android:layout_above="#+id/linearLayout5"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<Button
android:text="4"
android:layout_width="0dp"
android:layout_weight=".16667"
android:textAllCaps="false"
android:id="#+id/button41"
android:layout_height="wrap_content" />
<Button
android:text="5"
android:layout_width="0dp"
android:layout_weight=".16667"
android:textAllCaps="false"
android:id="#+id/button42"
android:layout_height="wrap_content" />
<Button
android:text="6"
android:layout_width="0dp"
android:layout_weight=".16667"
android:textAllCaps="false"
android:id="#+id/button43"
android:layout_height="wrap_content" />
<Button
android:text="*"
android:layout_width="0dp"
android:layout_weight=".16667"
android:textAllCaps="false"
android:id="#+id/button35"
android:layout_height="wrap_content" />
<Button
android:text="/"
android:layout_width="0dp"
android:layout_weight=".16667"
android:textAllCaps="false"
android:id="#+id/button36"
android:layout_height="wrap_content" />
<Button
android:text=")"
android:layout_width="0dp"
android:layout_weight=".16667"
android:textAllCaps="false"
android:id="#+id/button38"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/linearLayout5"
android:orientation="horizontal"
android:weightSum="1"
android:layout_above="#+id/linearLayout6"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<Button
android:text="7"
android:layout_width="0dp"
android:layout_weight=".16667"
android:textAllCaps="false"
android:id="#+id/button44"
android:layout_height="wrap_content" />
<Button
android:text="8"
android:layout_width="0dp"
android:layout_weight=".16667"
android:textAllCaps="false"
android:id="#+id/button45"
android:layout_height="wrap_content" />
<Button
android:text="9"
android:layout_width="0dp"
android:layout_weight=".16667"
android:textAllCaps="false"
android:id="#+id/button48"
android:layout_height="wrap_content" />
<Button
android:text="+/-"
android:layout_width="0dp"
android:layout_weight=".16667"
android:textAllCaps="false"
android:id="#+id/button50"
android:layout_height="wrap_content" />
<Button
android:text="%"
android:layout_width="0dp"
android:layout_weight=".16667"
android:textAllCaps="false"
android:id="#+id/button51"
android:layout_height="wrap_content" />
<Button
android:text="^"
android:layout_width="0dp"
android:layout_weight=".16667"
android:textAllCaps="false"
android:id="#+id/button52"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/linearLayout6"
android:orientation="horizontal"
android:weightSum="1"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
>
<Button
android:text="0"
android:layout_width="0dp"
android:layout_weight=".16667"
android:textAllCaps="false"
android:id="#+id/button25"
android:layout_height="wrap_content" />
<Button
android:text="."
android:layout_width="0dp"
android:layout_weight="0.3333"
android:textAllCaps="false"
android:id="#+id/button53"
android:layout_height="wrap_content" />
<Button
android:text="="
android:layout_width="0dp"
android:textAllCaps="false"
android:layout_weight="0.5"
android:id="#+id/button55"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/linearLayout2"
android:orientation="horizontal"
android:weightSum="1"
android:layout_above="#+id/linearLayout3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
</LinearLayout>
<Button
android:text="Mode"
android:layout_width="0dp"
android:layout_weight="0.50"
android:textAllCaps="false"
android:id="#+id/button3"
android:background="#ffff5830"
android:layout_height="wrap_content"
android:layout_above="#+id/linearLayout3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
Try to use Gridlayout instead of LinearLayout if you want to develop a calculator.
Your Layout2 is using wrap_content for height
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/linearLayout2"
android:orientation="horizontal"
android:weightSum="1"
android:layout_above="#+id/linearLayout3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
</LinearLayout>
but there is no content in that group (no child views) so this is correct behaviour as there is no need to make group height bigger.
Also, if you want to stack Layout2 on Layout3 you may consider wrapping themtogether in FrameLayout (or another RelativeLayout)

Another linear layout with a button not appearing

i made another linear layout beside the first one by your help, but now after trying to add a new button in the left the button is not appearing even though i made wrap_content as i learned but still it's not appearing while the buttons in the first linearlayout are working perfectly i hope you can help and thanks
this is the main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="right"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="اضغط على من تريد معرفة المزيد عنه"
android:padding="10dp"
/>
<Button
android:id="#+id/buttontype1"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="Start" />
<Button
android:id="#+id/button1"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="Start" />
<Button
android:id="#+id/button2"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="Stop"/>
<Button
android:id="#+id/button3"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="Start" />
<Button
android:id="#+id/button4"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="Stop" />
<Button
android:id="#+id/button5"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="Start" />
<Button
android:id="#+id/button6"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="Stop"/>
<Button
android:id="#+id/button7"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="Start" />
<Button
android:id="#+id/button8"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="Stop"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="vertical" >
<Button
android:id="#+id/buttonkk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:text="HII"/>
</LinearLayout>
</LinearLayout>
Put everything inside a ScrollView
Try to set all your button with value android:layout_weight="1"
It allows you to achieve proportional distribution of elements within a LinearLayout.
Read that : http://ugiagonzalez.com/2012/01/19/android-linearlayout-distribution-explained-weight-and-sizes/

How can i make a layout like as give below in the picture?

I am actually trying to create a layout like this but it seems to scatter all over the place.
Any help or idea to optimize it will be greatly appreciated.
xml
<RelativeLayout
android:id="#+id/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true" >
<ImageView
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/menu_normal"
android:onClick="onClickContentButton" />
<ImageView
android:id="#+id/new_chat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:onClick="actionPerformed"
android:src="#drawable/invite_friends_normal" />
<TextView
android:id="#+id/headerText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Profile"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/header"
android:background="#049080"
android:gravity="center_horizontal"
android:padding="10dp"
android:text="My profile"
android:textColor="#FFFFFF"
android:textSize="15sp" />
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/textView1"
android:orientation="vertical"
android:weightSum="2" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#android:color/darker_gray"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="20dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/com_facebook_profile_picture_blank_square" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="10dp"
android:layout_alignTop="#+id/imageView1"
android:src="#drawable/bang_selected" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imageView1"
android:layout_toRightOf="#+id/imageView1"
android:text="John smith"
android:textColor="#2D2D2D"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView2"
android:layout_below="#+id/textView2"
android:text="latitude:"
android:textColor="#2d2d2d"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/textView2"
android:text="longitude:"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
Graphical layout
I would make the main layout be a relative layout.
The "My Profile" button would be the first object in the layout
The segment underneath that can be another relative layout in which you would specify the association of all the elements.
And the bottom map portion would be the map view
Within the second portion, use a mix of vertical and horizontal linear layouts to get the look you desire

Categories

Resources