I am trying to replace my previous ugly text button with an imagebutton. However, after changing the XML file with the following ImageButton code, my application won't even start. Why?
<ImageButton
android:layout_height="fill_parent"
android:id="#+id/refresh"
android:src="#drawable/refresh"
/>
specify the layout width..
android:layout_width="wrap_content/*some value ex:50dp*/"
It turned out that I forgot the layout_width attribute. It works now.
<ImageButton
android:layout_height="fill_parent"
android:id="#+id/refresh"
android:src="#drawable/refresh"
android:layout_width="wrap_content"
/>
Related
I have RecyclerView include ImageButton play each item. But when I run ImageButton show nothing.
https://gist.github.com/tugnt/98917e0a2293d6cddd2e0e6e21d3d4fb
<Button
android:src="#drawable/ic_speaker"
android:id="#+id/play"
android:layout_margin="#dimen/pd_3dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
In your xml,see android:src property is not for button. It's imageview property. So use ImageView instead of Button or set android:background for Button
<ImageView
android:src="#drawable/ic_speaker"
android:id="#+id/play"
android:layout_margin="#dimen/pd_3dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Now try this.
Below is your code, in this you have used the android:src="#drawable/ic_speaker" for the button.
<Button
android:src="#drawable/ic_speaker"
android:id="#+id/play"
android:layout_margin="#dimen/pd_3dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
But src is for imageview, which doesn't support the button view. So use background instead of src and in the xml preview you cannot see the play button. Have a look at the below code, I have edited the code
<Button
android:background="#drawable/ic_speaker"
android:id="#+id/play"
android:layout_margin="#dimen/pd_3dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Use the xml preview to see whether you can see the view or not, which helps to sort put the bug easily and save our time. Hope this is helpful :)
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.
The image I'm setting in the app is disappearing. Here is the layout XML file:
<TextView
android:id="#id/txt_colorText2"
android:layout_width="wrap_content"
android:layout_height="30.0dip"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#id/txt_colorText1"
android:text="#string/str_textColor2"
android:textColor="#0077dd"
android:textSize="18sp" />
<ImageView
android:id="#id/img_imgColorPreview"
android:layout_width="wrap_content"
android:layout_height="30.0dip"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#id/txt_colorText2"
android:background="#drawable/ic_launcher"
android:contentDescription="#string/str_imgColor" />
And here's the code. I do this in onTouch() method:
imgView_ImagePreview.setBackgroundColor(rgbCode);
Initially when the activity is started, ic_launcher icon is shown to the right of txt_colorText2. However on touching any part of the image, there is no content shown to the right of txt_colorText2.
I've debugged quite a lot, and haven't found the root cause. Any help is greatly appreciated.
By changing background color, you are changing the background itself. in the xml instead of setting background, set src to achieve what you want.
<ImageView
android:id="#id/img_imgColorPreview"
android:layout_width="wrap_content"
android:layout_height="30.0dip"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#id/txt_colorText2"
android:src="#drawable/ic_launcher"
android:contentDescription="#string/str_imgColor" />
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.
So I'm working on making a design for my app and I need to put a imagebutton directly below an imageview. But since my imageview has a border around it with a drop shadow I need to hide (shift up) maybe 10 pixels of my imagebutton behind my imageview. Here is a quick drawing of what I want.
I hope that makes sense. I've been messing around with all kinds of different arrangements but I cant get what I want. Any help would be greatly appreciated.
First of all use a RelativeLayout. Add first the ImageButton and then the ImageView, in this way the ImageView will be on top of your ImageButton. Then you should set on your ImageButton the following :
<ImageButton
....
android:layout_below="#id/ImageViewId"
android:layout_marginTop="-10px" />
Use relative layout as said. Refer
http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html
for more positions and easy design.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/Imageview"
android:layout_marginTop="-16dp" />
<ImageView
android:id="#+id/Imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="46dp"
android:adjustViewBounds="true"
android:contentDescription="#string/description_logo"
android:src="#drawable/imagename" />
</RelativeLayout>