views:

313

answers:

3

Hi,

i have an EditText and a Button in my LinearLayout and i want to align them closely together so they see seem to belong together (edittext + micButton for speech input). Now they don't have the same height and they aren't really aligned well (button seems to be a little lower than the EditText). I know I can apply a negative margin like -5dip to make them come closer together, but is there perhaps a better way to do this? Set them in a specific container/layout so that they will automatically have the same height and no margin between them?

A: 

Place them in a TableLayout, like this...

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TableLayout android:id="@+id/remoteDeviceLayout"
        android:layout_width="fill_parent" android:layout_height="fill_parent"
        android:stretchColumns="1">
        <TableRow>
            <TextView android:id="@+id/buttonName" android:layout_width="wrap_content"
                android:text="Unit Name" />
            <EditText android:id="@+id/textName" android:layout_width="fill_parent"
                android:singleLine="true" android:gravity="right"
                android:cursorVisible="true" android:editable="true" 
                android:inputType="textCapSentences" />
        </TableRow>
...
JackN
I've placed them in table row, but they are still not at the same level and grouped together. It looks like in the LinearLayout
Daniel
Edit your original question by posting your code
JackN
A: 

I am having the same issue with the ToggleButtons.. I am adding them dynamically and not using any xml, yet the ToggleButton is about 5 px lower than my EditText fields..

My code is of long.. so I won't post it.. but it dynamically grows rows in a TableLayout under a ScrollView Layout.. of the following format.

ToggleButton | EditText | EditText | EditText

these controls are added to the tableRow which is then added to the tablelayout, which adds to the ScrollView..

is there a way to fix this so they are level?

TxAg
A: 

Android tries to automatically level everything off of the text and not the buttons themselves.

Took me forever to finally figure it out. Its really simple. Should fix it.

myButton.setGravity(Gravity.CENTER_VERTICAL);

or if they are in a row.. attach the buttons to a table row, then.

myTableRow.setGravity(Gravity.CENTER_VERTICAL);
TxAg