Android View Layout_Width - How to Change Programmatically

Android view layout_width - how to change programmatically?

I believe your question is to change only width of view dynamically, whereas above methods will change layout properties completely to new one, so I suggest to getLayoutParams() from view first, then set width on layoutParams, and finally set layoutParams to the view, so following below steps to do the same.

View view = findViewById(R.id.nutrition_bar_filled);
LayoutParams layoutParams = view.getLayoutParams();
layoutParams.width = newWidth;
view.setLayoutParams(layoutParams);

How to programmatically set the width of the LinearLayout?

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);

Android - i can not set EditText layout width programmatically

As @Amin said, layout width was zero when using getWidth in the onCreate or onStart or onResume methods. I found the solution like that:

tw1.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
tw1.getViewTreeObserver().removeOnGlobalLayoutListener(this);
int theWidth = tw1.getWidth();
tw2.setWidth(theWidth);
}
});

This method is called when the view object's properties change. For this problem it is a good solution i think.

Set View Width Programmatically

This code let you fill the banner to the maximum width and keep the ratio.
This will only work in portrait. You must recreate the ad when you rotate the device.
In landscape you should just leave the ad as is because it will be quite big an blurred.

Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
double ratio = ((float) (width))/300.0;
int height = (int)(ratio*50);

AdView adView = new AdView(this,"ad_url","my_ad_key",true,true);
LinearLayout layout = (LinearLayout) findViewById(R.id.testing);
mAdView.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT,height));
adView.setAdListener(this);
layout.addView(adView);

Android: How to Programmatically set the size of a Layout

Java

This should work:

// Gets linearlayout
LinearLayout layout = findViewById(R.id.numberPadLayout);
// Gets the layout params that will allow you to resize the layout
LayoutParams params = layout.getLayoutParams();
// Changes the height and width to the specified *pixels*
params.height = 100;
params.width = 100;
layout.setLayoutParams(params);

If you want to convert dip to pixels, use this:

int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, <HEIGHT>, getResources().getDisplayMetrics());

Kotlin

Android set height and width of Custom view programmatically

If you know the exact size of the view, just use setLayoutParams():

graphView.setLayoutParams(new LayoutParams(width, height));

Or in Kotlin:

graphView.layoutParams = LayoutParams(width, height)

However, if you need a more flexible approach you can override onMeasure() to measure the view more precisely depending on the space available and layout constraints (wrap_content, match_parent, or a fixed size). You can find more details about onMeasure() in the android docs.

How can I set layout height and width programmatically which support all screen size in Android?

You can get the device density like this:

float scaledDensity = getResources().getDisplayMetrics().scaledDensity;
LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(20 * scaledDensity,20 * scaledDensity);
param1.weight=1;

change programmatically view propieties

Your comment about why this would be bad practice:

"...the user will see in 2 types of UI during his use of the app"

is incorrect.

The way UI layout works is that there is a main thread Looper that handles all tasks that are sent to it. Every time there is an event that affects the UI, a function call is sent to that Looper and it runs through to completion before anything is redrawn to the screen.

So, for instance, if a button is pressed, the OnClickListener for that button will be called on the main thread and run through to completion before anything is redrawn on the screen. It doesn't matter how many actions you take by the end of your listener's function. That's why you're not supposed to do any blocking (time-consuming) actions on the main thread like writing to a file. That would freeze the UI while the file is being written.

So even if you modify 100 different buttons' layout params and colors sequentially, the user will not see any changes until after the function returns.

The same is true for onCreate(). The user will not see anything from your Activity until after onCreate() returns.

If your calculator does some calculation that takes so long that you're afraid it will freeze the UI noticeably, correct practice would be to first lock the buttons (disable them or set some Boolean property that all their listeners check to determine if they should work), then show some progress indicator, then launch a background thread or coroutine to do the work, and then when the calculation is done, unlock everything and show the result.

Realistically, you don't need to do that for a simple calculator. It's not like you're manipulating a huge data set.

View sizes set in Kotlin must be done in integer pixel units. You can search for other questions on here about how to convert dp units into pixel units.



Related Topics



Leave a reply



Submit