0

I've got a great image of a right arrow and a button to put it in, but I definitely don't want it flush to a side. Is there any way to put it in the center using only the XML?

Here's the relevant code:

<style name="widget.numberPad.actionButton.done.small" parent="widget.numberPad.actionButton.done">
    <item name="android:layout_width">90dp</item>
    <item name="android:drawableStart">@drawable/ic_arrow_forward_white_48px</item>
    <item name="android:gravity">center_horizontal|center_vertical</item>
    <item name="android:layout_alignParentBottom">true</item>
    <item name="android:layout_centerHorizontal">true</item>
</style>

The button should be exactly 90dp × 90dp, but I don't know how the 48px × 48px image translates into dp.

I've read How to center icon and text in a android button with width set to “fill parent”, but the solution is both in java and deals with a width issue that I do not.

Community
  • 1
  • 1
Ky -
  • 30,724
  • 51
  • 192
  • 308

1 Answers1

1

I'm not sure I understand your question, but to center an icon on a button, this is my solution.

Other possibility to keep Button theme.

    <Button
        android:id="@+id/pf_bt_edit"
        android:layout_height="@dimen/standard_height"
        android:layout_width="match_parent"
        />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_alignBottom="@id/pf_bt_edit"
        android:layout_alignLeft="@id/pf_bt_edit"
        android:layout_alignRight="@id/pf_bt_edit"
        android:layout_alignTop="@id/pf_bt_edit"
        android:layout_margin="@dimen/margin_10"
        android:clickable="false"
        android:elevation="20dp"
        android:gravity="center"
        android:orientation="horizontal"
        >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:adjustViewBounds="true"
            android:clickable="false"
            android:src="@drawable/ic_edit_white_48dp"/>

        <TextView
            android:id="@+id/pf_tv_edit"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="@dimen/margin_5"
            android:clickable="false"
            android:gravity="center"
            android:text="@string/pf_bt_edit"/>

    </LinearLayout>

With this solution if your add <item name="colorButtonNormal">@color/your_color</item> and <item name="colorControlHighlight">@color/your_highlight_color</item> in your activity theme, you can have the Material theme on Lollipop with the shadow and ripple, and for previous versions, flat button with your color and highlight color when you press it.

Additionally, here is this autoresizing solution as a picture:

Three buttons, each with the text "Edit" and a pencil icon, centered vertically.

  1. Lollipop device
  2. On pre-lollipop device
  3. Pre-lollipop device press button
Ky -
  • 30,724
  • 51
  • 192
  • 308
Anthone
  • 2,156
  • 21
  • 26
  • As I say in the title of the question, this is for Android 4.2.2 Jelly Bean. Please, can you provide an answer you know will work on 4.2.2 Jelly Bean? – Ky - Aug 24 '15 at 13:48