I have a ImageButton which looks like this.
<ImageButton
android:id="@+id/ImageButton"
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:background="@drawable/button_bg_purple"
android:scaleType="fitStart"
android:src="@drawable/ImageButton" />
ImageButton ImgButton= (ImageButton) this.findViewById(R.id.ImageButton);
Now I need to add a dynamic text in ImageButton programmatically and I cannot use button. How can I do it?
Thanks In Advance ...
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>
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)
来源:https://stackoverflow.com/questions/21776676/set-the-text-in-imagebutton