Making Android src image resource fill button area - android

I have the following XML:
<ImageButton
android:id="#+id/SwapTextButton"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_weight="0.5"
android:background="#drawable/my_gradient_button"
android:scaleType="fitCenter"
android:src="#drawable/extended_text" />
And it outputs a button which looks like this:
Is there any way to increase the size which the src resource takes up? By that I mean maintain the src within the area of the button, but make it larger than it is now.
I have tried changing the scaleType, but any other setting causes the src resource to display parts of itself outside the button boundary (aka it becomes too large).
I've had a research and there seems to be solutions based in java, but ideally I'd like to keep the solution in xml too.
Can someone suggest a fix/point me in the right direction?
Thanks

<ImageButton
android:id="#+id/SwapTextButton"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_weight="0.5"
android:background="#drawable/my_gradient_button"
android:scaleType="fitXY"
android:src="#drawable/extended_text" />
Try this, fitXY for scaletype should "fill" your ImageButton

This worked for me:
android:padding="0dp";

Related

Android button elevation shadow not working

I am trying to make a button that has a shadow using elevation with a background image being my sign in with gmail png. The button is contained within a relative layout. The elevation won't show no matter what I try to do. I tried solutions from other forum questions and none worked.
Here is my code for the button
<Button
android:id="#+id/google"
android:layout_width="270dp"
android:layout_height="38dp"
android:layout_marginTop="30dp"
android:layout_centerHorizontal="true"
android:background="#drawable/google"
android:elevation="10dp"
android:layout_below="#id/slogan"/>
The google drawable is a png image that I exported from Adobe XD.
Could someone please give me a pointer on what I am doing wrong? Thanks.
Additionally, I realize that if I set the background of the button to android:color/white the shadow appears. So I think the issue is with the png drawable? Does elevation not work with png images? Is there a workaround?
Use below the line of code to show an elevation in button
android:outlineProvider="bounds" – K. Sopheak
That works, thanks!
try this I hope it helps, because another view or layout just after your button is hiding shadow
android:layout_marginBottom="16dp"
For Material Button, I tried the following and it worked
android:translationZ="5dp"
Since you're using an image, replace the <Button> tag with that of an <ImageButton>. So, your workaround code would be:
<ImageButton
android:id="#+id/google"
android:layout_width="270dp"
android:layout_height="38dp"
android:layout_marginTop="30dp"
android:layout_centerHorizontal="true"
android:src="#drawable/google"
android:elevation="10dp"
android:layout_below="#id/slogan"/>
Take note that the android:src attribute is being used here rather than the android:background attribute.
This one worked for me!
try this , I hope this help you ||
android:layout_margin="5dp"

ImageButton Border Transparancy - Removing the square border around round image is not working?

Hello Guys I need some help in creating Imagebutton. I created custom button on PS and then saved it in PNG format with transparent background. After that i patched my image with 9patch. Now i am using that image as a button in my layout but the image is showing colored background corners. I tried alot of things but its not removing.
Here is my XML
<ImageButton
android:id="#+id/mybutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:src="#drawable/registerbutton"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginBottom="123dp" />
Keep the background attribute as #null like this :
android:background="#null"
If still you are getting that corner, it might be from the png file that you are using.
This is an example of what i get from #null
Probably the ouput of "9patching" operation added corners. Open the file and check if there are corners or not.
If that won' help, try switching android:src attribute with android:background attribute.
And u can make .png button online using this site

9-patch png doesn't want to stretch properrly

I am having some weired problem with 9-patch image in my android app.
I want to make a line across a screen with a simple png. After creating 9-patch from it I've simply added it as a drawable to ImageView container. Unfortunatelly it doesn't want to stretch and still has this same dimmensions. Because I've never used 9-patch png I have no idea what am I doing wrong and also searching in Google is not so helpful, because there are only info, how you can make one, now how you can use it.
This is my code fragment from xml file:
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="#drawable/testpng" />
and this is a image I want to add:
Well, if it's just a simple line,you wouldn't need a 9 patch...
Just use a View, give it a color as a background and give it a 1 px dimension:
<!-- Horizontal line -->
<View
android_layout_width="match_parent"
android_layout_height="1px"
android:background="#ffff"
/>
<!-- Vertical line -->
<View
android_layout_width="1px"
android_layout_height="match_parent"
android:background="#ffff"
/>
For the Shadowed line:
I'm using it so:
<View
android:layout_width="match_parent"
android:layout_height="2px"
android:layout_centerVertical="true"
android:background="#drawable/line_h"
/>
and this is line_h.9png, as small as I could make it (6*4 px) ;)
Now, this doesn't show in my graphical layout editor, but it does at run time!
Check that the extension of your file is .9.png. If it isn't, it's not recognized as a 9-patch.
I found an answer to my question. The missing thing was one line of code android:scaleType="fitXY".
Now it looks like this
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="#drawable/testpng" />

How can I stretch the image in imagebutton to the whole area of the imagebutton?

Here is a picture that will help you understand my problem:
I want to stretch the image shown inside ImageButton to the whole area of the ImageButton.
As you can see, the picture with the number 1 only take about 95% of the ImageButton and you can also see the boundaries of the ImageButton.
I still want to maintain the image button style
Here's how I am using ImageView:
<ImageButton
android:id="#+id/secondActivityBbuttonDownRight"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:scaleType="fitStart"
android:src="#drawable/111" />
I think I found out what is my problem.
I need a custom component that can contain only an image and have a clicking effect.
ImageButton is ImageView.
If you want your image to fill button
than set android:scaleType="fitXY", so the image you set in android:src will be stretched to fill whole image button.
ImageButton has a background under your image and padding.
If you do not want background
set android:background="#android:color/transparent".
If you do not want padding
set android:padding="0" or set background without padding.
You can also use
android:scaleType="centerCrop"
android:padding="0dp"
android:scaleType="fitXY" tested and works correctly.
Use below code :-
<ImageButton
android:id="#+id/secondActivityBbuttonDownRight"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:scaleType="fitStart"
android:background="#drawable/111" />
You should consider creating a 9-patch image to make the image fit correctly on an ImageButton in case you need to stretch it, but you don't want the image to become distorted.
To have a button with an image that completely covers it, you can use something like this:
<ImageButton android:id="#+id/iconBtn"
android:layout_width="64dip"
android:layout_height="64dip"
android:src="#drawable/icon_button"
android:scaleType="fitCenter"
android:background="#00000000"/>
For a full tutorial on creating buttons with custom skins and the use of selectors to manage their states, you can check a tutorial I've written some time ago here (Creating a custom Android button with a resizable skin).
If your image "111" has no padding itself then simply use android:background"#drawable/111"
Just set this two attributes on the ImageButton:
android:background="?attr/selectableItemBackgroundBorderless"
android:scaleType="centerCrop"
With those, you will get the provided src to be stretched, and also if you put some specific width and height on that ImageButton you won't get a rectangle around it.

Draw an Image beside an Edit text in android

I want to draw an image on the left of an EditText. I don't want the image appear insde the EditText though.
I use this:
<EditText
android:id="#+id/firstNameTxt"
style="#style/UserInfoInputs"
android:drawablePadding="20dp"
android:drawableLeft="#drawable/first_name" >
</EditText>
It displays the image inside of the EditText. However I use this on TextView and it works fine:
<TextView
android:id="#+id/positionValue"
style="#style/userInfo"
android:drawableLeft="#drawable/position" />
How this can be done for an EditText?
try this
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/go_image"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="#drawable/icon"
/>
<EditText
android:id="#+id/url"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:lines="1"
android:layout_weight="1.0" />
</LinearLayout>
let me know if this works.
The difference between the two use-cases you describe are simple. There is no difference. With an EditText, there are lines that are easily discernible. With a TextView there is not. Try setting the background property of the textview, and you'll see that the drawable is, in fact, drawn on the left side, but still 'inside', the TextView.
The simplest way to accomplish your task (as described) is to utilize an ImageView. Depending on what ViewGroup (LinearLayout, RelativeLayout, etc) you're using, the code may be a bit different; so, update your question with the appropriate info and I'll make my answer a bit more specific.
I should note that, another method you could use is to create your own custom component, which is really very easy to do. See this article Custom Components | Android Developer, be sure to scroll down to the Compound Controls heading title "Compound Controls". This would be especially helpful when this is a 'common' format of controls that you'll use often (I.E. you have an image next to a TextView throughout your app).
That is because the background part of the EditText stretches behind the entire contents of the view, including the drawables.
If you are using a RelativeLayout you can just add a separate ImageView:
<ImageView
layout_width="wrap_content"
layout_height="wrap_content"
layout_toLeftOf="#+id/firstNameTxt"
src="#drawable/first_name"
other imageview attributes as neccessary...
/>
<EditText
android:id="#+id/firstNameTxt"
style="#style/UserInfoInputs"
/>
Or if you use another kind of layout, create a LinearLayout-wrapper:
<LinearLayout
layout_width="wrap_content"
layout_height="wrap_content">
<ImageView
layout_width="wrap_content"
layout_height="wrap_content"
layout_toLeftOf="#+id/firstNameTxt"
src="#drawable/first_name"
other imageview attributes as neccessary...
/>
<EditText
android:id="#+id/firstNameTxt"
style="#style/UserInfoInputs"
/>
</LinearLayout>

Categories

Resources