user3197109
user3197109

Reputation:

Android Image on Image button not displayed

I facing the problem that, I cant add new image button to my XML file.I added my icons to Drawable, but It cannot show in image button. XML shows an error ImageButton class not found. How can I solve this ?

I got the following error in xml

The following classes could not be found: - ImageButton (Change to android.widget.ImageButton, Fix Build Path, Edit XML)

Here is my xml

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

       <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="109dp"
        android:src="@drawable/facebook" />

     </RelativeLayout>

Upvotes: 11

Views: 23291

Answers (7)

Max Devtec
Max Devtec

Reputation: 21

You need to define the Button as ImageButton in java classes.

Upvotes: 0

Ola
Ola

Reputation: 1

Change extend Activity to extend AppCompatActivity in your Activity Class

Upvotes: 0

Mr. Disability
Mr. Disability

Reputation: 838

I was using the interface builder and for some reason, it added this

tools:src="@drawable/ic_play_arrow_white_100dp"

Change it to this to make it work and display image in image button

android:src="@drawable/ic_play_arrow_white_100dp"

Upvotes: 4

user3708141
user3708141

Reputation: 11

I had the same problem. My images were .ico and I had the same error message in the Graphical Layout tab in eclipse. I proved to transform the .ico images to .png images and then I deleted the .ico and I imported the .png to the project. It was succesful.

Upvotes: 1

user1391869
user1391869

Reputation:

I think this not big issue. Your is right nothing wrong with you posted. In my eclipse it works fine.

  1. First check images properly put in drawable folder with small letter naming convention. or if you only have it in /res/drawable-xhdpi/ please check this also. May use this background instead of src.
  2. if you not not getting R cannot resolve with putting image in xml, then first Refresh project, if is not work then clean whole project, if this is also not work restart eclipse.

  3. if you restart eclipse, then it not work, may be your project library not properly located.

Upvotes: 0

lantonis
lantonis

Reputation: 143

Why dont you try a simple button instead of image button. Here is a code i have on some of my buttons with image. android:drawableTop="@drawable/three" : this is basically the image android:background="@drawable/buttonstyles" : this is a style for the button. No need to use it. Or the other layout features i have.

 <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/button2"
        android:layout_alignParentRight="true"
        android:layout_marginRight="20dp"
        android:background="@drawable/buttonstyles"
        android:drawableTop="@drawable/three"
        android:onClick="goToSettings"
        android:text="Settings" />

Upvotes: 4

Dakshesh Khatri
Dakshesh Khatri

Reputation: 629

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical" >

   <ImageButton
    android:id="@+id/imageButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="109dp"
    android:src="@drawable/a" />

   <ImageButton
       android:id="@+id/imageButton3"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignParentBottom="true"
       android:layout_centerHorizontal="true"
       android:layout_marginBottom="164dp"
       android:src="@drawable/a" />

 </RelativeLayout>

Upvotes: 0

Related Questions