Resize checkbox image in Android programmatically - android

I want to resize the image of the CheckBox programmatically, but i want to use the original images of the phone. I just want to scale it.
I don't wand to supply own images, because i want to keep the phone's look, and i would still have to scale them.
I already tried to get the systems image resources using
Setting Android CheckBox to a different image... and then back to the original images
but i still could not scale the drawable. (I tried drawable.setBounds()).
I'll probably also have this issue with radio buttons.
Thanks for your help!

you should just be able to set the layout parameters on the checkBox:
LayoutParams lp = findViewById(R.id.chk_id).getLayoutParams();
lp.width = width;
lp.height=height;
findViewById(R.id.chk_id).setLayoutParams(lp);
By getting the layoutparams before you presever any other layout setting associated with the view
width and height are in pixels, to scale it for density use:
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = (int)(150*dm.density);
you need to use the Container class for the Layout params this example is for a FrameLayout but just replace that with tyhe type of the parent container.

Related

Resize ImageView programmatically

I'm working with an ImageView and I want to resize it programmatically passing (for example) from a full-screen ImageView to a 50x50. Is there a way to do that?
Different from the one you suggested me to see because I don't need to fit the image in the ImageView but to resize the dimension of the ImageView.
My suggestion use a relative layout and put image inside that has with weight and able to auto resize which depends on the weight. The best way to auto resize even your screen rotates.
I didn't remember which one layout has weight format
You can use LayoutParams to set height width programmatically -
ViewGroup.LayoutParams layoutParams = imageView.getLayoutParams();
layoutParams.width = 30;
layoutParams.height = 30;
imageView.setLayoutParams(layoutParams);
ConstraintLayout provides a mechanism for fixing the aspect ratio of a child view. Select the child view that we want to control, and then set the ratio value, we can manually put the ratio that we want to set.
Using constraintLayout makes UI shrink or expand according to screen size without distorting the UI. This is the most important advantage of using ConstraintLayout in UI.

Android image 1/3rd of the screen with scrollview

In my app, I download images from my web server (each having a different resolution) but I would like to show these images on a fragment screen consistently.
Ideally, I would like the image to have width="match_parent" and height to take 1/3 of the screen EXACTLY. Furthermore, the image should be shown in a as it's part of a content layout with other controls that could possibly grow larger than the screen height (hence the need for scrolling).
I have tried putting the image and the rest of the content in a LinearLayout and then setting the weight to 1 (with max weight being 3), but since the image and the other contents are in a scrollview, it doesn't quite seem to work. the image is either too large or too small (depending on the orientation and the resolution of the image) and my 1/3rd settings don't seem to be respected.
Is there any way to do this other than fixing the height to a pre-determined (dp) value? I would like to avoid that unless there's no other choice.
Many thanks,
You could always set the height for each of the images programatically to 1/3 the height of the screen, like so
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height = metrics.heightPixels;
ImageView img = (ImageView) findViewById(R.id.image);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) img.getLayoutParams();
params.height = (int)height / 3;
img.setLayoutParams(params);

Layout stretching

Is there a layout, that would allow me to make it in absolute values, but when it would be on larger / smaller screen, it would stretch and adjusted those values, to fit onto the screen but preserve the same look?
Relative layout still keens on exact values.
For example, I have a button and I want it to have it the width of 1/3 of the screen of every device.
Setting manualy in code the width of an element to (for example) screenWidth/3 works. Yet I don't think it's clean. But this technique works.
Find device dimensions at runtime and set width of button at runtime.
Display mDisplay = getWindowManager().getDefaultDisplay();
int deviceWidth = mDisplay.getWidth();
int deviceHeight = mDisplay.getHeight();
button.getLayoutParams().width = deviceWidth / 3;
Give the values in dpi of your layout and view and they will adjust themselves on any screen

how to set a button layout_marginTop/layout_marginBottom in runtime?

I am using a relative layout with 4 buttons (in the center, one below the other).
My problem is to adjust the gap between all buttons so it will be the same between all buttons.
I want to do it dynamically according to the height of the screen (i use Display class to get the actual height).
what is the best way to do it?
Thanks,
Amigal
You can do this via modifying the LayoutParams of your View
Button b;// your button
RelativeLayout.LayoutParams lp=(RelativeLayout.LayoutParams)b.getLayoutParams();
lp.leftMargin=10;//your left margin here
lp.topMargin=10;//your top margin here and do this for other 2 margins if you need to
sometimes you need to call
b.setLayoutParams(lp);
to have the changes applied
also i dont know how you get the screen dimensions, but this works at every API:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);

how to set width of button with respect to the resolution available on android device

my current android project requires the width of button should be set dynamically depending on the resolution available on device
i want to arrange 9 button in a row but the size will depend on the resolution available
for the current scene we are using below code
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
//int sheight = displaymetrics.heightPixels;
int swidth = displaymetrics.widthPixels;
int px=swidth/11;
bW.setWidth(px);
bE.setWidth(px);
bR.setWidth(px);
bT.setWidth(px);
bY.setWidth(px);
bU.setWidth(px);
bI.setWidth(px);
bO.setWidth(px);
bP.setWidth(px);
somehow the width of button is not changed in any manner it remains the same
If i consider you buttons to be images, that you have set using the Selector file, then you need to keep those files in drawable-hdpi, drawable-mdpi, and drawable-ldpi, and the images will accomodate itself according to the available resolution..
i think you should use android:layout_weight property.Its going to consume space depending on the width available.
set to all the buttons as android:layout_weight="1"
Try this url
Linear Layout and weight in Android
Buttons and other objects that uses a ninepatch image won't get smaller than the ninepatch itself.
Make sure you are using an appropriate ninepatch image for you buttons that will fit the sizes you need.
i came up with this solution
ViewGroup.LayoutParams params = button.getLayoutParams();
params.width=px;
button.setLayoutParams(params);

Categories

Resources