Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ImageButton does not display image, even though it is visible in the Designer preview

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>

like image 634
Glave Avatar asked Nov 05 '16 03:11

Glave


People also ask

What is ImageButton explain with example?

In simple words we can say, ImageButton is a button with an image that can be pressed or clicked by the users. By default it looks like a normal button with the standard button background that changes the color during different button states.

Which attribute in XML code can be used to place an image on the surface of a button control?

By default, an ImageButton looks like a regular Button , with the standard button background that changes color during different button states. The image on the surface of the button is defined either by the android:src attribute in the <ImageButton> XML element or by the ImageView. setImageResource(int) method.


1 Answers

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

like image 130
Kunal Parikh Avatar answered Sep 25 '22 03:09

Kunal Parikh