I've read:
How to set margin of ImageView using code, not xml
and the comment to use "You can scale the px value using context.getResources().getDisplayMetrics().density "
Thing is, I have scale representing some values in colors, and need to point cursor to certain value.
As everything is counted in "dip"s how can I send dip grammatically, not px?
eg:
0____T_h_i_s__i_s___m_y___s_c_a_l_e_____________300dp
^ - My cursor pointing to 100dp (LeftMargin = 100dp)
I'm using this line of code to set it up:
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(leftMargin, 0, 0, 0);
BMIcursor.setLayoutParams(lp);
The left margin in pixels can be computed like this:
leftMarginPx = leftMarginDp * context.getResources().getDisplayMetrics().density;
So with your code:
leftMarginDp = 100 // 100dp;
leftMargin = leftMarginDp * context.getResources().getDisplayMetrics().density;
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(leftMargin, 0, 0, 0);
BMIcursor.setLayoutParams(lp);
Related
I want to set the width of a textView programmatically. I want the width to vary with the length of the text (value) of the textView. The text of the textView must not wrap. If the text of a textView is "OLA OMO ALARE", how can I use the length of this string (13) to derive the correct value to set lp.width to, in the code below ?
LayoutParams lp = tv.getLayoutParams();
lp.width =
tv.setLayoutParams(lp);
I tried the code below, but the width was not long enough, thus text was auto wrapped in textView.
lp.width = Math.round( tv.getPaint().measureText(tv.getText().toString())) )
Thanks.
1) You have to use Rect instance for this.
2) Then you need to use this in textView's getPaint() and getTextBound() methods which allows you to put your text of the textview.
3) At the end just get width from the Rect instance.
Code is here with all the steps implemented,
I tested this and it works fine. Hope it helps.
TextView textView = new TextView(this);
textView.setText("AALAP");
Rect rect = new Rect();
textView.getPaint().getTextBounds(textView.getText().toString(), 0, textView.getText().length(), rect);
Log.d(TAG, "onCreate: width: "+rect.width());
Use the ViewGroup.LayoutParams.WRAP_CONTENT constant for this.
ViewGroup.LayoutParams params = myTextView.getLayoutParams();
params.width = ViewGroup.LayoutParams.WRAP_CONTENT;
myTextView.setLayoutParams(params);
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);
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();
I am trying to create a button programmatically and add it to a LinearLayout.
I have the following code, and although all seems to work fine, I end up with a button with no text.
for (int i = 0; i < seed.dealer.size(); i++)
{
Button dealer = new Button(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, 40);
params.gravity= Gravity.CENTER;
params.setMargins(5, 5, 5, 5);
dealer.setLayoutParams(params);
dealer.setBackgroundResource(R.drawable.rounded_green);
dealer.setTypeface(Typeface.create("sans-serif-light", Typeface.BOLD));
dealer.setTextSize(22);
dealer.setTextColor(Color.parseColor("#F0F0F0"));
dealer.setText(seed.dealer.get(i).name);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
float logicalDensity = metrics.density;
int px = (int) (200 * logicalDensity + 0.5);
dealer.setMinWidth(px);
px = (int) (350 * logicalDensity + 0.5);
dealer.setMaxWidth(px);
LinearLayout ll = (LinearLayout) findViewById(R.id.dealerContainer);
ll.addView(dealer);
}
first check seed.dealer.get(i).name is giving a valid String (not null and not empty). if it is giving a valid String, I think the problem is with text color of a button (Color.parseColor("#F0F0F0") is giving color something like transparent(exactly I have not tested)). so try changing text color to other color like Color.BLACK or Color.RED something like...
The problem was with this line :
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, 40);
I had to convert height to dp because the button height was so small that made the it look like there was no text.
I have encountered a problem when i try to give a negative left margin to a LinearLayout.
The negative margin does not appear.
Here is my code
HorizontalScrollView hview = new HorizontalScrollView(context); // HorizontalScrollView is the outer view
RelativeLayout.LayoutParams hs_lot_params = new RelativeLayout.LayoutParams(164, 164);
hs_lot_params.setMargins(100, 100, 0, 0); // set the positions
ImageView image = new ImageView(context);
image.setBackgroundResource(R.drawable.leder);
LinearLayout.LayoutParams img_lot_params = new LinearLayout.LayoutParams(164, 164);
img_lot_params.setMargins(0, 0, 0, 0);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(164, 164);
layoutParams.setMargins(-132, 0, 0, 0);
ll.addView(image, img_lot_params);
hview.addView(ll, layoutParams);
Note: my plan is to scroll the image from left to right.
First, the left part of the image is hidden and can scroll to right to see the full image
ViewGroup.MarginLayoutParams params =
(ViewGroup.MarginLayoutParams)view.getLayoutParams(); params.topMargin = -100;
Negative margins should work in LinearLayout and RelativeLayout. What you probably need, is to scroll the HorizontalScrollView with scrollBy(int x, int y) or scrollTo(int x, int y) to achieve the "peek and scroll" effect you described.
Also keep in mind that using raw pixel units is generally a bad idea as the actual size will depend on the pixel density of the screen. Prefer dp measurements instead.