Im running into a speed bump in my android App. I want to center my Linear Layout in the center of the screen (Horisontally only) and I want to center another element only vertically. I haven't seen an easy apparent way to do this in the program.
http://i.stack.imgur.com/gfMBD.png
I want the grey Box to be centered horisontally in the app
I'm not sure, how your layouts are now, but I'd do something like this:
Vertical Linearlayout
RelativeLayout, centered horizontally
RelativeLayout, set to match parent
RelativeLayout, centered vertically
Use RelativeLayout as a root ViewGroup, and then just add android:layout_centerHorizontal="true" to your first LinearLayout, and android:layout_centerVertical="true" to your second one.
Related
Let's say I have two views that I want to center vertically. The first view is bigger than the second view.
I noticed that if I place theses two views inside a RelativeLayout with properties layout_height="wrap_content" and android:gravity="center_vertical" nothing happens. This is what I get :
In the opposite, if I place these two views inside a LinearLayour with properties layout_height="wrap_content" and android:gravity="center_vertical" the views are centered vertically :
Lastly, if I place these two views inside a RelativeLayout with properties layout_height with a fixed height and android:gravity="center_vertical" I get the same result as the LinearLayout. The views are centered vertically.
I would expect the views to be centered vertically in each case. Do you know the reason for this difference?
LinearLayout handles all its child object based on its orientation (Horizontal or vertical). So when you are saying gravity: "center_vertical". You are actually referencing based on your parent layout.
In case of RelativeLayout,it enables you to specify the location of child objects relative to each other (child A to the left of child B) or to the parent (aligned to the top of the parent).
Personally I would use gravity only in LinearLayouts and the centerInParent for RelativeLayouts.
In your first case it'll work with RelativeLayout as you expected if you use android:layout_centerVertical="true" to the child view which you want to be centred.
So in case of LinearLayout you need to specify the orientation first (i.e. horizontal/vertical) so that the child views are inflated based on the reference of your parent layout.
While in RelativeLayout, as the name says it all, you can specify the position with respect to the views which are the child of a parent RelativeLayout.
Now the views you want to achieve can be generated in many other ways too.
For example, set your parent layout as LinearLayout. Don't specify any gravity attribute in the parent layout. Hence, you set a layout_gravity attribute to the child to certer_vertical and this should work too.
Well, after some others tests, it seems that the behavior of android:gravity for a RelativeLayout is a bit random. I will just avoid to use is.
I am working on an android app and I have a scrollview, and in that, I have a linear layout in the main activity. I want to have the top and bottom borders of the linear layout to be "faded
" so as the user scrolls down the child views in the layout will "fade out" as the go down.
Here is an example of what I am trying to achieve in my linearlayout or scrollview (the "fading bottom border".
Thanks for you help!
Subby
You can do that by using the following attributes in the ScrollView.
android:requiresFadingEdge="vertical"
android:fadingEdgeLength="20dp"
If you want it on devices before API14, you need to use the following:
android:requiresFadingEdge="vertical"
android:fadingEdgeLength="20dp"
NOTE: This is actually discouraged as it is seen to have a negative performance impact on devices.
At the bottom of your layout, you can have a view above the listView, and set Gradient Color for the view, and also make it semitransparent, then you can have the effect you want.
I use a relative layout. I was wondering, if I want to place a text field to the right "of the middle" of another view (say imageview). How can I do it? I always know how to place it to the right, but it seems that the middles of both views are not aligned
Thanks
Put them both in a relative layout, and set the gravity for the relative layout to center.
I just started Android development and I'm having trouble figuring out why my text alignment inside my button isn't horizontally centered. It is vertically centered.
I've tried gravity, some padding, center, and text alignment center with no luck. I am using a RelativeLayout with an EditText and a TextView above. When I take the other views out of the activity, there is also no change.
I can get it to center with gravity center_vertical and some padding left, but this method seems inappropriate.
Here is a view of the button before update:
Update: Tested on my ADV for android:gravity="center_vertical|center_horizontal" and it ended up working fine. Seems to be a bug with the Graphic Layout view in Eclipse that is mis-aligning the inner context of the Button view. Here's it working on my AVD:
Set the gravity of the button as android:gravity = "center" or android:gravity = "center_vertical|center_horizontal" .
try layout_gravity="center" or gravity="center" instead. I always forget which one : P
android:gravity = "center"
will align the text inside the button both horizontally and vertically.
android:layout_gravity is different from android_gravity.
Refer for a detailed answer:
Gravity and layout_gravity on Android
I want to make an about button in the top left. I tried:
android:layout_gravity:"top|left"
but it doesn't work , I searched and all what I found was using RelativeLayout and if I use that I'll have to make all my layout from beginning and it's not that good like the linear layout.
Couldn't post the code here. So this is my code on pastebin
http://pastebin.com/5EjgyB0K
Here you have given android:layout_gravity="center" to the Linear Layout so it is going to set gravity of the layout and as center and your About Button is child of layout its to going to set in center and you have given Margin_top also.Try to remove gravity amd Margin_top and you can see the result, the button will be top|left of the screen.