I have got a TableLayout, which fills a half of device's screen width. And I want to adjust it's TableRows height, depending on a size of a text, which I place in the TableRow.
What attribute should I use to do it in my XML description, because android:layout_height="wrap_content" doesn't help me?
First of all, You should convert it from dps to pixels using the display factor formula.
final float scale = getContext().getResources().getDisplayMetrics().density;
int trHeight = (int) (30 * scale + 0.5f);
int trWidth = (int) (67 * scale + 0.5f);
ViewGroup.LayoutParams layoutpParams = new ViewGroup.LayoutParams(trWidth, trHeight);
tableRow.setLayoutParams(layoutpParams);
This problem was solved by using android:stretchColumns and android:shrinkColumns XML-attributes of TableLayout class.
Related
I am trying to get the width of a textview setting the text. Here's the Code I am using:
public int getTextViewEffectedWidth(TextView textView, String content){
Rect bounds = new Rect();
Paint paint = textView.getPaint();
paint.getTextBounds(content, 0, content.length(), bounds);
return bounds.width();
}
But this method returns larger width. Example:
I need set in textview value "6978"
This method was calculate width = 46 (By the way returned value in dp or sp ? I set in dp, cause in px this value in not enough.)
If I set textview's width 46 dp or sp, TextView will has extra place
I need to get the width value which would be consistent with width="wrap_content"
TextView size and font have default values.
try this,you will get the value in pixel;
int width=convertToPixel(textView.getWidth());
int height=convertToPixel(textView.getHeight());
private int convertToPixel(int n) {
Resources r = getResources();
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, n, r.getDisplayMetrics());
return (int) px;
}
I know it's a little while after your request, but are you running the check in an OnGlobalLayoutListener?
If not, then the view won't be completely drawn on the screen, and you won't have the dimensions available.
Inside the onGlobalLayout() you can called measuredWidth and measuredHeight.
I hope this helps, if not you, then someone else.
I have an activity, containing a layout. I have a pool of 10 imagebuttons set up and hidden (invisible) in this layout. I am trying to load the image to these imagebuttons programmatically, and then set their location on the screen.
I have found this previous discussion: How to set margin of ImageView using code, not xml
This, however, does not work. I am getting tiny imagebuttons as a result, despite setting the height and width parameters. Here is my code:
RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(btnGP1.getLayoutParams());
lp1.leftMargin = P1leftValue;
lp1.topMargin = P1topValue;
lp1.height = 80;
lp1.width = 80;
btnGP1.setLayoutParams(lp1);
Something about this is not right, but I am not having any luck finding a more accurate resource. Any ideas?
Update:
I have edited my code to this:
RelativeLayout.LayoutParams lp1 = (RelativeLayout.LayoutParams)btnGP1.getLayoutParams();
lp1.leftMargin = P1leftValue;
lp1.topMargin = P1topValue;
lp1.height = 280;
lp1.width = 280;
btnGP1.setLayoutParams(lp1);
And it appears to be almost working. The only thing that remains now is that the functions above only appear to be allowing me to enter pixel sizes. I need to be able to enter pixel densities instead as not all screen sizes are the same.
My new question is this: how do I go about this? Is there a simple answer here? If I try 280dp or "280dp" the compiler will not allow it.
The answer to this was found in another thread. This was my final code:
final float SCALE = getResources().getDisplayMetrics().density;
RelativeLayout.LayoutParams LP = new RelativeLayout.LayoutParams(btnGP15.getLayoutParams());
LP.leftMargin = (int) (P1leftValue * SCALE + 0.5f);
LP.topMargin = (int) (P1topValue * SCALE + 0.5f);
LP.height = (int) (tileSize * SCALE + 0.5f);
LP.width = (int) (tileSize * SCALE + 0.5f);
Where btnGP15 is my image, P1leftValue and P1topValue store the dp values for my required location, so 100 and 150 for example. tileSize stores the dp value for the size of my images, 70 in my case.
Working perfectly ;)
I have gridview which contains 2 columns. The width is set to fill available space.
I need height to be set to specific size according to width, because in every screen the width dimension is different, I can't set height in xml as constant. I need to set ratio, so height should be about 1.5 * width.
Thanks
You can set Height, but you want to get Width First.
You told 2 columns of grid so understand phone half of your phone width
Display display = getWindowManager().getDefaultDisplay();
int imgWidth = display.getWidth() / 2;
int imgHeight = (int)(imgWidth * 1.5f);
You get height of that.Now you can set your Grid View Width and Height
LayoutParams lp = HERE_YOUR_VIEW.getLayoutParams();
lp.width = imgWidth;
lp.height = imgHeight ;
HERE_YOUR_VIEW.setLayoutParams(lp);
I need to change the size (specifically the height) of a button in an Android app where the button resides in a table and is created programmatically. I have tried about 20 different approaches and failed miserably. If I create a button outside the table, I can change the size without a problem, but as soon as it goes in a table, the height stays fixed even though I can change the width.
I have tried creating and using, LinearLayout params, ViewGroup layout params, TableLayout params, etc. and setting their heights either via that constructor (e.g WRAP_CONTENT) or using setHeight(). I have also tried simply calling button.setHeight(). I have changed settings in my xml manifest for the table as well. I have used pixel values and Dpi values. All of this has failed. Here is the basics of what I have so far (just showing a call to b.setHeight()):
TableRow row = new TableRow(getApplicationContext());
row.setId(counter);
TextView t = new TextView(getApplicationContext());
t.setText("BLH " + counter);
t.setTextColor(Color.BLACK);
//I have also tried table.getContext() in this constructor...
Button b = new Button(getApplicationContext());
b.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v)
{
//blah blah blah
});
//Convert from pixels to Dpi
final float scale = getResources().getDisplayMetrics().density;
int heightDp = (int) (33 * scale + 0.5f);
int widthDp = (int) (60 * scale + 0.5f);
b.setText(R.string.removeButtonText);
b.setTextSize(12);
b.setTag(counter);
b.setHeight(heightDp);
b.setWidth(widthDp);
b.setId(counter);
counter++;
row.addView(t);
row.addView(b);
// add the TableRow to the TableLayout
table.addView(row,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
This correctly results in a button being placed in the table, but the height does not change no matter what values I use. In my xml file, here is what the table declaration looks like:
<TableLayout
android:id="#+id/myTable"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp"
android:stretchColumns="0">
</TableLayout>
I have tried messing with stretchColumns and other settings here; again, to no avail. Does anyone have any idea why I can't change the height of a button added to my table programmatically, but have no problems doing so outside the table? I am sure it is some setting or adjustment that I just haven't found. Any help is appreciated as I am at my wits end. Thanks in advance.
I fixed my problem. Turns out it was an ordering issue. The problem is that I was setting the height before adding the button to the table. This caused the button's LayoutParams to not have any parent as explained here: ViewGroup.getLayoutParams(). Here is what I needed to do (basically, change height after adding the button to the table):
table.addView(row,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
//now change the height so the buttons aren't so big...
final float scale = getResources().getDisplayMetrics().density;
int heightDp = (int) (33 * scale + 0.5f);
ViewGroup.LayoutParams params = b.getLayoutParams();
params.height = heightDp;
b.setLayoutParams(params);
b.requestLayout();
table.requestLayout();
The last two lines were suggested above by Lawrence. The table and button seemed to update without these. However, it seemed to me to be a good idea to update just in case.
Try this
int heightDp = (int) (33 * scale + 0.5f);
int widthDp = (int) (60 * scale + 0.5f);
ViewGroup.LayoutParams bLp = new ViewGroup.LayoutParams(widthDp,heightDp);
b.setLayoutParams(bLp);
instead of
b.setHeight(heightDp);
b.setWidth(widthDp);
I think you need to call View.requestLayout, this is not done automatically every time you add/remove child views.
So I need to change the size of an image depending on the area of the screen. The image will have to be half of the screen height, because otherwise it overlaps some text.
So Height= 1/2 Screen Height.
Width = Height*Aspect Ratio (Just trying to keep the aspect ratio the same)
I found something that was:
Display myDisplay = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width =myDisplay.getWidth();
int height=myDisplay.getHeight();
But how would I change image height in java? or even XML if possible? I can't seem to find a working answer.
You can do this with LayoutParams in code. Unfortunately there's no way to specify percentages through XML (not directly, you can mess around with weights, but that's not always going to help, and it won't keep your aspect ratio), but this should work for you:
//assuming your layout is in a LinearLayout as its root
LinearLayout layout = (LinearLayout)findViewById(R.id.rootlayout);
ImageView image = new ImageView(this);
image.setImageResource(R.drawable.image);
int newHeight = getWindowManager().getDefaultDisplay().getHeight() / 2;
int orgWidth = image.getDrawable().getIntrinsicWidth();
int orgHeight = image.getDrawable().getIntrinsicHeight();
//double check my math, this should be right, though
int newWidth = Math.floor((orgWidth * newHeight) / orgHeight);
//Use RelativeLayout.LayoutParams if your parent is a RelativeLayout
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
newWidth, newHeight);
image.setLayoutParams(params);
image.setScaleType(ImageView.ScaleType.CENTER_CROP);
layout.addView(image);
Might be overcomplicated, maybe there's an easier way? This is what I'd first try, though.