Im having problems specifiying the height of a relative Layout. From what i understand, these two blocks of code should be equivalent (myLayout is a RelativeLayout that i defined in XML, with an initial height of "0dp", and it's parent is also a RelativeLayout):
BLOCK 1:
RelativeLayout.LayoutParams p = (RelativeLayout.LayoutParams)myLayout.getLayoutParams();
p.height = (int)(35*scale);
myLayout.setLayoutParams(p);
myLayout.invalidate();
BLOCK 2:
RelativeLayout.LayoutParams p = (RelativeLayout.LayoutParams)myLayout.getLayoutParams();
RelativeLayout.LayoutParams newP = new RelativeLayout.LayoutParams(p.width, (int)(35*scale));
myLayout.setLayoutParams(newP);
myLayout.invalidate();
Scale, in my case, is 2.
So i expect myLayout to have a height of 70 after the execution of either of these blocks, and i expect it to return that height when i call myLayout.getHeight(). Also, i expect myLayout to occupy a rect with the height of 70 and its former width (happens to be match_parent).
But when i run the code, the first block does not change the height of myLayout on screen, nor does it change the return value of myLayout.getHeight(). It does, however, change myLayout.getLayoutParams().height.
Now the second block does work, although i have to run it twice(!?) for the change to take effect. Im seriously at a loss here and i cant find anything even closely related to this in the docs.
I thought this would be an easy task when i set out yesterday, but by now im questioning my sanity, among other things. Any Ideas?
If size is determined dynamically, you may need to use ViewTreeObserver to get the size seen on the screen.
Set your width and height in defining your params. If you want the width to be match_parent, change your code like below and it is going to work (if you don't want match_parent, just set the width that you desire in the code below):
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT, (int)(35*scale));
myLayout.setLayoutParams(params);
Related
that might be a silly question but i didn't find any answer on the internet.
I want to create a button that fills the whole screen (width = 100%, height = 100%) when pressing another button.
Therefore i tried this:
Button button = new Button();
button.setText("fills screen");
button.setWidth(100);
button.setHeight(100);
the problem is that the setWidth and setHeight methods are dealing with px and not with percent.
What do i need to add to use percent values instead of px values?
I'll link here a post that details how to set a width programmatically
programmatically set button width to 50 of parent linearlayout
You can utilize that method (except do not divide by 2 JohnEye demonstrates in his answer)
Or you can utilize the LayoutParams class and create a new instance that will have the parameters set and pass those parameters to your object.
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
button.setLayoutParams(lp); // Assuming you've already initiated it of course as above
MATCH_PARENT will fill up the view. If the root view is the highest view on the hierarchy then this will work, as it will fill up the entire view. If not, the button would need to be moved there. (It may be your linearLayout or some other view)
Hope this helps.
As the title states, I am trying to set the width of a layout element to be equal to the height (which is set to match the parent). I've tried setting the width parameter programatically from the height parameter, but this shows up as -1 rather than the actual height (since it matches the parent). Anyone know how I can create square layout elements whose size is dynamic? Thanks.
So what I've been doing for things like this -- and I don't know that it's ideal, but it works quite well -- is use ViewTreeObserver.OnGlobalLayoutListener. Something like this:
View myView = findViewById(R.id.myview);
ViewTreeObserver observer = myView.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new SquareLayoutAdjuster());
class SquareLayoutAdjuster
implements ViewTreeObserver.OnGlobalLayoutListener {
#Override
public void onGlobalLayout() {
int dimension = myView.getHeight();
LayoutParams params = myView.getLayoutParams();
params.width = dimension;
myView.setLayoutParams(params);
observer.removeGlobalOnLayoutListener(this);
}
}
That's just the general idea, I don't know if it compiles as is, but basically in the onGlobalLayout() method, your views are guaranteed to be measured. You remove the listener at the end to prevent it from being called multiple times (unless you need that to happen for whatever reason).
the solution of KCOPPOCK works for me, thank you. But i got "java.lang.IllegalStateException: This ViewTreeObserver is not alive, call getViewTreeObserver() again" at runtime. So i use
findViewById(R.id.serie_main_layout).getViewTreeObserver().removeOnGlobalLayoutListener(this);
instead
observer.removeGlobalOnLayoutListener(this);
and all works perfectly.
I have four ImageViews in a layout. I set the maximum width for each one of them by setMaxWidth function. The problem is that they keep a small distance between them and I don't want that. If I don't stipulate the maximum width, these gaps disappear, but the ImageViews sizes are inappropriate (bigger than it should be).
The main code is:
musicSubmenuButton = new ImageView(context);
musicSubmenuButton.setId(id++);
musicSubmenuButton.setImageResource(R.drawable.bt_musicas_on);
musicSubmenuButton.setMaxWidth(buttonSize);
musicSubmenuButton.setAdjustViewBounds(true);
photosSubmenuButton = new ImageView(context);
photosSubmenuButton.setId(id++);
photosSubmenuButton.setImageResource(R.drawable.bt_fotos_off);
photosSubmenuButton.setMaxWidth(buttonSize);
photosSubmenuButton.setAdjustViewBounds(true);
agendaSubmenuButton = new ImageView(context);
agendaSubmenuButton.setId(id++);
agendaSubmenuButton.setImageResource(R.drawable.bt_agenda_off);
agendaSubmenuButton.setMaxWidth(buttonSize);
agendaSubmenuButton.setAdjustViewBounds(true);
infoSubmenuButton = new ImageView(context);
infoSubmenuButton.setId(id++);
infoSubmenuButton.setImageResource(R.drawable.bt_info_off);
infoSubmenuButton.setMaxWidth(buttonSize);
infoSubmenuButton.setAdjustViewBounds(true);
The size I wanted, but without the gaps (obtained using the above code):
The ImageViews without the gaps, but in the original resolution (obtained using the above code, but commenting the setAdjustViewBounds calls):
Does anybody know why these gaps appear and/or a workaround to get rid of these gaps and keep my desired size?
Instead of setting max width, set layout_width on them to fill_parent. Then they will space out evenly.
i have a relative layout on which i am displaying a page and some content. when i zoom my page...the layout size is not increasing. i want my layout to increase its size dynamically. how do i set it??
i tried doing it in java code.
contentLayout.getLayoutParams().height = x (some value which is equal to the page size)
contentLayout.requestLayout()
but this is nt working. i have also set the layout params android:layout_width and android:layout_height to wrap-content in the xml file.
Kindly, help me out. thank you
You can do it this way. I also added setting of the margins:
RelativeLayout targetItem = (RelativeLayout) findViewById(R.id.RelativeLayout01);
RelativeLayout.LayoutParams adaptLayout = new RelativeLayout.LayoutParams(mWidth, mHeight);
adaptLayout.setMargins(marginLeft, marginTop, marginRight, marginBottom);
targetItem.setLayoutParams(adaptLayout);
For mWidth and mHeight you also may set dip values to make it adapt to the different screen sizes. Easy way to do that is to define Dimension in your strings.xml and work with them, not with absolute values.
So, if you define relative_width as 120dip, and relative_height as 100 dip then in code you have
RelativeLayout.LayoutParams adaptLayout = new RelativeLayout.LayoutParams(getResources().getDimension(R.dimen.relative_width), getResources().getDimension(R.dimen.relative_height));
I want to fill the screen with a 100 different letters in random positions. On the iPhone I just created a bunch of UILabels set their x and y positions and then used animations to move them about.
On Android it doesn't look like I can add a TextView to my view and specify its X and Y. Is there a way to do this?
View gameView = findViewById(R.id.gameboard);
tv = new TextView(gameView.getContext());
tv.setText("A");
tv.setWidth(w); tv.setHeight(h);
// How to set the X and Y?
EDIT: The solution was to use AbsoluteLayout:
AbsoluteLayout al = (AbsoluteLayout)findViewById(R.id.gb_layout);
tv = new TextView(this);
AbsoluteLayout.LayoutParams params = new AbsoluteLayout.LayoutParams(
AbsoluteLayout.LayoutParams.WRAP_CONTENT,
AbsoluteLayout.LayoutParams.WRAP_CONTENT,10,10);
params.x = 50;
params.y = 50;
al.addView(tv, params);
and to move it base on MotionEvent me:
AbsoluteLayout.LayoutParams p = new AbsoluteLayout.LayoutParams(
AbsoluteLayout.LayoutParams.WRAP_CONTENT,
AbsoluteLayout.LayoutParams.WRAP_CONTENT,(int)me.getX(), (int)me.getY());
mTV.setLayoutParams (p);
I think you are making a game and for this you should look into SurfaceView here is an good example then
1...if you look into code there is onDraw method where you will get the width of screen and height
2...Now taking this width and height as seed for random generator get x and y position.
3...Iterate through point 2 100 times and you will get the desired result
You can use a FrameLayout for this. Set each TextView's background to transparent and add it to the FrameLayout. Make each TextView, and the FrameLayout, fill their parent. The FrameLayout places all its child views at (0,0). To move letters around, just change the top and left padding of the corresponding TextView.
In android positioning child views is the responsibility of the layout class.
You need to create a custom.layout and overide the onLayout method. In this method you can iterate through all the child views calling View.layout(left,top,right,bottom) on each child.
i found this example code whilst trawling the net :
https://gist.github.com/882650
You should also check out view.setTranslationX (and Y) which might be exactly what you need
I'm not entirely sure how you'd go about doing this in code, but you need to use an absolute layout as your root element. (edit: Do not use absolute layout, as it was pointed out that it is deprecated. The closest alternative seems to be RelativeLayout.)
The properties in the XML format for the absolute layout coordinates are layout_x and layout_y.
edit: A little research is saying you need to be using setLayoutParams, but my Eclipse IDE is not working properly, so unfortunately I can't test exactly what you're looking for.