Image from ImageButton is not getting displayed on emulator from Android Studio

1.2k views Asked by At

In my graphical layout in android studio I'm seeing the images on my imagebutton but when I'm running my app on an emulator the images on the imagebutton do not get displayed.I'm attaching the screenshots of what I'm seeing in my android studio and what I get when I run my app on emulator. this is what i see on android studio

this is what i get on emulator

I'm also attaching my xml layout file

<?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"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_calc"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.sameer.coolcalc.CalcActivity"
tools:background="@android:color/background_light">

<LinearLayout
    android:orientation="vertical"
    android:layout_height="match_parent"
    android:weightSum="6"
    android:layout_width="wrap_content">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:background="@android:color/holo_blue_bright"
        android:weightSum="1">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:inputType="numberDecimal"
            android:ems="10"
            android:id="@+id/editText4"
            android:text="567"
            android:textSize="60sp"
            android:gravity="center_vertical|end" />
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:weightSum="4">

        <Button
            android:text="7"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/button14"
            android:layout_weight="1"
            android:background="@android:color/background_light"
            android:textColor="@android:color/black"
            android:textSize="36sp" />

        <Button
            android:text="8"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/button13"
            android:layout_weight="1"
            android:textSize="36sp"
            android:textColor="@android:color/black"
            android:background="@android:color/background_light" />

        <Button
            android:text="9"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/button12"
            android:layout_weight="1"
            android:background="@android:color/background_light"
            android:textColor="@android:color/black"
            android:textSize="36sp" />

        <ImageButton
            android:layout_height="match_parent"
            app:srcCompat="@drawable/divide"
            android:id="@+id/imageButton6"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:background="@android:color/background_light" />
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1">

        <Button
            android:text="4"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/button3"
            android:layout_weight="1"
            android:background="@android:color/background_light"
            android:textColor="@android:color/black"
            android:textSize="36sp" />

        <Button
            android:text="5"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/button2"
            android:layout_weight="1"
            android:background="@android:color/background_light"
            android:textColor="@android:color/black"
            android:textSize="36sp" />

        <Button
            android:text="6"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/button"
            android:layout_weight="1"
            android:background="@android:color/background_light"
            android:textColor="@android:color/black"
            android:textSize="36sp" />

        <ImageButton
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:srcCompat="@drawable/multiply"
            android:id="@+id/imageButton"
            android:layout_weight="1"
            android:background="@android:color/background_light" />

    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:weightSum="4">

        <Button
            android:text="1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/button6"
            android:layout_weight="1"
            android:background="@android:color/background_light"
            android:textColor="@android:color/black"
            android:textSize="36sp" />

        <Button
            android:text="2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/button5"
            android:layout_weight="1"
            android:background="@android:color/background_light"
            android:textColor="@android:color/black"
            android:textSize="36sp" />

        <Button
            android:text="3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/button4"
            android:layout_weight="1"
            android:background="@android:color/background_light"
            android:textColor="@android:color/black"
            android:textSize="36sp" />

        <ImageButton
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:srcCompat="@drawable/subtract"
            android:id="@+id/imageButton3"
            android:layout_weight="1"
            android:background="@android:color/background_light" />
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1">

        <LinearLayout
            android:orientation="horizontal"
            android:layout_height="match_parent"
            android:background="@android:color/background_light"
            android:layout_width="205dp"
            android:gravity="center">

            <Button
                android:text="clear"
                android:id="@+id/button15"
                android:background="@android:color/holo_blue_bright"
                android:layout_width="170dp"
                android:layout_height="50dp"
                android:textSize="30sp" />
        </LinearLayout>

        <Button
            android:text="0"
            android:layout_height="match_parent"
            android:id="@+id/button11"
            android:layout_width="103dp"
            android:background="@android:color/background_light"
            android:textColor="@android:color/black"
            android:textSize="36sp" />

        <ImageButton
            android:layout_width="103dp"
            android:layout_height="match_parent"
            app:srcCompat="@drawable/add"
            android:id="@+id/imageButton5"
            android:background="@android:color/background_light" />
    </LinearLayout>

</LinearLayout>

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:srcCompat="@drawable/equal"
    android:id="@+id/imageButton7"
    android:background="@android:color/transparent"
    android:layout_alignParentEnd="true"
    android:layout_marginTop="140dp" />
</RelativeLayout>
1

There are 1 answers

0
AndroidRuntimeException On

Use android:src instead of app:srcCompat.This will solve your problem for now.

app:srcCompat comes from Android Support Library, also the support Vector Drawables and Animated Vector Drawables.

If you want to use app:srCompat you should configure your gradle.

android {  
   defaultConfig {  
     vectorDrawables.useSupportLibrary = true  
    }  
 }

When using AppCompat with ImageView (or subclasses such as ImageButton and FloatingActionButton), you’ll be able to use the new app:srcCompat attribute to reference vector drawables (as well as any other drawable available to android:src)

More info here: https://android-developers.googleblog.com/2016/02/android-support-library-232.html