ImageButton show nothing in RecyclerView - android

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 :)

Related

I want to put image over button

I want to put png image over button i tried android:background but the png takes all the button what i want exactly is to use the logo png over button like this
the info logo is my png and "about" comes from the button text
u can use this:
android:drawableTop="#drawable/SomeIcon"
so then you get this:
<Button
android:id="#+id/damage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="about"
android:drawableTop="#android:drawable/ic_menu_info_details"/>
it works for me :)
You can use a FrameLayout. In a FrameLayout, the z-index is defined by the order in which the items are added, for example
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_text" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/my_drawable"
android:scaleType="fitCenter"/>
</FrameLayout>
In this instance, the ImageView would be drawn on top of the Button, along the bottom center of the Button.
You can check this thread
Basically, you can use:
<ImageButton
android:id="#+id/ImageButton01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/album_icon"
android:background="#drawable/round_button" />
As the thread says.
Take a RelativeLayout, place ImageView inside it. Now set onClickListener on RelativeLayout.
By this way you can create more complex buttons.
<RelativeLayout android:id="#+id/your_btn_id"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" />
</RelativeLayout>
you can use
android:drawableTop
to put drawable at top of Button and use appropriate padding to make it position well.
<Buttton
android:layout_width="99dp"
android:layout_height="99dp"
android:background = "#drawable/bg" />
bg is your custom image.

how change button Icons in android?

i wish to adjust icons in my application that i'm about to devolp.
this is the link of the tutorial that i'm folowing .
"https://www.youtube.com/watch?v=lkadcYQ6SuY"
this is the example given and created in the tutorial .
now i don't have the reputation yet to post pictures to show you what i want BUT
simple i want arrows for back and forward (custom created) instead of textbuttons
anyone who can help me ... ?
You can use ImageButton, put your icon into drawable folder(if it doesnt exist, create it), and set it to your imageButton
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/yourImgName" />
You can create a simple Button and set its Background for picture.
<Button
android:id="#+id/button"
android:background="#drawable/help"
android:contentDescription="#string/help"
android:gravity="bottom"
android:onClick="showHelp" />
Or you can create ImageButton :
<ImageButton
android:id="#+id/sync"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center_vertical"
android:contentDescription="#string/sync"
android:background="#android:color/transparent"
android:src="#drawable/sync" />
try the below code
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/your_image_name"/>

Android Button Customization

May i ask how to put icon on my button like the image below?
And also where can i have resources or sample resources of buttons like this?
You'll need to set the drawable to the left of your Button. Either via xml or programmatically.
Either create a button and set a compound drawable or create an ImageButton.
<TextView android:id="#+id/login_with_facebook"
android:layout_width="100dp"
android:height="48dp"
android:text="FACEBOOK"
android:gravity="center"
android:drawableLeft="#drawable/facebook_icon_48_x_48"
android:clickable="true"/>
<Button android:drawableLeft="#drawable/image"
android:text="facebook"/>
If u want space between image and text, then give drawablePadding Like:
<Button android:drawableLeft="#drawable/image"
android:text="facebook"
android:drawablePadding="10dp"/>
using RelativeLayout it can be done easily. here I got snippet for you,
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true">
<Button
android:id="#+id/fb_logo_button"
android:text=""
android:background="fb_logo"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
<Button
android:layout_toRightOf="#id/fb_logo_button"
android:id="#+id/fb_text_button"
android:text="Facebook"
android:textAlignment="center"
android:background="#android:color/holo_blue_bright"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
</RelativeLayout>
this will ensure that whatever your images are for respective DPIs, it will align this layout to center horizontally. Within this layout logo will be always to left of text as in picture.
This is applicable when your parent is also a RelativeLayout. If you got LinearLayout as parent then in above RelativeLayout tag change android:layout_centerHorizontal="true" to android:layout_gravity="center_horizontal"
I can't test it in eclipse as I'm out of home. Try this and let me know. Thanks.

How to add image for button in android?

How can I add an image to a button, rather than text?
Rather humorously, considering your tags, just use the ImageButton widget.
Simply use ImageButton View and set image for it:`
<ImageButton
android:id="#+id/searchImageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:src="#android:drawable/ic_menu_search" />
As he stated, used the ImageButton widget. Copy your image file within the Res/drawable/ directory of your project. While in XML simply go into the graphic representation (for simplicity) of your XML file and click on your ImageButton widget that you added, go to its properties sheet and click on the [...] in the src: field. Simply navigate to your image file. Also, make sure you're using a proper format; I tend to stick with .png files for my own reasons, but they work.
You should try something like this
<Button
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/qrcode"/>
the android:background="#drawable/qrcode" will do it
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="News Feed"
android:icon="#drawable/newsfeed" />
newsfeed is image in the drawable folder
The new MaterialButton from the Material Components can include an icon:
<com.google.android.material.button.MaterialButton
android:id="#+id/material_icon_button"
style="#style/Widget.MaterialComponents.Button.TextButton.Icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/icon_button_label_enabled"
app:icon="#drawable/icon_24px"/>
You can also customize some icon properties like iconSize and iconGravity.
Reference here.
Put your Image in drawable folder. Here my image file name is left.png
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="118dp"
android:layout_y="95dp"
android:background="#drawable/left"
android:onClick="toast"
android:text=" " />
You can create an ImageButton in your android activity_main.xml and which image you want to place in your button just paste that image in your drawable folder below is the sample code for your reference.
<ImageButton
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="49dp"
android:layout_weight="1"
android:onClick="prev"
android:src="#drawable/prev"
/>
Use android:drawableright or android:drawabletop,... attributes for buttons in xml file.
For example: in xml file add this attribute for a button:
Android:drawableleft="#drawable/ic_person"
or
Android:drawableright="#mipmap/ic_launcher"
you can use ImageView as Button. Create an ImageView and set clickable true after in write imageView.setOnClickListener for ImageView.
<ImageView
android:clickable="true"
android:focusable="true"`
android:id="#+id/imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
and in Activity's oncreate:
imageView.setOnClickListener(...

Android ImageButton with Image and Text

I want to have a button that has an image on top and some text on bottom. Both the image and text are decided during runtime, so I want to be able to combine ImageButton's setImageBitmap and Button's setText for each button.
Any ideas?
Thanks
Chris
Surround both image Button and text View code in .... to override text on image button's background. i.e:
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="69dp"
android:layout_marginTop="96dp"
android:background="#drawable/icon"
android:scaleType="fitXY" />
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Droid"
android:textColor="#ff0000"
android:textSize="24dip" >
</TextView>
</FrameLayout>
I finally found what I was looking for:
setCompoundDrawablesWithIntrinsicBounds lets me assign a bitmap/drawable for a Button, at the same time letting me use setText.
For eg: To set a bitmap image on top of the button, do something like
button.setCompoundDrawablesWithIntrinsicBounds(null, new BitmapDrawable(bitmapimage), null, null);
I don't think there is an easy way to do that.
I would create a custom-component.

Categories

Resources