mainLayout = (LinearLayout) findViewById(R.id.linearLayout);
mChart = new HorizontalBarChart(this);
mChart.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
mainLayout.addView(mChart);
I like to change the width and height to dp units like 100 dp or 200 dp,. the setLayoutParams doesn't take number units , the choices are only (wrap_content and match_content).. I'm new to Android so im confused how to change it.
Another ways is you add your dimension in dimens.xml.
For example, add <dimen name="chart_width">100dp</dimen>.
Then, at your code:
float width = getResources().getDimension(R.dimen.chart_width);
mChart.setLayoutParams(new ViewGroup.LayoutParams(
width,
ViewGroup.LayoutParams.WRAP_CONTENT));
Converting dip values into pixels will let your layout build correctly, this line of code will solve it:
int width = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
getResources().getDimension(R.dimen.desired_width),
getResources().getDisplayMetrics()
);
I think we can't set dp directly to the view. So we've to convert the dimension from pixel to dp.
To get pixel from dp, you can use TypedValue#applyDimension() method.
Resources r = getResources();
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, {sizeInDp}, r.getDisplayMetrics());
So the final code will be
float width = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, {widthInDp}, r.getDisplayMetrics());
float height = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, {heightInDp}, r.getDisplayMetrics());
mChart.setLayoutParams(new ViewGroup.LayoutParams(
width,
height));
int dp1 = dip2pix(getContext(), 1);
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
dp1 * 100, ViewGroup.LayoutParams.WRAP_CONTENT);
public static int dip2pix(#NonNull Context context, int dip) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip,
context.getResources().getDisplayMetrics());
}
Give it a try it will work for sure
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
200, 200);
params.setMargins(5, 0, 5, 0);
mChart= new HorizontalBarChart(getApplicationContext());
mChart.setLayoutParams(params);
mainLayout.addView(mChart);
Related
I have a LinearLayout which is made programmatically like this:
listLinear=new LinearLayout(this);
LinearLayout.LayoutParams listParam = new LinearLayout.LayoutParams(80,
ViewGroup.LayoutParams.MATCH_PARENT);
listLinear.setLayoutParams(listParam);
80 is my listLinear width. I want my LinearLayout width to be 80. My LinearLayout looks perfectly as I want in 4.5 inch screen but when I test it in 5 inch display it just shrinks and looks small in size.
Why is it happening?
The problem is the constructor of LinearLayout.LayoutParams expects the width/height in pixels.
For your layout to look the same on all screen sizes, you should define its width in dip (density-independent pixels).
Something like this should work:
listLinear = new LinearLayout(this);
int widthDp = 50; // desired width in dp, adjust this as you'd like
// converting dp to pixels
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
int widthPx = Math.round(
widthDp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
LinearLayout.LayoutParams listParam = new LinearLayout.LayoutParams(widthPx,
ViewGroup.LayoutParams.MATCH_PARENT);
listLinear.setLayoutParams(listParam);
after setcontentview
use this code
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
getWindow().setLayout((int) (width), (int) (height * .2));
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)
I would like to set height and width in dp for ImageView in android pragmatically.
How can I achieve this?
Set width & height with dp :
imageview.getLayoutParams().height = (int) getResources().getDimension(R.dimen.imageview_height);
imageview.getLayoutParams().width = (int) getResources().getDimension(R.dimen.imageview_width);
In your dimens.xml provide values for the keys :
<dimen name="imageview_width">50dp</dimen>
<dimen name="imageview_height">50dp</dimen>
Use display metrics to get the sale factor and then just some simple maths - example, if I want 200x150dp:
final float scale = getResources().getDisplayMetrics().density;
int dpWidthInPx = (int) (200 * scale);
int dpHeightInPx = (int) (150 * scale);
Then set the image view size:
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(dpWidthInPx, dpHeightInPx);
imageView.setLayoutParams(layoutParams);
This may be simpler and should do the trick:
ImageView im = (ImageView)findViewById(R.id.image1);
LayoutParams params = im.getLayoutParams();
params.height = getActivity().getResources().getDimensionPixelSize(R.dimen.item_height);
params.width = getActivity().getResources().getDimensionPixelSize(R.dimen.item_width);
And in your dimens.xml
<dimen name="item_height">80dp</dimen>
<dimen name="item_width">80dp</dimen>
This may help you...
ImageView im = (ImageView)findViewById(R.id.image1);
LayoutParams params = im.getLayoutParams();
params.height = 100;
params.width = 100;
at first choose a desirable dip and assign it to a var.
int dipAmount=350;
Then read the height of your ImageView.
float scale = imageview.Resource.DisplayMetrics.Density;
Convert from px to dip.
int converter =(int) (350 * scale + 0.5f);
Set the height to imageview.
imageView.LayoutParameters.Height=converter;
final float scale = getContext().getResources().getDisplayMetrics().density;
int height_ = (int) (250 * scale + 0.5f);
int width_ = (int) (250 * scale + 0.5f);
250 is in dp
ViewGroup.LayoutParams params = ImageView.getLayoutParams();
params.height = height_;
params.width = width_;
ImageView.setLayoutParams(params);
Try this:
image_view.getLayoutParams().height = 20;
image_view.getLayoutParams().width= 20;
Give me your feedback if it's acceptable. It's work for me.
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);
How do I specify that an int parameter is in dip? Specifically, how would write the equivalent of:
android:layout_width="50dip"
android:layout_height="50dip"
Something like...:
LayoutParams params = new LayoutParams(50, 50)
There is a built-in method that will do this too: TypedValue.applyDimension.
// Convert from 50dip to actual pixels
final int width = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics());
final int height = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics());
LayoutParams params = new LayoutParams(width, height);
You can use this to convert from sp units to pixels as well.