Align Custom Spinner to the Right - Actionbar compat
Asked Answered
S

1

1

I got this custom spinner I want to align to the right side of the actionbar. By default it aligns it as far to the left as possible. So I've changed my custom spinner's XML to include

android:layout_gravity="right"

but I also tried

     android:layout_alignParentRight="true"
     android:gravity="right"

And I just can't get it to work. I also tried setting the LayoutParams, but no dice..

Most of these are being suggested in other stack questions, but I haven't been able to find any other posibilities.

My java class:

private void SetUpSpinner(Menu menu) {

    actionBar = getSupportActionBar();

    actionBar.setDisplayShowHomeEnabled(false);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);

    actionBar.setNavigationMode(android.support.v7.app.ActionBar.NAVIGATION_MODE_LIST);

    categoryName = new String[]{"All","Bar", "Food"};

    MenuItem item = menu.findItem(R.id.category_spinner_item);
    item.setActionView(R.layout.action_sort);

    spinner = (Spinner) MenuItemCompat.getActionView(item);
    View spinnerView = MenuItemCompat.getActionView(item);

    SpinnerAdapter mSpinnerAdapter = (new SpinnerCustomAdapter(this, R.layout.spinnerrow, categoryName));
    spinner.setAdapter(mSpinnerAdapter);

    actionBar.setListNavigationCallbacks(mSpinnerAdapter, this);

    ActionBar.LayoutParams layoutParams = new ActionBar.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
    layoutParams.gravity = Gravity.RIGHT;
    getSupportActionBar().setCustomView(spinnerView, layoutParams);

}

menu/main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:yourapp="http://schemas.android.com/apk/res-auto" >

    <item android:id="@+id/action_share"
          android:icon="@drawable/ic_action_share"
          android:title="@string/action_share"
          yourapp:showAsAction="ifRoom"  />

    <item
            android:id="@+id/category_spinner_item"
            android:showAsAction="ifRoom"
            android:actionLayout="@layout/action_sort"  />

</menu>

layout/action_sort.xml

<?xml version="1.0" encoding="utf-8"?>
<Spinner xmlns:android="http://schemas.android.com/apk/res/android"
         android:id="@+id/category_spinner"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_gravity="right"
        />
Scylla answered 10/4, 2014 at 14:45 Comment(0)
S
2

Got it. Use

getSupportActionBar().setCustomView(spinner);

instead of

actionBar.setListNavigationCallbacks(mSpinnerAdapter, this);

and use LayoutParams to set the gravity to the right. It won't pick up on any XML gravity settings.

Scylla answered 10/4, 2014 at 15:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.