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>
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" />
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"
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.
<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>
I think this not big issue. Your is right nothing wrong with you posted. In my eclipse it works fine.
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.
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.
if you restart eclipse, then it not work, may be your project library not properly located.
Change extend Activity to extend AppCompatActivity in your Activity Class
You need to define the Button as ImageButton in java classes.
Related
I have a button that I've implemented in one of my activities in my Android project - the button is supposed to contain both a logo (a drawable) and some text following it, but I have a very peculiar problem. In the preview-view of my activity xml-file, the button looks perfectly fine but when I run my app on my phone it gets absurdly large.
I import the drawable from an .xml drawable that I've myself have made. I tried imported a .png straight into my activity but it was too large - so I make a new xml where I resize it and that xml is what I import as my drawable. Code:
Activity.xml
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/title_activity_facebookInvite"
android:id="#+id/btnInviteFacebook"
android:layout_alignParentBottom="false"
android:layout_centerHorizontal="true"
android:layout_below="#+id/mainActivityInviteText"
android:background="#drawable/bh_red_button"
android:layout_marginTop="#dimen/bh_input_element_margin"
android:textSize="#dimen/bh_button_font_size"
android:drawableLeft="#drawable/fb_resize"
android:layout_marginLeft="45dp"
android:layout_marginRight="45dp"
android:minHeight="0dp" />
fb_resize.xml (My drawable)
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/fb_resizeBtn"
android:drawable="#mipmap/fb_logo"
android:width="15dp"
android:height="15dp"
/>
</layer-list>
fb_logo is my PNG that I've made. Another very strange thing is that when I emulate it on a virtual device in Android Studio it looks perfectly fine, but not on my phone (Have tried a different phone aswell).
Looks like this is your problem :
android:layout_width="fill_parent"
Try replacing fill_parent with wrap_content
You could also try to add this in your item :
android:scaleType="fitCenter"
android:adjustViewBounds="true"
Okay here is a workaround, try replacing your button with this:
<FrameLayout
android:id="#+id/NavigateRightButtonLayout"
android:layout_below="#+id/mainActivityInviteText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="45dp"
android:layout_marginRight="45dp"
android:gravity="center">
<Button
android:textSize="#dimen/bh_button_font_size"
android:textColor="#FFFFFF"
android:textAlignment="gravity"
android:text="#string/title_activity_facebookInvite"
android:gravity="center"
android:background="#drawable/bh_red_button"
android:layout_marginTop="#dimen/bh_input_element_margin"
android:textSize="#dimen/bh_button_font_size"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<ImageView
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_gravity="left"
android:id="#+id/fb_resizeBtn"
android:background="#mipmap/fb_logo" />
</FrameLayout>
Solved it! Instead of a .png I used a vector-picture (.svg file) and that resized just fine for some reason.
I am new in Android development.I created a simple project with a simple button.I'd like to set image as background (see image). I've used android:src="#drawable/id_image" and android:background="#drawable/id_image2" but the image does not appear on the entire surface of button.Any help would be appreciated
Android, you can use android.widget.ImageButton to display a normal “Button“, with a customized background image.
<ImageButton
android:id="#+id/top_bar_btn_right_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:src="#drawable/ic_filter" />
You might use android:src and android:background for adding two images.
when you add your button , you just go to the menu of properties and choose background / drawable and choose your image
You can try something like this
<RelativeLayout
android:layout_width="250dp"
android:layout_height="400dp"
android:background="your_background_goes_here"
android:foreground="?android:attr/selectableItemBackground"
android:clickable="true"
android:centerInParent="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="your_icon_above_background"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text_for_your_image"/>
</RelativeLayout>
I am not able to add image. I downloaded the image and added it to res directory under drawable-mdpi. Then when I try to add the image on the view using imageView, it gives me this error
Couldn't resolve resource #drawable/ball01
Failed to convert #drawable/ball01 into a drawable
Here is my xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
tools:ignore="MergeRootFrame" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView"
android:layout_gravity="center"
android:textSize="32sp"
android:layout_above="#+id/button"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Enlighten Me"
android:id="#+id/button"
android:layout_centerVertical="true"
android:layout_alignParentRight="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:layout_above="#+id/textView"
android:layout_centerHorizontal="true"
android:src="#drawable/ball01" />
</RelativeLayout>
What am I doing wrong here.
The directory name should be 'drawable-mdpi'. Using a hyphen instead of an underscore.
image name must Digital or lowercase 。 ide only identification.
I had this same problem, I had this image which I named Barcode and then saved it into my drawable folder, then I decided it would be better to have it named barcode with a small b, so I renamed it.
Then I checked if it would work, and I got the same errors as you, after changing the name in my xml to 'Barcode' with a big B, it worked.
solution for me: delete barcode image from drawable and add it again with the name I'm going to use for it, don't change the name while it is in drawable folder.
I had this problem on android studio.
if you close and open project and it continues, all you need to do is RIGHT CLICK on the particular image you put in your "res" folder (that's giving the problem) and refactor-RENAME. Remove spaces in the name of the file. that should do it.
I have a layout which consists of an ImageView and a TextView. I recently changed android:src for ImageView to a new drawable, but when I am building and running the app on emulator or device its showing the old image instead of the new image that I have specified in XML Layout. I don't understand why the references are not getting updated and android is using the old reference of the image.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/list_selector"
android:orientation="horizontal"
android:padding="3dip" >
<ImageView
android:id="#+id/playImage"
android:layout_width="35dip"
android:layout_height="35dip"
android:src="#drawable/play_icon" />
<TextView
android:id="#+id/playlistName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:textColor="#color/black"
android:maxLines="1"
android:layout_toRightOf="#+id/playImage"
android:paddingLeft="5dp"
android:textStyle="bold"
android:text="Unamed" />
</RelativeLayout>
Please help me.
In such cases, I refresh project folder, clean the project. If these don't work then I will just close and open project. Eclipse acts weird many a times.
I did a silly mistake.I was also Setting the ImageView image in my custom adapter and that was overriding the XML ImageView default image.I removed the code of setting image src from adapter and this fixed the issue
I have the next problem: I use a lot the RelativeLayout, like the Android documentation is telling me to do. But, since few days, I don´t understand why, this is not working anymore:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#drawable/background"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="#+id/boton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Button" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/boton"
android:layout_centerHorizontal="true"
android:layout_marginTop="60dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000" />
</RelativeLayout>
This is my layout, very simple. So, according to this one, the textView should be BELOW the button, but, instead, it appears OVER the button!
.
Сan anyone explain me what happens?
EDIT: The problem is not when I try to run it on a device, the problem comes when I see it on the graphical layout of the ADT Plugin for Eclipse...
I try this code it's working properly.
If your side not working properly Try to Clean build your project and then Run.
If still having problem then Try this
android:layout_below="#+id/boton"
remove above line with this
android:layout_below="#id/boton"
Because "+" create new reference to R.java file
This may be the issue.
try by removing the below line from your code
android:layout_marginTop="60dp"
Take the '+' sign out of the TextView's layout_below attribute. The '+' tells it to deine a new id. Without the '+' it should refer to the existing one.
I think your issue is the drawable for the background, since I am not seeing an image in your preview.
Having this issue is giving you a build error so the preview doesn't show the layout in its current state (it reverts to the last sucessful build). Either remove that line and save to see the preview or check to make sure that you have an image in your drawables folder titled "background"
Try this, without the drawable to check if it is working for you:
<?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" >
<Button
android:id="#+id/boton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Button" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/boton"
android:layout_centerHorizontal="true"
android:layout_marginTop="60dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000" />
</RelativeLayout>
I am trying to run it, and now, I know what´s the problem.
When running the app in a terminal, it is working properly, the problems comes when I try to see it on the graphical layout on the ADT plugin on Eclipse! Anyone with the same problem??
Add </RelativeLayout> at the end of your relative layout to close it.