8

i am trying to create an Image Button in Android Studio. The Designer preview shows the image correctly, but when I run the App there is no image in the Button. Just an empty button as small as possible.

Things I have tried:

  • Copying the image from the Android drawable folder into my own drawable folder
  • Creating a placeholder image because of a bug the prevents the first image in the folder to not show up, according to some sources on stackoverflow
  • Using different images

None of this worked for me. It is like the whole function is just broken. The interesting part is, when I set the image I want as the background of the imagebutton it works fine. But it looses its "button-appearence", so thats not a solution for me.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

<org.osmdroid.views.MapView android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent" />

<LinearLayout
    android:id="@+id/search_ui"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:padding="1dp"
    android:background="@color/white"
    android:orientation="horizontal">
    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/ic_menu_manage"
        android:id="@+id/imageButton"/>
    <EditText android:id="@+id/edit_search_string"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:hint="@string/search_string" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_send"
        android:onClick="myClickHandler"/>
</LinearLayout>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="5dp"
    android:background="@color/white"
    android:layout_below="@id/search_ui">
    <TextView
        android:id="@+id/display"
        android:textSize="16sp"
        android:textStyle="bold"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="New Text" />
</LinearLayout>

Glave
  • 137
  • 1
  • 1
  • 10

3 Answers3

23

try using only src instead of srcCompat and see if it works

Kunal Parikh
  • 463
  • 4
  • 15
  • yes, for some reason i needed to wait a few minutes. – Glave Nov 05 '16 at 04:08
  • It works for me. Would you explain what's the difference between `src` and `srcCompat`? – kuzdu Jul 01 '18 at 18:55
  • @kuzdu, Kindy go through this link https://stackoverflow.com/questions/40624554/android-what-is-the-difference-between-appsrccompat-and-androidsrc if you still have issues in understanding then let me know – Kunal Parikh Jul 15 '18 at 08:04
1

Delete your image from srcCompat

enter image description here

Set this image to src. If you can't find src then search it and enter your image path in src

enter image description here

XpressGeek
  • 2,889
  • 3
  • 23
  • 27
0

My problem was that the name of the image was very long and had a hyphen in it. I simplified the filename of the drawing and it started working. A point of confusion was that the image (before the fix) showed up in the resource viewer but not the image button. The button shrunk down to nothing as if the image was very small.