android RelativeLayout change height programmatically - android

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)

Related

I programmatically created a TextView in TableLayout, it automatically sets the height and width

I programmatically created a TextView in TableLayout, it automatically sets the height and width.
Can I change the height and width of the TextView manually inside the TableLayout?
Use
TableLayout.LayoutParams params = new TableLayout.LayoutParams();
params.height = HEIGHT;
params.width = WIDTH;
textView.setLayoutParams(params);
same for other parameters like margin, padding etc
Use
LayoutParams lParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 1);
lParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);
You can set the height and width programmatically using the following code.
TableRow.LayoutParams param = new TableRow.LayoutParams();
paramColumn1.height = TableRow.LayoutParams.WRAP_CONTENT;
paramColumn1.width = TableRow.LayoutParams.WRAP_CONTENT;
textView1.setLayoutParams(param);
You can also set other properties using this way.

How to programmatically set the width of the LinearLayout?

Is the source of the slider and the lesson link:
http://www.oodlestechnologies.com/blogs/Facebook-Style-Slide-Menu-In-Android
In this case, the width of the file "left_menu.xml" determined TextView (android:layout_width="260dp")
How do I set the width to "LinearLayout" file "left_menu.xml" depending on the device screen? For example, I want the width of the "LinearLayout" was always 1/3 of the screen device? Or any way to set the width of the TextView 1/3 of the width of the device screen.
To set the LinearLayout or TextView width to 1/3 of the device screen:
first of all get the device screen width:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
try {
display.getRealSize(size);
} catch (NoSuchMethodError err) {
display.getSize(size);
}
int width = size.x;
int height = size.y;
now just create a LayoutParams and set it to the Text:
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams((int)(width/3),
LinearLayout.LayoutParams.WRAP_CONTENT); // or set height to any fixed value you want
your_layout.setLayoutParams(lp);
// OR
your_textView.setLayoutParams(lp);
LinearLayout linear = (LinearLayout) findViewById(R.id.ll);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
linear.setLayoutParams(params);
As you can see, you can set Integer values for the LinearLayout.LayoutParams() constructor, like this:
LinearLayout.LayoutParams cellParams = new LinearLayout.LayoutParams(0, 100);
The costructor wants pixels and not dp(Density Pixels), here's a formula to convert PXs from DPs:
(int) (<numberOfDPs> * getContext().getResources().getDisplayMetrics().density + 0.5f)
LinearLayout layout = (LinearLayout)findViewById(R.id.ll);
LayoutParams params = (LayoutParams) layout.getLayoutParams();
params.height = 100;
params.width = 100;
The above answers are correct. I'll just suggest future readers to take a look at their xml layout file and be sure that the LinearLayout element is not using weight attribute. If so, consider updating weight in your LayoutParams object.
Another good practice is to retrieve the layout params object from the view you're trying to adjust, instead of creating one from scratch and having to set all the values.
LinearLayout YOUR_LinearLayout =(LinearLayout)findViewById(R.id.YOUR_LinearLayout)
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
/*width*/ ViewGroup.LayoutParams.MATCH_PARENT,
/*height*/ 100,
/*weight*/ 1.0f
);
YOUR_LinearLayout.setLayoutParams(param);
The easy way to do that, set value of layoutParams, and please notice this size is not in DP.
view.layoutParams.width = 400;
view.layoutParams.height = 150;
view.requestLayout();

How to set Imageview height and width in activity android

Hi I wanted to change height and width property of image view inside my activity
I tried it in following way but its not working for me ...
View card_view = getLayoutInflater().inflate(R.layout.card_details1,null);
coupon_img = (ImageView) card_view.findViewById(R.id.coupon_image);
// I tried this ////////
coupon_img.getLayoutParams().height = 20;
coupon_img.getLayoutParams().width = 20;
// I also tried this ////
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(100, 100);
coupon_img.setLayoutParams(layoutParams);
// also this one ////
coupon_img.setMaxHeight(10);
But I am not able to change height and width of imageview src. Is there any mistake I am doing?
How to do this?
Need help...
Thank you...
In this piece of code, I am creating a new instance of an ImageView at runtime and setting dimensions to it:
// SET THE IMAGEVIEW DIMENSIONS
int dimens = 120;
float density = activity.getResources().getDisplayMetrics().density;
int finalDimens = (int)(dimens * density);
LinearLayout.LayoutParams imgvwDimens =
new LinearLayout.LayoutParams(finalDimens, finalDimens);
imgAlbumPhoto.setLayoutParams(imgvwDimens);
// SET SCALETYPE
imgAlbumPhoto.setScaleType(ScaleType.CENTER_CROP);
// SET THE MARGIN
int dimensMargin = 5;
float densityMargin = activity.getResources().getDisplayMetrics().density;
int finalDimensMargin = (int)(dimensMargin * densityMargin);
LinearLayout.LayoutParams imgvwMargin =
new LinearLayout.LayoutParams(finalDimens, finalDimens);
imgvwMargin.setMargins
(finalDimensMargin, finalDimensMargin, finalDimensMargin, finalDimensMargin);
This will set the dimensions of the ImageView. They will, however, be in px. Use the code from here if you need dp values: https://stackoverflow.com/a/9563438/450534
UPDATED:
To change the dimensions of an existing ImageView that has already been defined in the XML, use this:
coupon_img.setLayoutParams(new LayoutParams(100, 100));
try some thing like this...
LayoutParams params = new LayoutParams(100, 100);
parantlayout.addView(coupon_img, params);
I think this will help you.
I think you are not adding the changed image to the layout..
LinearLayout ll = (LinearLayout)findViewById(R.layout.yourlinearlayout);
image.setLayoutParams(
new LinearLayout.LayoutParams(
bmp.getWidth(),
bmp.getHeight()));
ll.addView(image);// Then add the image to linear layout

Android - How do I change the width of my ImageButton programmatically?

I have an ImageButton. The code is -
ImageButton shareButton = new ImageButton(this);
shareButton.setBackgroundResource(android.R.drawable.ic_menu_share);
RelativeLayout.LayoutParams shareParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
shareParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, navigationLogo.getId());
shareButton.setLayoutParams(shareParams);
I need to change its width. But there is no setWidth method in both ImageButton as well as the layout params. Looked a lot online without an answer.
Instead of using the RelativeLayout.WRAP_CONTENT for the width, you can use an actual number, which will be the new width of the button in pixels. Since you probably want to specify the width in dp to be resolution-independent, you will probably need to convert dp to pixels, with a method such as the following:
public static int dpToPixels(Context context, float dp) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dp * scale + 0.5f);
}
Try this.
ImageButton shareButton = new ImageButton(this);
shareButton.setBackgroundResource(android.R.drawable.ic_menu_share);
RelativeLayout.LayoutParams shareParams = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
shareParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, navigationLogo.getId());
shareParams.width = INTEGER_NUMBER;
shareButton.setLayoutParams(shareParams);

Android, programmatically layout a button view?

I am trying to programmatically define my program layout and add a button to it at a certain position. I am not using the layout xml as the content view.
RelativeLayout mainLayout;
mainLayout = new RelativeLayout(this);
mainLayout.setLayoutParams(new
LayoutParams(LayoutParams.FILL_PARENT,android.view.ViewGroup.LayoutParams.FILL_PARENT));
I have then added a button that I want to apply the properties
layout align center
parent align left
height 60px
width 60px
here is the button so far
Button BtnNext = new Button(this);
BtnNext.setWidth(60);
BtnNext.setHeight(60);
BtnNext.setFocusable(true);
BtnNext.setId(idBtnNext);
BtnNext.setText("Next");
mainLayout.addView(BtnNext, 1);
Height and width DON'T work correctly.
Hi you can try by setting layout params
RelativeLayout.LayoutParams rel_btn = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
rel_btn.height = 60;
rel_btn.width = 60;
BtnNext.setLayoutParams(rel_btn);
You can also add rules and set margins for Button by setting relative layout params like
rel_btn.addRule(RelativeLayout.CENTER_VERTICAL);
rel_btn.leftMargin = 220;
Height and width will not be as u wished because your are not using Density-independent pixel (dip)
The value you set here is in pixel
You can convert the pixel into dip by
final float scale = getResources().getDisplayMetrics().density;
int dip = (int) (60 * scale + 0.5f);
You will get more accurate result

Categories

Resources