How do you change the size of an ImageView? - android

I have setup my ImageView like the following:
<ImageView android:id="#+id/dl_image"
android:layout_width="60dp"
android:background="#drawable/pictureframe"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:adjustViewBounds="true"
android:scaleType="fitCenter"/>
Notice that layout_width is fixed to 60dp. Depending on the content that I acquired online, I want to resize this width to 90dp, or 120dp (while maintaining the aspect ratio of the image).
I tried using setLayoutParams, but passing LayoutParams(120, LayoutParams.WRAP_CONTENT) throws an exception. It doesn't seem to like it.
If possible, I am trying to avoid making another ImageView for larger sizes.

If you're working with an existing view it can be a lot of work creating a new set of LayoutParams from scratch. Instead - you can grab the view's existing LayoutParams, edit these, and then apply them to the view to update its LayoutParams using setLayoutParams()
ImageView imageView = findViewById(R.id.dl_image);
LayoutParams params = (LayoutParams) imageView.getLayoutParams();
params.width = 120;
// existing height is ok as is, no need to edit it
imageView.setLayoutParams(params);
Make sure you import the correct type of LayoutParams. For this case, as you commented, you can just use the LayoutParams of a ViewGroup. If you were setting parameters specific to a certain type of view (e.g. alignments in RelativeLayouts) you would have to import the LayoutParams of that type of view.

you can do it all like
ImageView imageView = findViewById(R.id.dl_image);
imageView.getLayoutParams().width = 120;

Related

How to set a textView width dynamically larger than the width of the screen after animation?

I have a text view with a height which wraps content and a width that matches parent. So that the textView width == the screen Width.
But at one point I want the text to rotate 90 degrees.
Now I want to be able to change the views width so that it is the devices height instead of width.
This would cause that the width would expand.
(Basically like when one does orientation changes, but I can´t just have an orientation change for only one textview so I have to rotate it.)
My problem: I can´t set the textViews width larger than the device width.
Even when I am already done with the animation.
So is it possible to make the textView width larger than the device width?
and if not can anyone please suggest how I could solve my problem because I really need to change the orientation of the textView...
EDIT----EDIT
Is there a way to create a landscape text view and put it in a portait activity? That would completly solve my problems of the last week...
EDIT
My fragment:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/f"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/t"
/>
</RelativeLayout>
I tried different things like
fragment.setRotation(90);
LayoutParams params = fragment.getLayoutParams();
params.height = text.getHeight();
params.width = deviceHeight;
fragment.setLayoutParams(params);
LayoutParams params1 = text.getLayoutParams();
params1.height = text.getHeight();
params1.width = deviceHeight;
text.setLayoutParams(params1);
EDIT________EDIT
Or has anyone ever written something like a verticalTExtView Class?
You can set the attributes of TextView as android:singleLine="true" and android:layout_width="wrap_content" and android:layout_height="wrap_content" // incase if you do not want the height to be increashed so singleLine is making the text to go out of screen width then you will need ScrollView for such behavior.
Hope this helps.

Android set ImageView's width as a ratio of another element (given the id)

I have an ImageView:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/pbc"
android:layout_below="#id/ryc"/>
Is it possible to set it's width to be a percentage of that of another element of id "#id/ryc" just like i've done so in android:layout_below ?If so, how?
Thanks
You will need to set the width in your activity, like the following code (just change yourPercentage to the percentage you want)(I have added an id to your imageview, but you can change it):
XML:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/pbc"
android:id="#+id/pbcImageView"
android:layout_below="#id/ryc"/>
Activity:
float perc = yourPercentage / 100;
ImageView imgView = (ImageView) findViewById(R.id.ryc);
ImageView pbcImageView = (ImageView) findViewById(R.id.pbcImageView);
pbcImageView.setWidth(imgView.getWidth() * perc)
Hope it helps!
Possibly, you can put them into LinearLayout and assign proportional weights.
If your design doesn't allow to do so and the size of the views needs to reflect dynamical changes, try overriding onSizeChanged() of one of the views and resize the other in this method. Otherwise giacomoni's answer is the most suitable.

Android, Change Image size in ImageView

For Android, How can I change the image size in an ImageView in a layout?
My image is jpeg format.
<ImageView
android:id="#+id/imageView1"
android:layout_margin="20dp"
android:src="#drawable/stop"
android:layout_width="50dp"
android:layout_height="50dp"/>
Just change width and height attribute.
You can also set Params using LayoutParams
ImageView yourImageView= (ImageView)findViewById(R.id.imageId);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(30, 30);
//width and height of your Image ,if it is inside Relative change the LinearLayout to RelativeLayout.
yourImageView.setLayoutParams(layoutParams);
There are a few ways to change the size of an image in an imageView in XML.
Scale the image by pulling around the edges of the imageView in the Graphical Layout (this is not really advised since the Graphical Layout sometimes adds undesired properties into the XML).
Apply a weight to the imageView (this requires the parent object to have specified a weightSum), causing the image to scale according to the screen size. This seems to be the most reliable way to gauge how your layout will look on many different sized screens.
Simply adjust the weight and height in the imageView (as others have mentioned).
<ImageView
android:id="#+id/imageViewId"
android:src="#drawable/icon"
android:layout_width="xxdp"
android:layout_height="yydp"/>
where xx is the width in pixels and yy is the height in pixels.
(give integer values as you desire the size to be)

Resize ImageView in run time using a button - Android

I am displaying an image using ImageView container. the image needs to be resized when a user enters a specific value and clicks update
resize is just for display, the original resource remains as it is. NO editing, save, etc
xml layout
<ImageView
android:background="#drawable/picture01"
android:id="#+id/picture01holder"
android:layout_below="#+id/TextView01"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
</ImageView>
main.java
final ImageView pic01 = (ImageView)findViewById(R.id.picture01);
now what do i do to dynamically assign this pic01 a size of my choice. just the function, i can implement it inside a button myself. i believe i have to use something like pic01.setLayoutParams but i dont know how to use it.
basically i want to overwrite layout_height="wrap_content" and layout_width="wrap_content" in .java
change the Imageview size by taking LayoutParams
final ImageView pic01 = (ImageView)findViewById(R.id.picture01);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(50, 50);
pic01.setLayoutParams(layoutParams);
First of all, you have to set the scale type of the image to CENTER_INSIDE or CENTER_CROP depending on your preferences. You should do this in your onCreate() method.
myImageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
Afterwords you just have to resize the image view in your layout. This will depend on the type of layout you're using. An example for LinearLayout would be:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(100,200);
myImageView.setLayoutParams(params);
Or you can set the components minimal size:
myImageView.setMinimumWidth(200);
myImageView.setMinimumHeight(200);
In both cases make sure that the size can be achieved inside the layout. If you have multiple components with smaller weights, the image view may be unable to take up the space you need.
RelativeLayout.LayoutParams myParams = new RelativeLayout.LayoutParams(yourWidthHere, yourHeightHere);
pic01.setLayoutParams(myParams)

Android: Change absolute position of a view programmatically

If you use an AbsoluteLayout (I know that it is deprecated, but it was the only way to solve my problem) you can give the childViews the tag android:layout_x and android:layout_y to set their absolute position within the AbsoluteLayout.
However I don't want to set these information in the xml, because I only know them at runtime. So how can I set these parameters at runtime programmatically? I don't see any method on the View like view.setLayoutX(int x) or something.
Here is my XML, which works fine, when I set the layout_x and layout_y values:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:src="#drawable/myImageView"
android:layout_width="1298px"
android:layout_height="945px"
android:layout_x="0px"
android:layout_y="0px" />
<Button
android:id="#+id/myButton1"
android:text="23"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="50px"
android:layout_y="300px"
android:tag="23"/>
<Button
android:id="#+id/myButton2"
android:text="48"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="50px"
android:layout_y="300px"
android:tag="48"/>
</AbsoluteLayout>
In fact, I don't want to set any button within the xml anymore, but rather retrieve some information via remote and add buttons depending on that information.
Here is the part the code I'm using so in my onCreateMethod to add these buttons:
for (MyRemoteObject remoteObject: list) {
Button button = new Button(this);
button.setOnClickListener (listener);
button.setTag(remoteObject.id);
button.setText(remoteObject.id);
// button.setLayoutX(remoteObject.x) ????
// button.setLayoutY(remoteObject.y) ????
myLayout.addView(button);
}
Use the version of addView that takes LayoutParams:
LayoutParams params = mLayout.generateLayoutParams();
params.x = remoteObject.x;
params.y = remoteObject.y;
mLayout.addView(button, params);
In response to your comment on Mayra's answer, I had a similar issue with RelativeLayout instead of AbsoluteLayout. The solution was to use a similar method and cast it as your layout type. Something like this:
LayoutParams params = (android.widget.RelativeLayout.LayoutParams) this.generateDefaultLayoutParams();
params.height = 360;
params.width = 360;
params.addRule(CENTER_IN_PARENT);
this.addView(board, params);
It took me forever to figure this out so I wanted to post it here for someone else if they needed it.
I had just the same problem but found a somewhat different solution.
int width = 100, height = 50, x = 10, y = 20;
Button button = new Button(this);
AbsoluteLayout myLayout = (AbsoluteLayout)findViewById(R.id.myLayout);
myLayout.add(button, new AbsoluteLayout.LayoutParams(width, height, x, y));
And if you find out what to use for "fill_parent" (hint try -1) then you may use those constants for width and height.
I don't know why, but when moving top and left edges the Android keeps the right and bottom edges in same place. As I could not change the width and height properly (the image was disappearing), I've used this:
LayoutParams layoutParams=new LayoutParams(width, height);
layoutParams.leftMargin = newLeft;
layoutParams.topMargin = newTop;
imageView.setLayoutParams(layoutParams);
I don't know if it's the best way, but worked for me. I've tested it in 2.2+ and works fine!
You must import android.widget.LinearLayout.LayoutParams; because the default is ViewGroup's LayoutParams (by Davit T)
the above answers are right.but this one would be more perfect
AbsoluteLayout.LayoutParams layoutParams = new AbsoluteLayout.LayoutParams(AbsoluteLayout.LayoutParams.WRAP_CONTENT,AbsoluteLayout.LayoutParams.WRAP_CONTENT,DragLayer.int left_location,int top_location);
layout.addView(view,layoutParams)

Categories

Resources