How to center align a view? - android

I need to lay a variable number of views (may just be one) next to each other like in LinearLayout. But I want the whole arrangement to be center aligned. The views should be next to each other. But the whole arrangement should be equidistant from the left and right edge of the screen or the containing parent. How can I accomplish this?

You will have to wrap your views inside a LinearLayout and your linear layout inside something else:
<LinearLayout
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal">
<View/>
<View/>
etc...
</LinearLayout>
</LinearLayout>
Make sure all your views use android:layout_width="wrap_content". If you are working with RelativeLayout, it will be:
<RelativeLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_centerHorizontal="true">
<View/>
<View/>
<View/>
</LinearLayout>
</RelativeLayout>

Did you try
android:gravity="center"
?

This will do it for you
android:layout_gravity="center_horizontal"
Also you want to consider the weight property to make sure that this layout element takes priority over others.
http://developer.android.com/reference/android/widget/LinearLayout.LayoutParams.html
To group everything together you can use a Frame Layout or Relative Layout.

Related

How would you design this in Android? A split screen with a button

I'm not sure whether to use a RelativeLayout and/or a LinearLayout to design the following screen! Would someone mind helping me out?
The correct answer can be found here:
How can I add the new "Floating Action Button" between two widgets/layouts
The easiest option (since you have the button overlay) would be to use a RelativeLayout with a LinearLayout nested inside (assuming the guy squatting is a different widget from the one containing text). If the image of the guy sqatting and the text are together in the same widget, then you just have to replace the LinearLayout with the widget. Children of RelativeLayout take higher precedence on the Z axis. So you can have a RelativeLayout with:
<RelativeLayout
android:height="match_parent"
android:width="match_parent" >
<LinearLayout
android:height="match_parent"
android:width="match_parent" >
<ImageView
android:src="some_source"
android:height="0dip"
android:width="match_parent"
android:layout_weight="1"/>
<TextView
android:text="your_text"
android:height="0dip"
android:width="match_parent"
android:layout_weight="1"/>
</LinearLayout>
<ImageButton
android:id="#+id/someId"
android:src="some_source"
android:background="#android:color/transparent"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
Keep in mind that I am assuming the image of the guy squatting is a separate widget (ImageView) and the text below that is a separate widget (TextView) and the '+' button is a separate widget which is overlaid over the content.
RelativeLayout as parent with a LinearLayout set to match_parent for the split screen with two more LinearLayout's at 1:1 weight each. Then a button that's centred and to the right with a margin should do it. If you're after exactly the one you posted. So like this:
<RelativeLayout>
<LinearLayout width=match_parent, height=match_parent>
<LinearLayout weight=1/>
<LinearLayout weight=1/>
</LinearLayout>
<Button position=center-right, margin_right=1cm/>
</RelativeLayout>
Something along those lines.
Edit: Changed it to match_parent since that's the newer version.

Layout weight in relative layout

I am aware about layout weight in linear layout. Can i assign layout weight in relative layout.
example: two image view in a layout which fills the layout in the ratio 60:40. first image should take up 60% of the whole screen height and the second image has to take the remaining 40% of the screen.
Don't just answer for this example problem alone please tell me the concept precisely or post some reference links about layout weight in relative layout. Thanks in advance.
You can place an invisible view in center of your layout and align your view in left and right. Here is an example
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toLeftOf="#+id/view"
android:background="#fffba2" />
<View
android:id="#+id/view"
android:layout_width="1dp"
android:layout_height="1dp"
android:layout_centerHorizontal="true"
android:visibility="invisible" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="#+id/view"
android:background="#ba2fff" />
</RelativeLayout>
There is no need of weights with Relative Layout. You can move around the Image Views to make sure that they are in correct proportion. Weights are only used with LinearLayout.
Actually you cannot use weight in a RelativeLayout but you can use a combination of Relative and Linear layouts in order to take both advantages of them!
Tip: Try to use as less layouts as possible to avoid slow UI, because of multiple screen measurements! [1] [2]

It is possible create a layout and view flipper on the same line?

It is possible create a screen of the following way?
![Screen][2]
When I change of page, the same layout is displayed on every page
Regards
Yes, the layout is possible:
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ViewFlipper android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ViewFlipper>
<!-- I assumed your layout at the top-left corner is a LinearLayout, otherwise replace it with what you have -->
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:id="#+id/firstLayout">
</LinearLayout>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/firstLayout"
android:layout_margin="5dp"/>
</RelativeLayout>
Edit:
Your question is a little ambiguous, if you want to just add a TextView near the Layout element from your layout then add the TextView and set it an attribute of toRightOf(with some margin to put some gaps). Check the modification in the layout above. If you want the TextView to be vertical centered(taking in consideration the height of Layout) like in your picture then you could wrap the Layout and TextView in a LinearLayout with (width and height set to wrap_content) and aligned to top and left in the parent RelativeLayout.
Your question isn't really clear, but I suspect what you're looking for is exactly what Kursprog gave you, except that you want to put the top left layout and the text view into a wrapper Linear layout, so your xml would look like this (minus all the attributes).
<RelativeLayout> <!--Parent -->
<ViewFlipper/>
<LinearLayout android:orientation="horizontal"><!-- wrapper to your two top layouts
<LinearLayout/> <!-- whatever layout it is that you're describing in your diagram -->
<TextView/> <!-- the text view in your diagram -->
</LinearLayout>
</RelativeLayout>
You'll want to give the wrapper a width of fill_parent and set the layout_weight of both inner elements to 1, so that they're distributed evenly on the screen.
I solved the problem with the FrameLayout. The FrameLayout to allow overlap Views

LinearLayout, RelativeLayout, etc. margins do not work as expected

Margins in group layouts do not seem to work.
For example,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_margin="40dip"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="I'm a button" />
</LinearLayout>
should display a button with 40p margins on all sides. However, it has 80p margins on the right and bottom.
Am I doing something wrong?
Is this a bug?
A workaround would be to use gravity, but this only works with even margins.
BTW, there is a similar question posted here but has not been answered.
android:padding="40dp" on the LinearLayout or android:layout_margin="40dp" on the Button will give you the effect you want. Padding defines the space between a views edges and its content, layout margin defines extra space on the sides of a view.
The problem is actually the way FrameLayout interprets margins. setContentView() attaches your "main" layout to a FrameLayout, which is the actual root of the view hierarchy (you can see that with Hierarchy Viewer) and is offered to you by the phone.
Margins are managed by the parent layout, so in this case that main FrameLayout. I don't know if it's a feature or a bug, but that's how this layout interprets margins.
So well, the solution was already posted while I was typing: use padding instead.
if you need set margin for a layout, simply wrap it with another linear or relative layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:layout_margin="40dip"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="I'm a button" />
</LinearLayout>
</LinearLayout>
Wrapping the Linear Layout with another layout is the best strategy.

Buttons below ListView are not fixed to the bottom of the screen when the ListView is empty

I have a Layout which contains a ListView some buttons below it.
What I want is for the buttons to be fixed to the bottom of the screen.
When the ListView has several items (more than what fits the screen) the code is working properly and the buttons appear in the bottom of the screen.
However, when the ListView is empty or does not require scrolling, the buttons are coming up the screen (they are not staying fixed in the bottom).
Here's the layout (some details omitted but I can add them if necessary):
<LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView />
<ListView
android:width="fill_parent"
android:height="wrap_parent"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal|bottom"
android:orientation="horizontal"
android:layout_weight="0" />
<Button
android:layout_weight="1" />
<Button
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
I expected setting the "gravity" of the second LinearLayout to "bottom" to make the buttons fixed to the bottom of the screen, but it's not working in the mentioned case.
Any ideas?
Thanks.
You have the layout_height of your top LinearLayout set to wrap_content. This means that it will shrink as its contents shrink. Try setting it to fill_parent instead. Or, give it a layout_weight of 1.
One of the options is to create TextView which will be shown instead of listview, when it is empty.
You do it with listView.setEmptyView(emptyView)
Then set layout_weight of this TextView to same weight as your listview. So when listview is empty, its place will be taken by emptyView and buttons will stay fixed at the bottom.
Also set your emptyView to android:gravity="center".
Other option:
In vertical RelativeLayout add to the layout with buttons property android:layout_alignParentBottom="true".
All the stuff with listView put inside LinearLayout and add to it android:layout_above="#+id/buttons_layout".
That's it. Elements inside LinearLayout will take their place depending on weight but whole layout wont go beyond buttons.

Categories

Resources