Android ActionBar setActionView layout issue
Asked Answered
B

2

5

I've been trying to use the setActionView from the ActionBar in ICS

Seems like it should be straight forward but somehow I'm not getting the layout alignment that I would hope for. As you can see in the image below the 'target' icon is centered correctly within it's layout. But when I setActionBar(progress) the progress view is always aligned to the right whatever I try.

before clicking the menu item after clicking the menu item

Here are the 2 states, before and after clicking the menu item. As you can see the progress view is always aligned to the right. I've tried changing the gravity options in the my progress layout xml from left to right to center and whatever I do change it doesn't seem to change anything.

I haven't found any info regarding this problem so I'm thinking I must be doing something wrong.

Do anyone have a clue? Thanks for the help!

Here's my action bar menu layout 'action_bar_menu.xml'

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/locate"
          android:title="locate"
          android:icon="@drawable/locate"
          android:showAsAction="ifRoom|withText" />
</menu>

Here's my progressbar layout 'inderterminate_progress.xml'

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:gravity="center">

    <ProgressBar android:layout_width="25dp"
                 android:layout_height="25dp"
                 android:layout_gravity="center"
                 android:indeterminate="true"
                 style="?android:attr/progressBarStyleInverse"/>
</FrameLayout>

And finally here's my testx Activity

public class HelloAndroidActivity extends Activity {

    /**
     * Called when the activity is first created.
     * @param savedInstanceState If the activity is being re-initialized after
     * previously being shut down then this Bundle contains the data it most
     * recently supplied in onSaveInstanceState(Bundle). <b>Note: Otherwise it is null.</b>
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        getActionBar().setTitle("Test");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.action_bar_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);


        if (R.id.locate == item.getItemId()) {

            final MenuItem menuItem = item.setActionView(R.layout.inderterminate_progress);

            new Thread(new Runnable() {
                @Override
                public void run() {
                    SystemClock.sleep(3000);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            menuItem.setActionView(null);
                        }
                    });
                }
            }).start();
        }

        return true;
    }
}
Boulware answered 18/5, 2012 at 2:58 Comment(0)
V
9

It seems that explicitly defining the style and adding a 4dip padding in the root of the layout to inflate as shown below solves this issue

<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:paddingLeft="4dip"
    android:paddingRight="4dip"
    style="?android:attr/actionButtonStyle" />

This seems to be used in the android stylesheet here

Vivid answered 2/7, 2012 at 11:34 Comment(1)
Yes you're totally right :). Thx for the help it's very appreciated.Boulware
D
3

A little late but the right solution is not an absolute value such as 4dp. The right approach is to set the minWidth to ABS values:

android:minWidth="@dimen/abs__action_button_min_width"

Example progress bar:

<?xml version="1.0" encoding="utf-8"?>

<ProgressBar xmlns:android="http://schemas.android.com/apk/res/android"
             android:minWidth="@dimen/abs__action_button_min_width"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:focusable="true"
             android:layout_gravity="center"
             style="@android:style/Widget.ProgressBar.Small"/>
Dermot answered 12/5, 2013 at 20:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.