Android. ImageButton, stretch background and center src - android

I have a ImageButton with 2 images
What I need from this imagebutton if having the backgrounf image to stretch, etc the src image to be centered in the stretched button)
The background image(textured gray) to stretch between 2 ImageButton
The src image("Title") to be centered on this button.
Still 2 problems:
1- Background stretch seem to work, but only if I replace layout_width="wrap_content" with layout_width="fill_parent", BUT, the button doesn't from before the other button.
2- even with fill parent, i get the src image also stretched.
Suggestions welcome!
xml:
<ImageButton android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btnListviewHeader_Title"
android:background="#drawable/a_btn_sort_title"
android:onClick="listView_FilterBy_Title"
android:nestedScrollingEnabled="true"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:layout_alignBottom="#+id/btnListviewHeader_Number"
android:cropToPadding="true"
android:src="#drawable/a_btn_sort_title_en"
android:layout_alignTop="#+id/btnListviewHeader_Number"
android:layout_toLeftOf="#+id/btnListviewHeader_Fav"
android:layout_toRightOf="#+id/btnListviewHeader_Number"
android:layout_toEndOf="#+id/btnListviewHeader_Number" />

Finally found my answer:
1- ImageButton between 2 ImageButton = remove: "android:layout_toEndOf="#+id/btnListviewHeader_Number""
2- Src centered: android:scaleType="fitCenter"
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btnListviewHeader_Title"
android:background="#drawable/a_btn_sort_title"
android:onClick="listView_FilterBy_Title"
android:nestedScrollingEnabled="true"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:layout_alignBottom="#+id/btnListviewHeader_Number"
android:cropToPadding="true"
android:src="#drawable/a_btn_sort_title_en"
android:layout_alignTop="#+id/btnListviewHeader_Number"
android:layout_toLeftOf="#+id/btnListviewHeader_Fav"
android:layout_toRightOf="#+id/btnListviewHeader_Number" />
Hope it help someone :)

Related

Buttons background turns to blue color (not same as original)

my problem seems to be easy but I have no idea how to solve it.
I have a few buttons and all of them have a background image. The problem is, all images turns into blue color
It looks like this:
Original photo:
Here's a part of my xml code:
<Button
android:id="#+id/memory_button1"
android:layout_weight="1"
android:maxWidth="30dp"
android:maxHeight="30dp"
android:onClick="#{()->viewModel.showTile(1)}"
android:background="#{viewModel.button001}">
</Button>
Icons url:
https://www.flaticon.com/packs/restaurant-225
Better approach to use ImageView as button with ripple click effect would be
<ImageView
android:id="#+id/memory_button1"
android:layout_weight="1"
android:maxWidth="30dp"
android:maxHeight="30dp"
android:background="?selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:onClick="#{()->viewModel.showTile(1)}"
android:src="#{viewModel.button001}"/>
Also you can even wrap it in a CardView
If you want to have the only image in your button. you can replace it with ImageView so that you can have full control of how the image is being drawn.
<ImageView
android:id="#+id/memory_button1"
android:layout_weight="1"
android:maxWidth="30dp"
android:maxHeight="30dp"
android:onClick="#{()->viewModel.showTile(1)}"
android:src="#{viewModel.button001}">
</ImageView>
You will option for scale type for ImageView's so that you can render image without stretching it.

Remove whitespace in imagebutton

In my Image Buttons, there is a lot of white space around the source image. Is it possible to remove this space so that the imagebutton just surrounds the source image itself.
<ImageButton
android:id="#+id/story_1"
android:src="#drawable/my_story"
style="?android:attr/borderlessButtonStyle"
android:layout_width="55dp"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:contentDescription="Add my story" />
Try this
<ImageButton
android:id="#+id/story_1"
android:src="#drawable/my_story"
style="?android:attr/borderlessButtonStyle"
android:background="#null"
android:layout_width="55dp"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:contentDescription="Add my story" />
Probably the problem caused by the image transparency. Try remove the white spaces around the image and make those areas transparent.
make sure the original image you are using doesn't have too much white since you already have the solution in your xml scaleType="fitXY" you can try scaleType="fitCenter" but your image is already stretched to fit your entire button in the orginal code

How to remove the square border from a image button

I have an image button shape like a 'star' and i what the clickable area to be only the 'star' but instead of that i have a transparent square clickable area around the 'star', can anyone please tell me how to remove the square bcoz the next 'star' it's close and the square overlaps the first 'star'.
thank you so much
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:layout_weight="1"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="190dp"
android:layout_marginBottom="180dp">
<ImageButton
android:id="#+id/star"
android:layout_width="120dp"
android:layout_height="120dp"
android:background="#drawable/star"
/>
Instead of ImageButton you can user ImageView and make in clicable.
Set
android:background=#android:color/transparent
and insert
android:src=#drawable/star
Or just use an ImageView instead, because you can have OnClickListener on both, therefore they could also function as described in your question.
You can use:
android:background="#null"
in the imageButton tag.

Sudden Issue in setting image border

I have to show circular profile picture with border around it. I have a background image for border. I am setting image "src" with padding of 3 dp.
My Image view in XML looks like:
<ImageView
android:id="#+id/pic_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/blank_avatar"
android:padding="3dp"
android:scaleType="fitXY"
/>
I am setting image to this image view using "setImageBitmap(Bitmap bitmap)" programatically.
I googled and tried setting property
android:cropToPadding="true"
but this is also of no use. Padding is being ignored. so no border is shown around image.
Any help will be highly appreciated.
this is what i have done to make the borders
1st - in the layout file i did the following :
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"
android:background="#123456"
android:padding="10dp"
android:id="#+id/imageView"
android:scaleType="fitXY"/>
2nd - in my activity i used the same method you are using to change the image "setImageBitmap(bitmap)" like the following :
ImageView imageView = (ImageView)findViewById(R.id.imageView);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.aaaa);
imageView.setImageBitmap(bitmap);
and i got a image with the padding around it with 10dp like the image attached.
Hope that helps .
Change properties of ImageView android:scaleType="fitXY" to android:scaleType="center" or android:scaleType="fitCenter" and try again .
Hope this will work .

android ImageButton do not stretch image

How can I prevent the ImageButton from stretching the image?
Using this snippet:
<ImageButton
android:src="#drawable/prediction"
android:background="?android:attr/selectableItemBackground"
android:gravity="center_horizontal"
android:scaleType="centerInside"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:contentDescription="some description"
android:text="#string/predict"
local:MvxBind="Click ExecuteQueryCmd" />
I get the following effect:
Image is 64x64 png.
All I want is for the ImageButton to respect native width and length, hence resolution, of the image it is being assigned. Recommendations based on a similar post is to use a Button, set its background to an image then use custom selectors but this sounds like a hack. Or is this the proper way to do this?
It work for me with
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:background="#android:color/transparent"
android:contentDescription="some description"
android:scaleType="centerInside"
android:src="#drawable/ci_icon"
android:text="#string/predict"
local:MvxBind="Click ExecuteQueryCmd" />
Put your image in android:src attribute
Set the width of the ImageButtons to fill_parent and use scaletype fitStart for the images that hug the left margin, and fitEnd for the ones on the right. Should do the trick, at least as far as your example image goes. You may have some spacing issues if the proportional width of the images exceed the screen width, but it should work for you.
Do this:
<ImageButton
android:src="#drawable/prediction"
android:background="?android:attr/selectableItemBackground"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:contentDescription="some description"
android:text="#string/predict"
local:MvxBind="Click ExecuteQueryCmd" />

Categories

Resources