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.
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);
Pragmatically you can set textview height like:
private TextView mTxtView;
int height = 50; //your textview height
mTxtView.getLayoutParams().height = height;
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);
In Kotlin with DP to pixels translation
changeTextHeight.setOnClickListener { view ->
// random height for testing
val randomHeightInDP = Random().nextFloat() * (50.0f - 10.0f) + 10
// set Height in pixels
hello.layoutParams.height = convertDpToPixel(randomHeightInDP, applicationContext)
//refresh layout
hello.requestLayout()
}
Convert DP to pixels, see this post:
fun convertDpToPixel(dp: Float, context: Context): Int {
return (dp * (context.resources.displayMetrics.densityDpi.toFloat() / DisplayMetrics.DENSITY_DEFAULT)).toInt()
}
the sample way for this if you want to use dimen
first, you should set size in dimen XML file.
<dimen name="text_height">50dp</dimen>
<dimen name="text_width">50dp</dimen>
and
text_l.getLayoutParams().height =
getResources().getDimensionPixelSize(R.dimen.text_height);
text_l.getLayoutParams().width =
getResources().getDimensionPixelSize(R.dimen.text_width);
Or
if you want to set just int (for example we wanna set 50dp height and 100dp width)
text_l.getLayoutParams().height = 50 * 3;
text_l.getLayoutParams().width= 100 * 3;
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
}
Related
Set i have a fragment CustomSwitchFragment, that i want to use as followed:
<fragment android:name="m6world.test_toggle_yes_no.CustomSwitchFragment"
android:id="#+id/custom_switch_fragment"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:orientation="horizontal" />
i don't want to have to input 80dp, i would like to be able to put wrap_content and the fragment will be set whatever width i used when created it.
Basically i don`t want to have to tell people who are gonna use my fragment, to set it to 80dp specifically.
I guess you can use something like this
Display display = getWindow().getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
WindowManager.LayoutParams params = getWindow().getAttributes();
params.width = width * 70 / 100;
params.height = height * 30 / 100;
getWindow().setAttributes(params);
getWindow().getAttributes().verticalMargin = 0.2F;
Actually I am using Relative layout for image view. This image view is for receiving images from the internet to the application. The thing I need is can I change the image size dynamically for a particular images (say jpeg) and another size for attachments(docx,pdf), etc.
I used if condition for images and another if for attachments. Now how to set image size for receiving images and another image size for receiving attachments. I need to do dynamically. Note I am using only one image view for both images and attachments.
<RelativeLayout>
<ImageView>
id=iv
width=wrap content
height=wrap content
</ImageView>
</RelativeLayout>
for class file
if("image/png"){
iv.setImageResource(R.drawable.abc);
or
code to get images from web
}
else if(mimetype("application/pdf")
{
iv.setImageResource(R.drawable.abc)
}
this is the model
Help me out!
/*
requestLayout()
Call this when something has changed which has
invalidated the layout of this view.
*/
imageView.requestLayout();
imageView.getLayoutParams().height = height;
imageView.getLayoutParams().width = width;
reference : https://android--code.blogspot.com/2015/08/android-imageview-set-width-and-height.html
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams)imageView.getLayoutParams;
lp.width = x;
lp.height = y;
imageView.setLayoutParams(lp)
try this,
RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams(width,height);
ImageView ivImage=(ImageView)findViewById(R.id.image);
ivImage.setLayoutParams(params);
Try this code to change image aspect ratio dynamically,
private void setPicDimensions() {
Point screenDimensions = Utils.getScreenDimensions();
width = screenDimensions.x;
height = Utils.getHeightFromAspectRatio(width, 16, 9);// dynamically set aspect ratio value here its 16:9
}
// in your image view set layout params dynamically whereever you need to change
//Declare height and width as int and iv_pic as imageview
LayoutParams layoutParams = iv_offer_pic.getLayoutParams();
layoutParams.height = height;
layoutParams.width = width;
iv_pic.setLayoutParams(layoutParams);
most important is:
<imageview android:scaleType="fitXY" />
<RelativeLayout>
<ImageView>
id=iv
android:scaleType="fitXY"
width=wrap content
height=wrap content
</ImageView>
</RelativeLayout>
for class file
if("image/png"){
iv.setImageResource(R.drawable.abc);
or
code to get images from web
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams)imageView.getLayoutParams;
lp.width = 100;
lp.height = 100;
imageView.setLayoutParams(lp)
}
else if(mimetype("application/pdf")
{RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams)imageView.getLayoutParams;
lp.width = 50;
lp.height = 50;
imageView.setLayoutParams(lp)
iv.setImageResource(R.drawable.abc)
}
this is the model
Help me out!
I want to see the sum of textview's height and of textview's padding.
I set padding with method:
textView.setPadding(0,x,0,x);
But if I use
textView.getHeight();
I will receive 0.
If I use
ViewGroup.LayoutParams params = (ViewGroup.LayoutParams) textView.getLayoutParams();
Log.d(TAG,params.height+"");
I will receive -2 that's impossible.
What have I to do?
Edit: This is the XML code
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/try"
android:textColor="#fff"
android:id="#+id/tv"
android:gravity="center_horizontal" />
Edit2: I use this code for setting width of text view and it work correctely.
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
final int width = size.x;
final int height = size.y;
ViewGroup.LayoutParams params = (ViewGroup.LayoutParams) textView.getLayoutParams();
params.width = width/2;
I don't want to set the height programmately because I set a padding. But I want to get the measure
Edit3:
I try to use
Rect bounds = new Rect();
textView.getPaint().getTextBounds(textView.getText(), 0,textView.getText().length(), bounds);
bounds.height(); //This should give you the height of the wrapped_content
as Slartibartfast told me. But there is a problem:
Probably this is the measurement of text size... But it isn't the correct measurement of height of textview. I tried to sum this number and the 2 measure of padding and try to set:
params.height = bounds.height() + 2*x;
The view is smaller than the view without this line of code.
Take a look at : How to retrieve the dimensions of a view?
Basically the size is known only after the layout is finished, one way is to create a custom text view and use onSizeChanged method
EDIT:
How about
Rect bounds = new Rect();
textView.getPaint().getTextBounds(textView.getText(), 0,textView.getText().length(), bounds);
bounds.height(); //This should give you the height of the wrapped_content
I try change layout height programmatically. I have two buttons. In one button I change my layout position and second button I try to receive save position witch I was first time.
<RelativeLayout
android:id="#+id/popaplayout"
android:layout_width="fill_parent"
android:layout_height="200dp" >
This is a my layout
and in first button I wrote
RelativeLayout.LayoutParams parms = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
parms.addRule(RelativeLayout.BELOW, R.id.movies_title_layout);
scrollpopap.setLayoutParams(parms);
scrollpopap.setScrollingEnabled(true);
and second button
RelativeLayout.LayoutParams rel_btn = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, 300);
rel_btn.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
scrollpopap.setLayoutParams(rel_btn);
scrollpopap.setScrollingEnabled(false);
In xml file I wrote height 200dp and programmatically 300 and there are different height. How I can wrote save height in programmatically?
if anyone knows solution please help me
thanks
First convert 200 dp into pixels -
final float scale = getContext().getResources().getDisplayMetrics().density;
int pixels = (int) (200 * scale + 0.5f);
Then set programatically,
RelativeLayout.LayoutParams rel_btn = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, pixels);
rel_btn.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
scrollpopap.setLayoutParams(rel_btn);
scrollpopap.setScrollingEnabled(false);
Convert DP to PX like this.
final float scale = getContext().getResources().getDisplayMetrics().density;
int px = (int) (100 * scale + 0.5f); // replace 100 with your dimensions
Set Some layout_gravity of relative layout. and Change height and width by java code.
RelativeLayout rl = (RelativeLayout) findViewById(R.id.yourId);
rl.getLayoutParams().height = px;
rl.getLayoutParams().width = px;
If you intending to change RelativeLayout height, then this help.
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.yourId);
relativeLayout.getLayoutParams().height = 100;
relativeLayout.getLayoutParams().width = 100;
Use this
RelativeLayout layout = (RelativeLayout) findViewById(R.id.yourlayout_id);
// Gets the layout params that will allow you to resize the layout
LayoutParams params = layout.getLayoutParams();
params.height = 320;
params.width = 320;
For Kotlin Users who want to set Height & Width from Dimens file, use this:
relativeLayout.layoutParams.width = relativeLayout.context
.resources.getDimensionPixelSize(R.dimen.width)
relativeLayout.layoutParams.height = relativeLayout.context
.resources.getDimensionPixelSize(R.dimen.height)
How can I get and set the size of ImageButton programmatically?
Android seems to make button size bigger than image size.
Maybe I am wrong.
I want to make it the same size as image size.
try to set padding to 0dp in xml layout:
<ImageButton ...
android:padding="0dp" />
The image should be as big as button.
If you want to make button (but not an image) transparent set button background color to #null:
<ImageButton ...
android:background="#null" />
int height = button.getLayoutParams().height;
int width = button.getLayoutParams().width;
button.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT));
To programmatically get the height and width:
ViewGroup.LayoutParams layoutParams = mImageButton.getLayoutParams();
int height = layoutParams.height;
int width = layoutParams.width;
To programmatically set the height and width:
int myHeight = ContextCompat.getDrawable(getContext(), R.drawable.myImageAsset).getIntrinsicHeight();
/*any desired height, but for OP's sake ^^*/
int myWidth = ContextCompat.getDrawable(getContext(), R.drawable.myImageAsset).getIntrinsicWidth();
/*any desired width, but for OP's sake ^^*/
ViewGroup.LayoutParams layoutParams = mImageButton.getLayoutParams();
layoutParams.height = myHeight;
layoutParams.width = myWidth;
mImageButton.setLayoutParams(layoutParams);