I’ve created a custom spinner using my own ProjectSpinnerAdapter. I’ve also got another class which just inflates the layout but hasn’t been implemented yet so is still using the standard spinner.
I can’t seem to get my custom spinner to align the same way as the standard spinner.
I’m also having problems when the length of the item is wider than the table cell as you can see below. Is there not a way to truncate the options if they are over a certain length ? – This doesn’t seem like its the correct way to fix this but I can’t directly set the width of the widget as this is controlled via the table layout.*
**Fixed, See update*
Screenshots


The standard spinner aligns perfectly with the Button it sits next to (they are both contained within the same TableRow)
layout
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="1" >
<TableRow>
<Button
android:id="@+id/selectButton"
android:text="@string/show" />
<Spinner android:id="@+id/projectSpinner" />
</TableRow>
spinner_list_item.xml (Uses maxLength)
<?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="wrap_content"
>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/title_text_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:maxLength="15"
android:gravity="center"/>
</LinearLayout>
Update
The issue was the layout of the spinner item, in my custom layout spinner_list_item.xml I’d specified a layout all I needed was the textview
spinner_list_item.xml (Uses maxLength)
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/title_text_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:maxLength="15" />
If I may ask, why did you choose the TableLayout? I think the same could be achieved with a
RelativeLayoutandlayout_toRightOflayout_below, etc. Or aLinearLayoutwith aweightSumandgravityeach child having alayout_weight.I’m not going to give you a solution that changes your entire layout though ;), but I tested this and this seems to work for me.
Hopefully this works and it’s a better way than setting a fixed string length
edit
I’m not sure why you’re populating the spinner the way you are, but you can try:
android:gravityis wrong too, needs to beandroid:layout_gravity(but that’s LinearLayout onlyLast edit 😀
try using the default layouts
then