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
Left-align textPress Ctrl+L.
To right align text in TextView in Kotlin Android, set android:textAlignment attribute with the value “viewEnd” in layout file, or programmatically set the textAlignment property of the TextView object with View.
LinearLayout is a view group that aligns all children in a single direction, vertically or horizontally. You can specify the layout direction with the android:orientation attribute. Note: For better performance and tooling support, you should instead build your layout with ConstraintLayout.
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>
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With