Set the text in ImageButton

旧巷老猫 提交于 2019-11-30 20:13:18

ImageButton cannot contain text:

Options you can try

1) use Button instead of ImageButton

2) use ImageButton and TextView below to show text

Hope this could help

You cannot set text to ImageButton because it has no method as setText() or android:text property.

ImageButtons can't have text (or, at least, android:text isn't listed in its attributes). It looks like you need to use Button (and look at drawableTop or setCompoundDrawablesWithIntrinsicBounds(int,int,int,int)).

Instead of ImageButton try with Button. You can able to add the button background image.So try with Button instead of ImageButton.

you use of drawableRight or drawableLeft attribute and use text attribute for create Button with text and image.

<Button 
 android:id="@+id/buttonok" 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content"
 android:drawableLeft="@drawable/buttonok"
 android:text="OK"/>

Try this instead of Button

<FrameLayout
    android:layout_width="40dp"
    android:layout_height="100dp"
    >
    <Button
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/prevButton"
        />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:paddingTop="16dp"
            android:paddingBottom="16dp"
            android:paddingLeft="8dp"
            android:paddingRight="8dp"
            android:src="@drawable/arrow_left"
            android:scaleType="fitCenter"
            android:adjustViewBounds="true"
            android:background="#00000000"
            android:enabled="false"
            android:clickable="false"
            />
        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:text="prev"
            android:textSize="20sp"
            android:gravity="center"
            android:background="#00000000"
            android:textColor="#FF000000"
            android:enabled="false"
            android:clickable="false"
            android:layout_weight="1"
            />

    </LinearLayout>

</FrameLayout>
Kashif Ks

Try this:

<Button
    android:id="@+id/Button"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:layout_gravity="center"
    android:layout_marginBottom="6px"
    android:layout_marginRight="3px"
    android:layout_toRightOf="@+id/Logo"
    android:scaleType="fitStart"
    android:background="@drawable/ImageYouWantToShow" />
Button imgButton= (Button) this.findViewById(R.id.Button)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!