I have couple textfields and button a the bottom inside LinearLayout and it looks ok. I want to add around ScrollView so when keyboard shows that I can scroll. When I add ScrollView my button from bottom and shows up on center screen. (If there is no ScrollView it is a the bottom of screen, where it should be). What to change to make this works and button be inside scroll but on the bottom of screen.
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical" >
<ImageView
android:layout_width="269px"
android:layout_height="161px"
android:layout_gravity="center_vertical|center_horizontal"
android:src="#drawable/show" />
<com.example.widgets.CustomTextView
android:id="#+id/txt_message"
style="#style/paragraph"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="80px"
android:text="#string/pin/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="top|center_horizontal"
android:layout_marginTop="150px"
android:layout_weight="1"
android:gravity="top|center_horizontal"
android:orientation="vertical" >
<com.example.widgets.CustomTextView
style="#style/caption"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:gravity="center_vertical|center_horizontal"
android:text="#string/pin_enter" />
<com.example.widgets.CustomEditText
android:id="#+id/txt_code"
style="#style/pin"
android:layout_width="310px"
android:layout_height="110px"
android:layout_gravity="center_vertical|center_horizontal"
android:background="#drawable/selector_edit_text"
android:gravity="center_vertical|center_horizontal"
android:maxLength="4"
android:numeric="integer"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:textAlignment="center" />
</LinearLayout>
<Button
android:id="#+id/btn_save"
style="#style/button"
android:layout_width="625px"
android:layout_height="110px"
android:layout_gravity="center_horizontal"
android:background="#drawable/button"
android:enabled="false"
android:text="#string/login" />
</LinearLayout>
</ScrollView>
You basically can not.
ScrollView's child is per definition measured as wrap_content. That's the reason, why your inner Views is shrinked to the minimum size fitting the content.
Move the Buttons out of the ScrollView (on the same level of hierarchy), if you want to have them fixed on the bottom.
Related
I'm updating an old existing app to be 4+ only and am trying to introduce a flavour of Material Design for < Lollipop. To that end, I am using MaterialDesignLibrary to add a few elements - in particular, the floating button.
The floating button displays OK, along with its animation, but I cannot get its bottom position displaying correctly.
Despite the recommendation that the component should be put in the right-bottom of the screen, I am trying to put the component in the right-bottom of my container wrapper... The right positioning works ok, but the bottom positioning seems to take from the wrong element.
Here is my xml for the section in the screen:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:layout_marginTop="10dip" >
<RelativeLayout
android:id="#+id/llFeelingContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/light_background"
android:baselineAligned="true"
android:orientation="vertical" >
<TextView
android:id="#+id/tvFeelingTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="3dip"
android:paddingLeft="5dip"
android:textSize="16sp" />
<View
android:id="#+id/vSeparatorTop"
android:layout_width="match_parent"
android:layout_height="1dip"
android:layout_below="#id/tvFeelingTitle"
android:background="#color/material_lightgreen_100" />
<LinearLayout
android:id="#+id/llCircleContentHolder"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_below="#id/vSeparatorTop"
android:baselineAligned="false"
android:orientation="horizontal"
android:paddingBottom="5dip"
android:paddingTop="3dip" >
<LinearLayout
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:gravity="center"
android:text="pain"
android:textSize="14sp"
android:textStyle="bold" />
<com.app.prohealth.extras.CircleDisplay
android:id="#+id/circlePain"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/circle_0"
android:gravity="center" />
</LinearLayout>
<LinearLayout
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:gravity="center"
android:text="energy"
android:textSize="14sp"
android:textStyle="bold" />
<com.app.prohealth.extras.CircleDisplay
android:id="#+id/circleMood"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/circle_0"
android:gravity="center" />
</LinearLayout>
<LinearLayout
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:gravity="center"
android:text="mood"
android:textSize="14sp"
android:textStyle="bold" />
<com.app.prohealth.extras.CircleDisplay
android:id="#+id/circleEnergy"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/circle_0"
android:gravity="center" />
</LinearLayout>
</LinearLayout>
<View
android:id="#+id/vSeparatorBottom"
android:layout_width="match_parent"
android:layout_height="1dip"
android:layout_below="#id/llCircleContentHolder"
android:background="#color/material_lightgreen_100" />
<TextView
android:id="#+id/tvFeelingSubTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/vSeparatorBottom"
android:paddingBottom="3dip"
android:paddingLeft="5dip"
android:text="How are you feeling now?"
android:textSize="16sp" />
</RelativeLayout>
<com.gc.materialdesign.views.ButtonFloat
android:id="#+id/buttonFloat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_marginTop="20dp"
android:background="#1E88E5"
materialdesign:animate="true"
materialdesign:iconDrawable="#drawable/fab_ic_add" />
</FrameLayout>
Here is a shot of where the Floating Button appears, and where it should appear:
You'll see in the code that I have tried negative margins and padding, but nothing I've tried has had any effect.
The Floating Button should appear aligned with the bottom of tvFeelingSubTitle; what has gone wrong?
UPDATE
The suggestion from mistwalker did work, in as much as the button now aligns itself to the bottom. However, it expands the height of the bottom container to match the height of the button:
I tried a number of different variations on the suggestion, which either squashed the button to fit a fixed height container, or overwrote the content completely.
The layout I am wanting to create is more like this:
Maybe this isn't possible and I'm wasting my time and yours...
What do you think?
Try putting
android:layout_below="#id/vSeparatorBottom"
android:align_baseline=#id/tvFeelingSubTitle
android:alignParentRight="true"
and remove the margin attributes plus the alignParentBottom one.
That should do it.
ADD:
About your second question - You are seeing that because your floating action button(FAB) is INSIDE your RelativeLayout.
Instead have FrameLayout as the outermost parent and the RelativeLayout (minus the FAB) and FAB as siblings.
Then adjust the style on the FAB to sit in the bottom right corner of your FrameLayout with layout_gravity=bottom|right and any margins you may want to add.
I have a game layout in my app that has four buttons that should display at the top of my view and then I have two FrameLayouts that are also in this linear layout. The first FrameLayout just makes the top 1/4 of the view so my imageButtons that implement the Drag and drop API are not allowed at the top. The second FrameLayout is for the bottom 3/4 of the view which has 4 imagebuttons in it.
My issue is that when the game layout is displayed, the buttons in the linear layout are displayed just how i want them to, but the two FrameLayouts are squished to the top right corner.
Here is the code:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/game_layout" >
<Button
android:id="#+id/doneButton"
android:layout_width="170dp"
android:layout_height="wrap_content"
android:onClick="onDoneClicked"
android:text="#string/done" />
<Button
android:id="#+id/cancelButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onCancelClicked"
android:text="#string/cancel" />
<Button
android:id="#+id/leaveButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onLeaveClicked"
android:text="#string/leave" />
<Button
android:id="#+id/finishButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onFinishClicked"
android:text="#string/finish" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.75"
android:orientation="horizontal"
android:id="#+id/topHalf" >
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:orientation="horizontal"
android:id="#+id/bottomHalf" >
<ImageButton
android:id="#+id/polarCapButton1"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|left"
android:background="#drawable/polarcap" />
<ImageButton
android:id="#+id/polarCapButton2"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|right"
android:background="#drawable/polarcap" />
</FrameLayout>
<TextView
android:id="#+id/scores"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageButton
android:id="#+id/spaceRockButton1"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|left"
android:background="#drawable/spacerock" />
<ImageButton
android:id="#+id/spaceRockButton2"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:background="#drawable/spacerock" />
</LinearLayout>
Any help would be appreciated guys. Thanks.
I see several things that can be a potential problem.
Your linear layout contains several widgets and only 2 of those contain layout_weight values. To solve this issue, you want to wrap your two frame layouts in another LinerLayout container, where you specify the distribution of your 2 frame layouts inside your new linear layout.
In your new linerLayout, you want to specify the total weight of the layout, such as: android:weightSum="1".
Depending of your linear layout orientation, (if unspecified, it will be horizontal) If your LinearLayout orientation is horizontal, specify: android:layout_width="0dp". If the orientation is vertical, specify: android:layout_height="0dp" for the child elements.
Also, it seems to me that you want your buttons to align horizontally next to each other (horizontal orientation), and your two frames to align underneath the buttons, but right underneath each other (vertical orientation).
So in this case, the code would look something like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/top_layout" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/game_layout" >
<Button
android:id="#+id/cancelButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onCancelClicked"
android:text="#string/cancel" />
<Button
android:id="#+id/leaveButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onLeaveClicked"
android:text="#string/leave" />
<Button
android:id="#+id/finishButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onFinishClicked"
android:text="#string/finish" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="1"
android:id="#+id/ll_layout_frames" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.75"
android:orientation="horizontal"
android:id="#+id/topHalf" >
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.25"
android:orientation="horizontal"
android:id="#+id/bottomHalf" >
<ImageButton
android:id="#+id/polarCapButton1"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|left"
android:background="#drawable/polarcap" />
<ImageButton
android:id="#+id/polarCapButton2"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|right"
android:background="#drawable/polarcap" />
</FrameLayout>
</LinearLayout>
<!-- you can insert more widgets here, they will be placed in the vertical linear layout -->
</LinearLayout>
So I have an ExpandableListView and basically I have some text (person's name), and then I want a button on the same row (towards the right side if possible). Currently, this code in my XML file it has the text on one line, and then the button on the next line, I've played around with it but can't figure out how to get it both on one line.
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="55dip"
android:orientation="horizontal"
android:weightSum="1" >
<TextView
android:id="#+id/lblListItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="17dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_weight="0.8"
android:textSize="11dp"
android:text="Checkout"
android:id="#+id/checkout" />
</LinearLayout>
This will do the trick.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="55dip"
android:orientation="horizontal" // Edited here
android:weightSum="1" > // here
<TextView
android:id="#+id/lblListItem"
android:layout_width="match_parent" //and here
android:layout_height="35dp"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="center_vertical"
android:layout_weight="0.2" // here
android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent" // and here
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_weight="0.8" // and here
android:textSize="12dp"
android:text="Checkout"
android:id="#+id/checkout" />
</LinearLayout>
Make sure of 2 things when using weightsum:
if orientation is horizontal, width of all child views to be displayed in a row will need to have "match_parent" and some value for layout_weight.
0.2 will give 80% of horizontal width of parent and 0.8 will give 20%.
You have
android:orientation="vertical"
in your parent LinearLayout which stacks elements from top to bottom (naturally). Remove that line (since horizontal is the default orientation for LinearLayout). Then you would need to use weight or margin to get it positioned where you want.
Another option would be to replace your LinearLayout with a RelativeLayout. This will allow you to use android:alignParentRight="true" on your Button to put it on the right side of the screen (RelativeLayout puts Views at the top-left by default).
I want to layout 3 element Button , TextView , Button on the bar like this
[Button1] --- TextView --- [Button2]
2 button are always anchor fixed in left and right screen(padding 4dp) ,and TextView is center (change width depend on screen size),They should be apply for all screen size(not scaled in larger screen). How can I do it??
It would look something like this.-
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true" />
</RelativeLayout>
Notice that you still need to set other properties such as texts, ...
You can use LinearLayout with android:weightSum
Try Like this, By using android:weightSum, you can divide how much space for Button and textView.. And it will automatically adjust in all the density devices.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="10" >
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_margin="4dp"
android:layout_weight="3"
android:text="LeftButton" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="4dp"
android:layout_weight="4"
android:gravity="center"
android:text="Center TextView text" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_margin="4dp"
android:layout_weight="3"
android:text="Right Button" />
</LinearLayout>
android:weightSum work fine for me, I resolved all my troubles :)
I've been trying to create what I think is a relatively simple layout for the past hour.
What I want is to have an edit text take up the vertical real estate left over after my buttons and labels are put onto the screen. I've found plenty of information on how to do that here at SO and I feel like I've implemented that information here (layout_weight and 0dip height), however as soon as I put in the 0dip height my edittext is just not displayed.
What am I missing here?
Edit: Turns out that my Layout2 was set to match_parent and this leaves 0 available space for the weighted children to occupy. I changed the behavior there to wrap_content and all worked great.
I'm targeting Android 2.3.3
Here's my layout xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/description"
android:textAppearance="?android:attr/textAppearanceSmall" />
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="0dip"
android:inputType="textMultiLine"
android:layout_weight="1" >
<requestFocus />
</EditText>
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/save" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/clear" />
</LinearLayout>
</LinearLayout>
because linearLayout2 fills out the parent. linearLayout2 must have layout_weight behaviour or own height not match_parent.