Android Left,Center and Right Alignment
Solution 1
EDIT: I believe this will solve your problem. See the below code. Let me know if this helps! I know this has helped me and i will be using it. Accept this as the answer if it worked! =)
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1">
<TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button1"
android:id="@+id/button1button"></Button>
<EditText
android:layout_height="wrap_content"
android:id="@+id/firstedittext"
android:layout_width="wrap_content"></EditText>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button2"
android:id="@+id/button2button"></Button>
</TableRow>
</TableLayout>
EDIT: Added relative layout to see if it works for you, let me know if it works. You will need to adjust the "10px" to get the desired distance. I am seeing if there is a better way to do this, where the distance is more dynamic.
You can make the buttons "alignParentLeft" and "alignParentRight" to get them to align with the right and left side of the screen. However, i am still having trouble getting the text between the two views.
You could do this in your xml file for the layout. Make it a horozontal layout and add the first button first, then the text view, then the second button. Will post xml shortly for an example.
<RelativeLayout
android:id="@+id/LinearLayout01"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button1"
android:layout_alignParentLeft="true"
android:layout_marginRight="10px"
android:id="@+id/button1button"></Button>
<EditText
android:layout_height="wrap_content"
android:id="@+id/firstedittext"
android:layout_toRightOf="@id/button1button"
android:layout_marginRight="10px"
android:layout_width="wrap_content"></EditText>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/firstedittext"
android:text="button2"
android:id="@+id/button2button"></Button>
</RelativeLayout>
Solution 2
this will do the trick. What you need is absolute gravity. This one should work with all Android screen sizes too. I don't think you will need to use any margins or paddings, they will mess you up once you have contents in your children (margins are relative in terms of choosing them, aka: you want a smaller margin when you have less text...which happens often during translation, new functions, replacements, etc..)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button1"
android:layout_alignParentLeft="true"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/firstedittext"
android:layout_centerInParent="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button2"
android:layout_alignParentRight="true"/>
</RelativeLayout>
Related videos on Youtube
budsiya
Updated on July 09, 2022Comments
-
budsiya almost 2 years
I am pretty new to android and trying to do a sample app where 3 elements (2 buttons and 1 text view ) are placed horizontally. I need the text to be in the center with two buttons aligned left and right of the view.
E.g.
|<Button> <Text> <Button>|
Appreciate if you could help me with this.
Thanks in advance
-
budsiya about 13 yearsThanks for the reply. I tried it that way. But they do no get aligned properly. (The two buttons need to be aligned to left and rights sides of the screen.) And I have set the width to fill-parent.
-
prolink007 about 13 yearsI see now, i modified your post so the picture will show up much better. It is awaiting review. When it gets reviewed you should see it showing up in your post now. I have never had luck with actually aligning the elements of the view like you are saying. However, when i get the chance i will look into this and see if i can get it working, because i am also looking for a simple answer to that question.
-
budsiya about 13 yearsThanks Edison... I will try it out tonight and let you know the result. Appreciate your help. :)
-
budsiya about 13 yearsThank you so much for your support. Really appreciate that. I will try both the methods once I get home tonight. Will let you know the result. Tx!
-
prolink007 about 13 years@user258587: Cool, hope it works. I tested it here at work and it worked perfectly for me. However, i did not test to see what would happen if you turned the phone over. GL
-
prolink007 about 13 yearsI was looking everywhere for that "layout_centerInParent" and was unable to find it. I was assuming it would follow the same naming as the others "layout_alignParentLeft" and "layout_alignParentRight". So, i was looking for "layout_alignParentCenter", lol.
-
Edison about 13 years@prolink007 Are you using the latest SDK? I'm not sure what SDK did layout_x given, but if you just type android: and wait and then type "l", Eclipse should filter that out for you...I usually just type "l" instead of trying to remember the whole thing because...my memory sucks T_T.
-
prolink007 about 13 yearsthat is what i did, but i typed "layout_align" looking for something like "layout_alignParentCenter", lol. I was thinking it would be the same as right and left.
-
Edison about 13 years@prolink007 haha, ic. Yea, that happens very often to me too.
-
Manmohan Soni almost 6 yearsFor high performance check the below link and make the width of the text view wrap content to achieve the above requirement : stackoverflow.com/questions/11222916/…