re-setting a TextView height programmatically
I want to reset a textView height after I have added it to the main window in the xml file.
inside a RelativeLayout,
<TextView
android:id="@+id/text_l"
android:layout_width="50sp"
android:layout_height="50sp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginLeft="10sp"
android:layout_marginTop="145dp"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000" >
</TextView>
I just want to change it from 50 to 70:
I tried:
TextView text = (TextView)findViewById(R.id.text_l);
text.setHeight(70);
but nothing changed.

add a comment |
I want to reset a textView height after I have added it to the main window in the xml file.
inside a RelativeLayout,
<TextView
android:id="@+id/text_l"
android:layout_width="50sp"
android:layout_height="50sp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginLeft="10sp"
android:layout_marginTop="145dp"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000" >
</TextView>
I just want to change it from 50 to 70:
I tried:
TextView text = (TextView)findViewById(R.id.text_l);
text.setHeight(70);
but nothing changed.

Try this: stackoverflow.com/questions/3522224/…
– Nitesh Khosla
Feb 7 '12 at 11:33
add a comment |
I want to reset a textView height after I have added it to the main window in the xml file.
inside a RelativeLayout,
<TextView
android:id="@+id/text_l"
android:layout_width="50sp"
android:layout_height="50sp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginLeft="10sp"
android:layout_marginTop="145dp"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000" >
</TextView>
I just want to change it from 50 to 70:
I tried:
TextView text = (TextView)findViewById(R.id.text_l);
text.setHeight(70);
but nothing changed.

I want to reset a textView height after I have added it to the main window in the xml file.
inside a RelativeLayout,
<TextView
android:id="@+id/text_l"
android:layout_width="50sp"
android:layout_height="50sp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginLeft="10sp"
android:layout_marginTop="145dp"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000" >
</TextView>
I just want to change it from 50 to 70:
I tried:
TextView text = (TextView)findViewById(R.id.text_l);
text.setHeight(70);
but nothing changed.


edited Feb 7 '12 at 13:02


Seshu Vinay
10.1k751102
10.1k751102
asked Feb 7 '12 at 11:25
Q8yDevQ8yDev
170127
170127
Try this: stackoverflow.com/questions/3522224/…
– Nitesh Khosla
Feb 7 '12 at 11:33
add a comment |
Try this: stackoverflow.com/questions/3522224/…
– Nitesh Khosla
Feb 7 '12 at 11:33
Try this: stackoverflow.com/questions/3522224/…
– Nitesh Khosla
Feb 7 '12 at 11:33
Try this: stackoverflow.com/questions/3522224/…
– Nitesh Khosla
Feb 7 '12 at 11:33
add a comment |
4 Answers
4
active
oldest
votes
You should change it via LayoutParams
:
LayoutParams params = (LayoutParams) textView.getLayoutParams();
params.height = 70;
textView.setLayoutParams(params);
EDIT
You should not use sizes in pixels in you code, use dimensions for this:
dimens.xml:
<dimen name="text_view_height">50dp</dimen>
In code:
params.height = getResources().getDimensionPixelSize(R.dimen.text_view_height);
1
Thanks man, that actually worked :D
– Q8yDev
Feb 7 '12 at 22:06
The bizare thing here is: textview.setheight(...) did work on some of my test devices, even though in contrast to the documentation of the setHeight function of TextView it does NOT change the layout parameters of the view. But on one of my test devices (Xperia ray, Android 4.0.4) this worked only in landscape but not in portrait mode. Changing the height in the layout params as you suggested worked fine on all of my test devices.
– Nantoka
Jun 17 '13 at 23:48
It didn't work for me. Im afraid it doesn't work in newer APIs. Once the TextView has been layered, setting height in the TextView has no effect.
– cesards
Mar 8 '17 at 12:01
Note that TextView textView = new TextView(context); textView.getLayoutParame() weill be null. You should judge null before use it.
– BertKing
Nov 16 '17 at 11:21
add a comment |
Pragmatically you can set textview height like:
private TextView mTxtView;
int height = 50; //your textview height
mTxtView.getLayoutParams().height = height;
3
I prefer to do this, rather than finding the correct Layoutparams class
– Roisgoen
Dec 15 '15 at 23:44
that's true @Roisgoen
– Dhruv Raval
Dec 16 '15 at 4:48
What calls for special attention is that if you use the textView to getLayoutParams(), it would be better to judge null?
– BertKing
Nov 16 '17 at 10:52
add a comment |
you can dynamically set width and height of textview by
private TextView mTxtView;
private int screenWidth, screenHeight;
Display display = getWindowManager().getDefaultDisplay();
screenWidth = display.getWidth();
screenHeight = display.getHeight();
LayoutParams params = mTxtView.getLayoutParams();
params.width = screenWidth-30;
mTxtView.setLayoutParams(params);
add a comment |
I know this is an old question, but for the sake of others who might find this, there are scenarios where you should call textView.requestLayout() after changing the layout parameters. While it my be fine to omit it if you are simply changing the layout parameters as a one time thing before the layout is drawn. In my case, I wanted to change the height parameter of a TextView based on a radio button selection using onCheckedChangedListener, but the TextView height would only update the first time it was drawn. Adding requestLayout() solved this problem.
TextView tv;
ViewGroup.LayoutParams params = tv.getLayoutParams();
params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
if(!tv.isInLayout()) {//make sure it isn't currently in a layout pass
tv.requestLayout();//request a layout pass
}
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%2f9175391%2fre-setting-a-textview-height-programmatically%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
You should change it via LayoutParams
:
LayoutParams params = (LayoutParams) textView.getLayoutParams();
params.height = 70;
textView.setLayoutParams(params);
EDIT
You should not use sizes in pixels in you code, use dimensions for this:
dimens.xml:
<dimen name="text_view_height">50dp</dimen>
In code:
params.height = getResources().getDimensionPixelSize(R.dimen.text_view_height);
1
Thanks man, that actually worked :D
– Q8yDev
Feb 7 '12 at 22:06
The bizare thing here is: textview.setheight(...) did work on some of my test devices, even though in contrast to the documentation of the setHeight function of TextView it does NOT change the layout parameters of the view. But on one of my test devices (Xperia ray, Android 4.0.4) this worked only in landscape but not in portrait mode. Changing the height in the layout params as you suggested worked fine on all of my test devices.
– Nantoka
Jun 17 '13 at 23:48
It didn't work for me. Im afraid it doesn't work in newer APIs. Once the TextView has been layered, setting height in the TextView has no effect.
– cesards
Mar 8 '17 at 12:01
Note that TextView textView = new TextView(context); textView.getLayoutParame() weill be null. You should judge null before use it.
– BertKing
Nov 16 '17 at 11:21
add a comment |
You should change it via LayoutParams
:
LayoutParams params = (LayoutParams) textView.getLayoutParams();
params.height = 70;
textView.setLayoutParams(params);
EDIT
You should not use sizes in pixels in you code, use dimensions for this:
dimens.xml:
<dimen name="text_view_height">50dp</dimen>
In code:
params.height = getResources().getDimensionPixelSize(R.dimen.text_view_height);
1
Thanks man, that actually worked :D
– Q8yDev
Feb 7 '12 at 22:06
The bizare thing here is: textview.setheight(...) did work on some of my test devices, even though in contrast to the documentation of the setHeight function of TextView it does NOT change the layout parameters of the view. But on one of my test devices (Xperia ray, Android 4.0.4) this worked only in landscape but not in portrait mode. Changing the height in the layout params as you suggested worked fine on all of my test devices.
– Nantoka
Jun 17 '13 at 23:48
It didn't work for me. Im afraid it doesn't work in newer APIs. Once the TextView has been layered, setting height in the TextView has no effect.
– cesards
Mar 8 '17 at 12:01
Note that TextView textView = new TextView(context); textView.getLayoutParame() weill be null. You should judge null before use it.
– BertKing
Nov 16 '17 at 11:21
add a comment |
You should change it via LayoutParams
:
LayoutParams params = (LayoutParams) textView.getLayoutParams();
params.height = 70;
textView.setLayoutParams(params);
EDIT
You should not use sizes in pixels in you code, use dimensions for this:
dimens.xml:
<dimen name="text_view_height">50dp</dimen>
In code:
params.height = getResources().getDimensionPixelSize(R.dimen.text_view_height);
You should change it via LayoutParams
:
LayoutParams params = (LayoutParams) textView.getLayoutParams();
params.height = 70;
textView.setLayoutParams(params);
EDIT
You should not use sizes in pixels in you code, use dimensions for this:
dimens.xml:
<dimen name="text_view_height">50dp</dimen>
In code:
params.height = getResources().getDimensionPixelSize(R.dimen.text_view_height);
edited Apr 18 '17 at 11:34
SpyZip
3,81241946
3,81241946
answered Feb 7 '12 at 11:28
Jin35Jin35
8,04622751
8,04622751
1
Thanks man, that actually worked :D
– Q8yDev
Feb 7 '12 at 22:06
The bizare thing here is: textview.setheight(...) did work on some of my test devices, even though in contrast to the documentation of the setHeight function of TextView it does NOT change the layout parameters of the view. But on one of my test devices (Xperia ray, Android 4.0.4) this worked only in landscape but not in portrait mode. Changing the height in the layout params as you suggested worked fine on all of my test devices.
– Nantoka
Jun 17 '13 at 23:48
It didn't work for me. Im afraid it doesn't work in newer APIs. Once the TextView has been layered, setting height in the TextView has no effect.
– cesards
Mar 8 '17 at 12:01
Note that TextView textView = new TextView(context); textView.getLayoutParame() weill be null. You should judge null before use it.
– BertKing
Nov 16 '17 at 11:21
add a comment |
1
Thanks man, that actually worked :D
– Q8yDev
Feb 7 '12 at 22:06
The bizare thing here is: textview.setheight(...) did work on some of my test devices, even though in contrast to the documentation of the setHeight function of TextView it does NOT change the layout parameters of the view. But on one of my test devices (Xperia ray, Android 4.0.4) this worked only in landscape but not in portrait mode. Changing the height in the layout params as you suggested worked fine on all of my test devices.
– Nantoka
Jun 17 '13 at 23:48
It didn't work for me. Im afraid it doesn't work in newer APIs. Once the TextView has been layered, setting height in the TextView has no effect.
– cesards
Mar 8 '17 at 12:01
Note that TextView textView = new TextView(context); textView.getLayoutParame() weill be null. You should judge null before use it.
– BertKing
Nov 16 '17 at 11:21
1
1
Thanks man, that actually worked :D
– Q8yDev
Feb 7 '12 at 22:06
Thanks man, that actually worked :D
– Q8yDev
Feb 7 '12 at 22:06
The bizare thing here is: textview.setheight(...) did work on some of my test devices, even though in contrast to the documentation of the setHeight function of TextView it does NOT change the layout parameters of the view. But on one of my test devices (Xperia ray, Android 4.0.4) this worked only in landscape but not in portrait mode. Changing the height in the layout params as you suggested worked fine on all of my test devices.
– Nantoka
Jun 17 '13 at 23:48
The bizare thing here is: textview.setheight(...) did work on some of my test devices, even though in contrast to the documentation of the setHeight function of TextView it does NOT change the layout parameters of the view. But on one of my test devices (Xperia ray, Android 4.0.4) this worked only in landscape but not in portrait mode. Changing the height in the layout params as you suggested worked fine on all of my test devices.
– Nantoka
Jun 17 '13 at 23:48
It didn't work for me. Im afraid it doesn't work in newer APIs. Once the TextView has been layered, setting height in the TextView has no effect.
– cesards
Mar 8 '17 at 12:01
It didn't work for me. Im afraid it doesn't work in newer APIs. Once the TextView has been layered, setting height in the TextView has no effect.
– cesards
Mar 8 '17 at 12:01
Note that TextView textView = new TextView(context); textView.getLayoutParame() weill be null. You should judge null before use it.
– BertKing
Nov 16 '17 at 11:21
Note that TextView textView = new TextView(context); textView.getLayoutParame() weill be null. You should judge null before use it.
– BertKing
Nov 16 '17 at 11:21
add a comment |
Pragmatically you can set textview height like:
private TextView mTxtView;
int height = 50; //your textview height
mTxtView.getLayoutParams().height = height;
3
I prefer to do this, rather than finding the correct Layoutparams class
– Roisgoen
Dec 15 '15 at 23:44
that's true @Roisgoen
– Dhruv Raval
Dec 16 '15 at 4:48
What calls for special attention is that if you use the textView to getLayoutParams(), it would be better to judge null?
– BertKing
Nov 16 '17 at 10:52
add a comment |
Pragmatically you can set textview height like:
private TextView mTxtView;
int height = 50; //your textview height
mTxtView.getLayoutParams().height = height;
3
I prefer to do this, rather than finding the correct Layoutparams class
– Roisgoen
Dec 15 '15 at 23:44
that's true @Roisgoen
– Dhruv Raval
Dec 16 '15 at 4:48
What calls for special attention is that if you use the textView to getLayoutParams(), it would be better to judge null?
– BertKing
Nov 16 '17 at 10:52
add a comment |
Pragmatically you can set textview height like:
private TextView mTxtView;
int height = 50; //your textview height
mTxtView.getLayoutParams().height = height;
Pragmatically you can set textview height like:
private TextView mTxtView;
int height = 50; //your textview height
mTxtView.getLayoutParams().height = height;
edited Sep 8 '15 at 6:36


phi
3,76312838
3,76312838
answered Aug 4 '15 at 10:38


Dhruv RavalDhruv Raval
3,8272329
3,8272329
3
I prefer to do this, rather than finding the correct Layoutparams class
– Roisgoen
Dec 15 '15 at 23:44
that's true @Roisgoen
– Dhruv Raval
Dec 16 '15 at 4:48
What calls for special attention is that if you use the textView to getLayoutParams(), it would be better to judge null?
– BertKing
Nov 16 '17 at 10:52
add a comment |
3
I prefer to do this, rather than finding the correct Layoutparams class
– Roisgoen
Dec 15 '15 at 23:44
that's true @Roisgoen
– Dhruv Raval
Dec 16 '15 at 4:48
What calls for special attention is that if you use the textView to getLayoutParams(), it would be better to judge null?
– BertKing
Nov 16 '17 at 10:52
3
3
I prefer to do this, rather than finding the correct Layoutparams class
– Roisgoen
Dec 15 '15 at 23:44
I prefer to do this, rather than finding the correct Layoutparams class
– Roisgoen
Dec 15 '15 at 23:44
that's true @Roisgoen
– Dhruv Raval
Dec 16 '15 at 4:48
that's true @Roisgoen
– Dhruv Raval
Dec 16 '15 at 4:48
What calls for special attention is that if you use the textView to getLayoutParams(), it would be better to judge null?
– BertKing
Nov 16 '17 at 10:52
What calls for special attention is that if you use the textView to getLayoutParams(), it would be better to judge null?
– BertKing
Nov 16 '17 at 10:52
add a comment |
you can dynamically set width and height of textview by
private TextView mTxtView;
private int screenWidth, screenHeight;
Display display = getWindowManager().getDefaultDisplay();
screenWidth = display.getWidth();
screenHeight = display.getHeight();
LayoutParams params = mTxtView.getLayoutParams();
params.width = screenWidth-30;
mTxtView.setLayoutParams(params);
add a comment |
you can dynamically set width and height of textview by
private TextView mTxtView;
private int screenWidth, screenHeight;
Display display = getWindowManager().getDefaultDisplay();
screenWidth = display.getWidth();
screenHeight = display.getHeight();
LayoutParams params = mTxtView.getLayoutParams();
params.width = screenWidth-30;
mTxtView.setLayoutParams(params);
add a comment |
you can dynamically set width and height of textview by
private TextView mTxtView;
private int screenWidth, screenHeight;
Display display = getWindowManager().getDefaultDisplay();
screenWidth = display.getWidth();
screenHeight = display.getHeight();
LayoutParams params = mTxtView.getLayoutParams();
params.width = screenWidth-30;
mTxtView.setLayoutParams(params);
you can dynamically set width and height of textview by
private TextView mTxtView;
private int screenWidth, screenHeight;
Display display = getWindowManager().getDefaultDisplay();
screenWidth = display.getWidth();
screenHeight = display.getHeight();
LayoutParams params = mTxtView.getLayoutParams();
params.width = screenWidth-30;
mTxtView.setLayoutParams(params);
answered Sep 17 '13 at 10:55


Rajeev KashyapRajeev Kashyap
413
413
add a comment |
add a comment |
I know this is an old question, but for the sake of others who might find this, there are scenarios where you should call textView.requestLayout() after changing the layout parameters. While it my be fine to omit it if you are simply changing the layout parameters as a one time thing before the layout is drawn. In my case, I wanted to change the height parameter of a TextView based on a radio button selection using onCheckedChangedListener, but the TextView height would only update the first time it was drawn. Adding requestLayout() solved this problem.
TextView tv;
ViewGroup.LayoutParams params = tv.getLayoutParams();
params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
if(!tv.isInLayout()) {//make sure it isn't currently in a layout pass
tv.requestLayout();//request a layout pass
}
add a comment |
I know this is an old question, but for the sake of others who might find this, there are scenarios where you should call textView.requestLayout() after changing the layout parameters. While it my be fine to omit it if you are simply changing the layout parameters as a one time thing before the layout is drawn. In my case, I wanted to change the height parameter of a TextView based on a radio button selection using onCheckedChangedListener, but the TextView height would only update the first time it was drawn. Adding requestLayout() solved this problem.
TextView tv;
ViewGroup.LayoutParams params = tv.getLayoutParams();
params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
if(!tv.isInLayout()) {//make sure it isn't currently in a layout pass
tv.requestLayout();//request a layout pass
}
add a comment |
I know this is an old question, but for the sake of others who might find this, there are scenarios where you should call textView.requestLayout() after changing the layout parameters. While it my be fine to omit it if you are simply changing the layout parameters as a one time thing before the layout is drawn. In my case, I wanted to change the height parameter of a TextView based on a radio button selection using onCheckedChangedListener, but the TextView height would only update the first time it was drawn. Adding requestLayout() solved this problem.
TextView tv;
ViewGroup.LayoutParams params = tv.getLayoutParams();
params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
if(!tv.isInLayout()) {//make sure it isn't currently in a layout pass
tv.requestLayout();//request a layout pass
}
I know this is an old question, but for the sake of others who might find this, there are scenarios where you should call textView.requestLayout() after changing the layout parameters. While it my be fine to omit it if you are simply changing the layout parameters as a one time thing before the layout is drawn. In my case, I wanted to change the height parameter of a TextView based on a radio button selection using onCheckedChangedListener, but the TextView height would only update the first time it was drawn. Adding requestLayout() solved this problem.
TextView tv;
ViewGroup.LayoutParams params = tv.getLayoutParams();
params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
if(!tv.isInLayout()) {//make sure it isn't currently in a layout pass
tv.requestLayout();//request a layout pass
}
answered Jan 1 at 5:18


smitty1smitty1
394518
394518
add a comment |
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%2f9175391%2fre-setting-a-textview-height-programmatically%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
Try this: stackoverflow.com/questions/3522224/…
– Nitesh Khosla
Feb 7 '12 at 11:33