Changing LinearLayout Height from top (default from bottom) in Android programmatically (Java) - java

I want to change the height of a LinearLayout and works everything OK, but I want height decreasing and increasing from the top (the defaults starts decreasing and increasing from the bottom).
Here is an example:
LinearLayout linearLayout = findById(R.id.layout);
LinearLayout.LayoutParameters layoutParams = (LinearLayout.LayoutParameters) linearLayout.getLayoutParams();
layoutParams.height = linearLayout.getHeight() - 1;
linearLayout.setLayoutParams(layoutParams);
This works OK. The only thing I wanna change is the direction of height change.
Thanks for the replay.

You can achieve this by binding(the bottom) of your linear layout inside a constraint layout,
Also is there a parent to your linear layout?, if so then it being unbound might be the reason

I achieved changing the direction with the setLayoutDirection(View.LAYOUT_DIRECTION_LOCALE) from teh ViewGroup class. Also had to change layout's gravity to bottom in the xml.
Thanks for contribute to my answer.

Related

Weight property on android linear layout is not working

I am aware that there are a lot of questions out there with a similar question. However, most of them deal with horizontal orientation whereas my problem is with vertical orientation. And I have tried a few but they were of no help.
I am having issues with vertical orientation of linear layout. I have got this so far:
My layout parameter for this is as follows:
LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
lparams.weight = 1;
// And then simply adding this Layout param to all the widgets
However, when I set the height property to 0, I get nothing. My understanding is I should still get the same output since it should be assigning equal height to all of them based on the weight property. Can any one please help out?
I have finally figured out the problem. The problem was not with my understanding. My understanding is correct. However, when setting the content view I was passing in the same LayoutParams that I used for widgets. When, I set the height to 0, it would all set the height of everything to 0. To get around it, I just needed to create a new LayoutParam and pass that in with Content View.

How do i add a textview ontop of a picture

UPDATE: so i got the picture as a layout:http://gyazo.com/38a4c80dab36e15593a32f35951b9f7d
Now how do i make it so that i can put text on the picture. I want to make sure that the text doesnt go beyond the picture box. Thanks in advance!
Add a RelativeLayout where your picture is now. I suppose your picture has a fixed size (e.g. 60dp wide and 60dp high). Use that size for your RelativeLayout. Now add an ImageView as first child with both width and height set to match_parent. This ImageView will contain your picture. Add a TextView as second child, also with width and height set to match_parent. Now you have a TextView on top of an ImageView, both with the same size.

Few questions about android's screen orientation

I write a rectangle widget.
After writing its portrait XML, I want to write its landscape.
I should change any LinearLayout orientation from horizontal to
vertical?
horizontal is right to left (not top to bottom) relative to the
screen in origin portrait mode?
If i don't mention layout_height property, what is the default?
(match parent?)
how does weight property take effect when I specify
layout_width = X dip?
when I specify layout_width = wrap_content?
what does fill_parent mean when in top-most LinearLayout?
1a) No, you should not.
1b) Horizontal is left to right relative to the screen in origin portrait mode.
2) There is no default value, you have to set it (other time you will see an error)
3) Please look at that question
4) The view should be as big as its parent (minus padding)
To give the task of managing screen orientation to Android on behalf, you don't need to think all this stuff. Just mention android:configChanges="orientation|keyboardHidden|screenSize|screenLayout" in your activity from AndroidManifest.xml

Android Custom View: MATCH_PARENT with OnDaw don't work

I'm new to Android but i have a simple problem.
I have a vertical linear layout, if i place here a normal button, and give it the MATCH_PARENT flag for the width and the height, the button will fill all the space. All ok.
Now i have a my simple custom view (a class that extends View) and i overrided the OnDraw method for draw a cross in the middle of the view. For draw the cross i need to get the width and the height of the control, if in the UI Layout editor of eclipse i set a fixed width and height, it works correcly (i see a preview of the cross directly in Eclipse) but if i extend the View to the borders i got only WARP_CONTENT (With button i get MATCH_PARENT), no problem, i change it in the XML, but LayoutParams width and height will return 0!! (I see the error directly in the preview of eclipse!) Why?
I tried to set manualy the width and the height in the OnWindowFocusChanged method to wait but it give me always 0.
The problem is that the view actualy have the rights dimensions but when i call the getLayoutParams().width it give me 0, so i see a white rectangle in the right position (white background of the view), but the cross is drawed on the upper-left corner!
Sorry for my bad english.
Thank you.
Mauro.
i think u need to set this parameter for your view.
android:layout_alignTop
or something related to this.i guess

Two overlapping ImageViews in Android

In a ViewGroup I want to put two ImageViews overlapping: The imageview A have a transparent background and some drawing across the image, the imageview B.
I want both them to be at the same place and have the same size (but can't fix their size in the XML because if the image at ivB changes the ivA should resize to match the new size.
Since the ivA source image does not change I tried ivA.getWidth() and ivA.getHeight() for setting the ivB size but their value are 0,0 in the onCreate() method.
Is there some "easy" way of doing this?
Override the ImageView and in onLayout() you can get proper width or height after calling super.onLayout(). Try setting values in the place.
Try getting the dimensions in onWindowFocusChangedListener.
An easier way would be putting the ImageViews in a FrameLayout (which is built to put Views on top of the other), and resize the FrameLayout instead.

Categories