Android ImageView change maxHeight property through code
I have a scrollable view in which I'm inserting various views programatically. Some views are text fields, some are images.
Most of the images that I insert are horizontal images but some of the images are vertical. For vertical images I want to modify my ImageView's maxHeight property programatically. So I've put an approximate check if(height > (width * 1.3) )
This is my Imageview xml
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:background="@null"
android:maxHeight="200dp"
android:scaleType="fitCenter" />
And this is my code:
if (h > (1.3 * w)) {
imageView.setMaxHeight(500);
}
But this doesn't work. Can anybody tell me why & what's the solution?
android imageview
add a comment |
I have a scrollable view in which I'm inserting various views programatically. Some views are text fields, some are images.
Most of the images that I insert are horizontal images but some of the images are vertical. For vertical images I want to modify my ImageView's maxHeight property programatically. So I've put an approximate check if(height > (width * 1.3) )
This is my Imageview xml
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:background="@null"
android:maxHeight="200dp"
android:scaleType="fitCenter" />
And this is my code:
if (h > (1.3 * w)) {
imageView.setMaxHeight(500);
}
But this doesn't work. Can anybody tell me why & what's the solution?
android imageview
add a comment |
I have a scrollable view in which I'm inserting various views programatically. Some views are text fields, some are images.
Most of the images that I insert are horizontal images but some of the images are vertical. For vertical images I want to modify my ImageView's maxHeight property programatically. So I've put an approximate check if(height > (width * 1.3) )
This is my Imageview xml
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:background="@null"
android:maxHeight="200dp"
android:scaleType="fitCenter" />
And this is my code:
if (h > (1.3 * w)) {
imageView.setMaxHeight(500);
}
But this doesn't work. Can anybody tell me why & what's the solution?
android imageview
I have a scrollable view in which I'm inserting various views programatically. Some views are text fields, some are images.
Most of the images that I insert are horizontal images but some of the images are vertical. For vertical images I want to modify my ImageView's maxHeight property programatically. So I've put an approximate check if(height > (width * 1.3) )
This is my Imageview xml
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:background="@null"
android:maxHeight="200dp"
android:scaleType="fitCenter" />
And this is my code:
if (h > (1.3 * w)) {
imageView.setMaxHeight(500);
}
But this doesn't work. Can anybody tell me why & what's the solution?
android imageview
android imageview
asked Nov 22 '18 at 23:39
Pranav MahajanPranav Mahajan
7561432
7561432
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Since your value 500 is a pixel value. So, I think you need to convert 500 to corresponded dp value. Try to use this:
private int dpToPx(int dp, Activity activity) {
Resources r = activity.getResources();
return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics()));
}
and change your code to this:
if (h > (1.3 * w)) {
imageView.setMaxHeight(dpToPx(500, this));
}
Tried. Didn't work.I think something more needs to be done for setMaxHeight() to work programatically. Maybe it doesn't work after the view is rendered
– Pranav Mahajan
Nov 30 '18 at 14:08
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439127%2fandroid-imageview-change-maxheight-property-through-code%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Since your value 500 is a pixel value. So, I think you need to convert 500 to corresponded dp value. Try to use this:
private int dpToPx(int dp, Activity activity) {
Resources r = activity.getResources();
return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics()));
}
and change your code to this:
if (h > (1.3 * w)) {
imageView.setMaxHeight(dpToPx(500, this));
}
Tried. Didn't work.I think something more needs to be done for setMaxHeight() to work programatically. Maybe it doesn't work after the view is rendered
– Pranav Mahajan
Nov 30 '18 at 14:08
add a comment |
Since your value 500 is a pixel value. So, I think you need to convert 500 to corresponded dp value. Try to use this:
private int dpToPx(int dp, Activity activity) {
Resources r = activity.getResources();
return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics()));
}
and change your code to this:
if (h > (1.3 * w)) {
imageView.setMaxHeight(dpToPx(500, this));
}
Tried. Didn't work.I think something more needs to be done for setMaxHeight() to work programatically. Maybe it doesn't work after the view is rendered
– Pranav Mahajan
Nov 30 '18 at 14:08
add a comment |
Since your value 500 is a pixel value. So, I think you need to convert 500 to corresponded dp value. Try to use this:
private int dpToPx(int dp, Activity activity) {
Resources r = activity.getResources();
return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics()));
}
and change your code to this:
if (h > (1.3 * w)) {
imageView.setMaxHeight(dpToPx(500, this));
}
Since your value 500 is a pixel value. So, I think you need to convert 500 to corresponded dp value. Try to use this:
private int dpToPx(int dp, Activity activity) {
Resources r = activity.getResources();
return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics()));
}
and change your code to this:
if (h > (1.3 * w)) {
imageView.setMaxHeight(dpToPx(500, this));
}
answered Nov 22 '18 at 23:58
Sinan CeylanSinan Ceylan
42428
42428
Tried. Didn't work.I think something more needs to be done for setMaxHeight() to work programatically. Maybe it doesn't work after the view is rendered
– Pranav Mahajan
Nov 30 '18 at 14:08
add a comment |
Tried. Didn't work.I think something more needs to be done for setMaxHeight() to work programatically. Maybe it doesn't work after the view is rendered
– Pranav Mahajan
Nov 30 '18 at 14:08
Tried. Didn't work.I think something more needs to be done for setMaxHeight() to work programatically. Maybe it doesn't work after the view is rendered
– Pranav Mahajan
Nov 30 '18 at 14:08
Tried. Didn't work.I think something more needs to be done for setMaxHeight() to work programatically. Maybe it doesn't work after the view is rendered
– Pranav Mahajan
Nov 30 '18 at 14:08
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439127%2fandroid-imageview-change-maxheight-property-through-code%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown