1

I need help for a Layout in Android Studio.

I got Texts and Icons (5 each) and every text is associated with an icon.

Icon and text are vertically centered.

I need to put every texts and icon in a shape of a « + ». So one in center, top, bottom, left and right.

How can I group one text with its icon so I can interprete this as one element and got more easier to center for this layout ?

Thanks !

You will find the result I have to match :

enter image description here

Saku
  • 305
  • 1
  • 2
  • 8

2 Answers2

2

Just put them both in a layout view. like in a linearLayout with vertical orientation and set gravity to centerhorizontal. if you want to make it clickable just add an id to the linearlayout.

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:id="@+id/image_text_element">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/settings"
        android:layout_gravity="center_horizontal">
    </ImageView>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="test"
        android:layout_gravity="center_horizontal" />
</LinearLayout>
quealegriamasalegre
  • 2,887
  • 1
  • 13
  • 35
-1

How can I group one text with its icon so I can interprete this as one element and got more easier to center for this layout ?

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/your_text"
    android:background="?android:attr/selectableItemBackground"
    android:drawableTop="@drawable/your_icon"/>
aiqency
  • 1,015
  • 1
  • 8
  • 22
  • Why? It look like on the picture you provide – aiqency Mar 20 '20 at 02:54
  • Adding drawable to a button is very limited and I can change size of the drawable – Saku Mar 20 '20 at 03:01
  • You can set the padding through drawablePadding and the size via resources folder. Otherwise you can create a custom view from an xml. https://stackoverflow.com/a/17413018/2754562 and use it in your layout. – aiqency Mar 20 '20 at 03:21
  • Drawable top gives you such limited control over scaling and placement. You're better off not doing that and creating a linear layout as described in the other answer. You'll find a few cases where this works, but by and large its a bad idea. – Gabe Sechan Mar 20 '20 at 05:03
  • @GabeSechan The OP ask "How can I group one text with its icon" and provide picture example. By using this code you get exactly the same result as the picture description. Plus I comment that he can write a custom view to get a better control. What's the point of down voting what is correct? – aiqency Mar 20 '20 at 12:20