ActionBar DropDown padding causes blank space [landscape mode]

678 views Asked by At

Currently I have the following style.xml

UPDATED - All relevant styles included

<style name="AppTheme" parent="Theme.AppCompat.Light">
    <item name="actionBarStyle">@style/MyActionBarStyle</item>
    <item name="actionDropDownStyle">@style/MyDropDownStyle</item>
</style>
<style name="MyActionBarStyle" parent="Widget.AppCompat.ActionBar.Solid">
    <item name="android:actionBarDivider">@null</item>
    <item name="background">@drawable/logo</item>
    <item name="displayOptions">showHome</item>
</style>
<style name="MyDropDownStyle" parent="Base.Widget.AppCompat.Spinner.DropDown.ActionBar">
    <item name="android:paddingLeft">240dp</item>
    <item name="android:dropDownHorizontalOffset">240dp</item>
    <item name="android:background">@android:color/transparent</item>
</style>

This allows me to set move the DropDown 240dp away from its X axis which works quite good. See Screenshot #1

Screenshot #1 enter image description here

But as a side effect this padding causes a blank "space" on the right side of the popup itself. See screenshot #2

Screenshot #2 enter image description here

How can I set a fixed size of the Popup or ignore the padding being set by DropDown style?

UPDATED

The reason for the paddingLeft is to not overlap with logo (defined as background)

<item name="background">@drawable/logo</item>

UPDATE 2 - Screenshot without dropDownHorizontalOffset style

enter image description here

PS:

dropDownHorizontalOffset is only used to set the X axis of the Popup matching to match the paddingLeft

1

There are 1 answers

0
Ole K On BEST ANSWER

A possible workaround is to replace the ActionBar with the new toolbar (android.support.v7.widget.Toolbar) and add a Spinner (DropDown) right inside of it

Code lines have been changed for better illustration and are not tested

main_activity.xml

<LinearLayout
    android:orientation="vertical"
    android:background="@color/navigationBar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

        <android.support.v7.widget.Toolbar
            android:background="@drawable/logo"
            android:id="@+id/toolbar"
            android:layout_height="wrap_content"
            android:layout_width="match_parent">
            <Spinner
                android:layout_marginLeft="240dp"
                android:id="@+id/spinner"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </android.support.v7.widget.Toolbar>
</LinearLayout>

MainActivity.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    //toolbar.setLogo(R.drawable.logo);

    setupSpinnerTabs();

    // for device orientation purpose
    displaySpinner(getResources().getConfiguration().orientation);
}

private void setupSpinnerTabs() {
    ToolbarSpinnerAdapter adapter = new ToolbarSpinnerAdapter(toolbar.getContext());

    spinner = (Spinner)findViewById(R.id.spinner);
    spinner.setAdapter(adapter);
    // the below is used for the selection "change event" of the spinner
    //spinner.setOnItemSelectedListener(new SpinnerSelection());
}

ToolbarSpinnerAdapter.java

public class ToolbarSpinnerAdapter extends BaseAdapter implements SpinnerAdapter {
    Context context;

    ToolbarSpinnerAdapter(Context ctx) {
        context = ctx;
    }

    @Override
    public int getCount() {
        return 1;
    }

    @Override
    public Object getItem(int position) {
        return "Spinner here"
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View defaultView = getLayoutInflater().inflate(R.layout.your_spinner_dropdown_layout, null);
        // [...]

        text.setText( this.getItem(position).toString() );
        return defaultView;
    }
}

The result should look quite simliar to the below image:

enter image description here

PS: In Portrait orientation there might not be enough space for the Spinner