Android/Monodroid image smaller than imagebutton - android

I want to create an imagebutton, but the image doesn't fill the imagebutton. The image has the same dimensions as the imagebutton.
The imagebutton has the correct dimension, it seems like the imagebutton has an padding?!
I tried most of the scaletypes.
My code:
ImageButton ib = new ImageButton(Context);
AbsoluteLayout.LayoutParams lp = new AbsoluteLayout.LayoutParams((int)getWidth(3), (int)rowHeight, (int)offsetValue, (int)(offsetRow+row*rowHeight));
Stream bitmap = act.Assets.Open("somefolder/" + name + ".gif");
Bitmap bMap = Android.Graphics.BitmapFactory.DecodeStream(bitmap);
bMap = Bitmap.CreateScaledBitmap(bMap, (int)getWidth(3), (int)rowHeight, false);
ib.SetScaleType(ImageButton.ScaleType.FitXy);
ib.SetAdjustViewBounds(true);
ib.SetImageBitmap(bMap);
bitmap = null;
bMap = null;
ib.LayoutParameters = lp;
layout.AddView(ib);

Yeah it has a padding or something like that, I wanted to create a button in an EditText in order to allow the user to make a password viewable and had the same issue... and haven't found a solution for this until now.
For what you are trying I recomment using an ImageView and make it clickable than you have the same functionality, just like an imagebutton,

Standard 9-patch drawable for ImageButton defined in Android framework has content padding which doesn't allow you to use all space inside the button. To solve this problem, use your own background drawable (set it with android:background="#drawable/..." attribute) or adjust the drawable you set in android:src attribute.
Please read this article for more information on how content padding is defined by 9-patch drawables.

Related

Setting LinearLayout Background Resource scale type programmatically

I have a linear layout that when i want programmatically to enter a TileXY background image to it i can do it that way:
ImageButton.SetImageBitmap(BitmapFactory.DecodeFile(Button1ImageURI));
Bitmap temp = BitmapFactory.DecodeFile(Button1ImageURI);
BitmapDrawable bitmapDrawable = new BitmapDrawable(Resources, temp);
bitmapDrawable.SetTileModeXY(Shader.TileMode.Repeat, Shader.TileMode.Repeat);
MainLinearLayout.Background = bitmapDrawable;
I'm not sure how i can do the same but this time i want the bitmapDrawable to ScaleType.FitCenter in the LinearLayout.
I'm not sure how i can do that, if i don't put a SetTileModeXY in the BitmapDrawable, Background in MainLinearLayout is scaled with FitXY,
but i want to be scaled in FitCenter
Why you want to set image as background on the LinearLayout, when you can create ImageView as a child of the LinearLayout with width and height that match the parent(LinearLayout), and use the benefits of the ImageView. Setting the image as source and use the ScaleType?

Should I use a LinearLayout with repeated background or a SVG ImageView to fill a whole row?

Linked questions
The below questions couldn't finally solve my problem, described below.
Android: Is it possible to repeat an (SVG!) drawable inside an ImageView?
Android: Keep ratio for a full-width and undefined height ImageView in a ConstraintLayout?
Showing a full-width ImageView containing a SVG distorts it
Context
I use a ConstraintLayout. My aim is to show an SVG image which contains a button:
Its width is 100% the screen's width - it must be repeated
Its height is defined to be the space between the bottom side of a widget and the bottom side of the button - it must be repeated
The problem
Each time I tried to show this image, either the drawable was badly scaled, badly cropped or blurred.
An exemple is:
It should look like this:
Many tests
I have tried to use an ImageView with the attribute src: I used every scale type, with and without the attribute that allows to set a custom ratio
I have tried to use an ImageView with a background instead of src
I have tried to use a RelativeLayout with a background drawable file that is repeated: so I didn't use an SVG image but its JPEG version, and even this way has resulted in bad results
Nota for 3.: I'd really want to use an SVG image instead of a bitmap one, because it will be resolutions-compliant.
My question
So, given all these explanations (cf.: part Context) and given the above illustrations, how would you proceed to show this image?
use java code
ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor("#888888")); // bg color
Drawable vDrawable = AppCompatResources.getDrawable(this, R.drawable.ic_vector_star); // vector drawable
if (vDrawable != null) {
Bitmap bitmap = Bitmap.createBitmap(vDrawable.getIntrinsicWidth(), vDrawable.getIntrinsicHeight(),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
vDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
vDrawable.draw(canvas);
BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(), bitmap);
bitmapDrawable.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT); // set repeat
LayerDrawable drawable = new LayerDrawable(new Drawable[]{colorDrawable, bitmapDrawable});
findViewById(R.id.frameLayout).setBackground(drawable);
}

Converting bitmap to drawable in Android

I'm downloading the image from server and storing it in bitmap object. I want to set this image as background for button. But the button doesn't have the property setImageBitmap. So is there anyway I can set the background of button with the downloaded bitmap image? Like by converting the bitmap to drawable? Sorry, I'm new to Android, please bear with my mistakes (if any).
P.S : I want to use button control only. Because I want some text at the bottom of each buttons and I'm creating these buttons dynamically.
The best way to convert a Bitmap to drawable in android is as follows,
Drawable drawable = new BitmapDrawable(getResources(), bitmap);
where bitmap is the name the Bitmap. Then set the drawable as your Button background as follows,
btn.setBackground(drawable);
N.B: Without specifying getResources() as the first argument, you may experience inconsistent image sizing across different screen densities.
for more info refer this
http://developer.android.com/reference/android/graphics/drawable/BitmapDrawable.html
Simply use BitmapDrawable.
Drawable drawable=new BitmapDrawable(contact_pic);
Convert Bitmap to BitmapDrawable like this
Drawable drawable = new BitmapDrawable(getResources(),your_bitmap);
and then set it as background to button as ,
button.setBackgroundDrawable(drawable);
P.S. You can also do the same inline as ,
button.setBackgroundDrawable(new BitmapDrawable(getResources(),your_bitmap));
This is how to do it, I've used several times:
Drawable drawable = (Drawable)new BitmapDrawable(Bitmap);
Button button= new Button(this);
button.setBackground(drawable);

Can I force a TextView to not set his dimension in order to fill the background?

I need to put an image (downloaded via HTTP request) as a TextView's background programmatically.
On this image I have to change the scaleType (FIT_XY, CENTER, FIT_CENTER, ...). Since that TextView doesn't allow to set the scaleType, my idea was to:
create an ImageView
set the downloaded image as background to the ImageView
set the ImageView's drawable as TextView's background
Unfortunately this approach doesn't work. I think that the TextView try to expand himself to bound the image.
Anyone can confirm my assumption?
Any possible solution?
CODE ADDED
ScaleType scaleType = ImageUtil.parseStretchOption(model.background.backgroundImageStretchOption);
ImageView iv = new ImageView(getContext());
iv.setScaleType(scaleType);
iv.setBackground(new BitmapDrawable(getContext().getResources(), future.get()));
TextView view = new TextView(getContext());
view.setBackground(iv.getBackground());

Android - How to change grey background created by a BitmapDrawable

I'm using the follow code, to create a non-scaled, centred image as a background, in a relative layout:-
RelativeLayout explosionlayout = (RelativeLayout) findViewById (R.id.explosionlayout);
explosionlayout.setBackgroundColor(R.color.white);
Bitmap myBitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.bomb);
BitmapDrawable test1 = new BitmapDrawable(myBitmap);
test1.setGravity(Gravity.CENTER);
The only problem I have, is that the background of the relativelayout is grey, regardless of what I set it to, either via XML or in code.
Any ideas would be appreciated, thanks.
You probably want this instead:
explosionlayout.setBackgroundColor(getResources().getColor(R.color.white));
or just
explosionlayout.setBackgroundColor(0xffffffff);
The reason is that R.color.white is an ID, while setBackgroundColor expects an actual 32-bit integer representation of the color.

Categories

Resources