Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I show an icon next to Text in xml file?

Tags:

android

xml

I tried to code like this

    <ImageView android:id="@+id/ivIcon"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:src="@android:drawable/ic_btn_speak_now" />

    <TextView
        style="@style/behindMenuItemLabel"
        android:text="Item0" />

But it outputs like this. It's not in the same line :(

enter image description here

My whole xml code is just like this. (icon) item will be a set, which will be clicked by a user to move on activity.
So I need grouping them together.

<LinearLayout style="@style/behindMenuScrollContent"
    android:paddingTop="25dp" >

    <TextView
        style="@style/behindMenuItemTitle"
        android:text="Subject" />

    <TextView
        style="@style/behindMenuItemLabel"
        android:text="Item0" />

    <TextView
        style="@style/behindMenuItemLabel"
        android:text="Item1" />

    <TextView
        style="@style/behindMenuItemLabel"
        android:text="Item2" />

    <TextView
        style="@style/behindMenuItemLabel"
        android:text="Item3" />

    <TextView
        style="@style/behindMenuItemLabel"
        android:text="Item4" />

    <TextView
        style="@style/behindMenuItemLabel"
        android:text="Item5" />

    <TextView
        style="@style/behindMenuItemTitle"
        android:text="Subject2" />

    <TextView
        style="@style/behindMenuItemLabel"
        android:text="Item6" />

    <TextView
        style="@style/behindMenuItemLabel"
        android:text="Item7" />

    <TextView
        style="@style/behindMenuItemLabel"
        android:text="Item8" />

    <TextView
        style="@style/behindMenuItemLabel"
        android:text="Item9" />

    <TextView
        style="@style/behindMenuItemLabel"
        android:text="Item10" />


    <Button
        android:id="@+id/behind_btn"
        style="@style/behindMenuItemLabel"
        android:text="BUTTON" />

</LinearLayout>

UPDATE: I don't need margin between icon and text but need margin only on left!

enter image description here

style.xml

<style name="behindMenuItemLabel">
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_marginLeft">20dp</item>
    <item name="android:layout_marginBottom">4dp</item>
    <item name="android:textSize">30sp</item>
    <item name="android:textColor">#d2d2d2</item>
</style>
like image 203
MKK Avatar asked Dec 30 '25 17:12

MKK


1 Answers

Try something like this :

<TextView
    android:id="@+id/behindMenuItemLabel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="30dp"
    android:layout_weight="40"
    android:drawablePadding="-5dp"
    android:gravity="center_vertical"
    android:text="Speak Now"
    android:textColor="#000000"
    style="@style/behindMenuItemLabel" />

And this is your style.xml

<style name="behindMenuItemLabel">
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_marginLeft">20dp</item>
    <item name="android:layout_marginBottom">4dp</item>
    <item name="android:textSize">30sp</item>
    <item name="android:drawableLeft">@drawable/your_icon</item>
    <item name="android:textColor">#d2d2d2</item>
</style>
like image 91
Harish Godara Avatar answered Jan 01 '26 08:01

Harish Godara



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!