Centering my linear layout in android - java

I'm trying to achieve this positioning:
Where I have two rows of EditText and TextView next to each other.
The version textview is anchored to the bottom of the screen, but takes up the entire width and the text is centered.
My current layout file is this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:orientation="vertical"
android:gravity="center_vertical"
tools:context="com.example.mp.MainActivity$PlaceholderFragment" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/edittext1"
android:layout_width="250dip"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="NEW"
android:textColor="#444444"
android:paddingLeft="5dip"
android:textSize="10pt" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/edittext2"
android:layout_width="250dip"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/textview2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="NEW"
android:textColor="#444444"
android:paddingLeft="5dip"
android:textSize="10pt" />
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="5dip"
android:gravity="bottom">
<TextView
android:id="#+id/textview3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Version 1.0"
android:textColor="#444444"
android:paddingLeft="5dip"
android:textSize="10pt" />
</RelativeLayout>
</LinearLayout>
But the two rows of EditText and TextView are positioned past the middle of the screen and the Version textview is immediately below the two rows of EditText and TextView instead of anchored at the bottom.

I would recommend using a RelativeLayout for the root layout and use the property android:layout_alignParentBottom="true" for the "version". Then wrap the other Views in a LinearLayout. Something like
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:orientation="vertical"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
<EditText
android:id="#+id/edittext1"
android:layout_width="0dp"
android:layout_weight="3"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/textview1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="NEW"
android:textColor="#444444"
android:paddingLeft="5dip"
android:textSize="10pt" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="#+id/edittext2"
android:layout_width="0dp"
android:layout_weight="3"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/textview2"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="wrap_content"
android:text="NEW"
android:textColor="#444444"
android:paddingLeft="5dip"
android:textSize="10pt" />
</LinearLayout>
</LinearLayout>
<TextView
android:id="#+id/textview3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Version 1.0"
android:textColor="#444444"
android:paddingLeft="5dip"
android:textSize="10pt"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
I haven't had a chance to really test it but this should get you pretty close. I wrapped the two sets of Views in the center in a LinearLayout then gave each their own LinearLayout so they can make use of weight (which you can adjust to your needs). The parent LinearLayout is centered vertical so that those 4 Views will be centered the way I believe you want.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">//hope this will work for you
<LinearLayout
android:id="#+id/alltextfield"
android:layout_width="wrap_content"
android:layout_height="wrap_content"`enter code here`
android:orientation="horizontal" >
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="EditText" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New" />
</LinearLayout>
<LinearLayout
android:id="#+id/alltextfield"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="EditText" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New" />
</LinearLayout>

Related

How to add text above it?

I'm making a layout:
Here is a code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#373c5c"
android:gravity="center_vertical"
android:orientation="vertical" >
<LinearLayout android:id="#+id/list_offer_item_container"
android:layout_marginTop="15dp"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/shape"
android:orientation="horizontal"
android:padding="10dp">
<ImageView
android:id="#+id/imgcreditcompany"
android:layout_width="36dp"
android:layout_marginRight="4dp"
android:layout_marginTop="0dp"
android:layout_marginBottom="0dp"
android:contentDescription="#string/app_name"
android:layout_height="36dp" />
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:gravity="start"
android:orientation="vertical" >
<TextView
android:id="#+id/txtname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginTop="1dp"
android:textColor="#48899f"
android:textSize="#dimen/textsizeearncredit_title"
android:text="Name"
android:textAllCaps="false"
android:textStyle="normal|bold" />
<TextView
android:id="#+id/txtdesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginTop="1dp"
android:maxLines="1"
android:textColor="#80869c"
android:textSize="#dimen/textsizeearncredit_desc"
android:text="This is a description of the offer and this is just a demo to show off 3 lines stacking correctly on top of each other"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginRight="20dp"
android:gravity="end"
android:layout_gravity="center_vertical">
<TextView android:id="#+id/list_offer_badge_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/list_offer_item_container"
android:textColor="#color/md_amber_700"
android:textSize="12sp"
android:visibility="gone"
android:text=""/>
</LinearLayout>
<ImageView
android:id="#+id/nextArrow"
android:layout_alignParentRight="true"
android:tint="#color/md_grey_300"
android:rotation="180"
android:layout_width="32dp"
android:visibility="gone"
android:layout_marginRight="16dp"
android:layout_gravity="center"
android:layout_centerVertical="true"
android:layout_height="32dp"
android:contentDescription="#string/app_name"
android:padding="#dimen/two" />
</LinearLayout>
</RelativeLayout>
How to add an text above the 'button style' linear layout?
I want for example "Hello dear user! Check it below".
Where put textview, where add more layout or whatever?
It is very simple!
You have to add TextView, with id (text, for example) and, in your LinearLayout you need to add line android:layout_below="#+id/text". Optional, you can specify attribute android:layout_alignParentTop="true" in TextView.
Just use code below:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#373c5c"
android:gravity="center_vertical"
android:orientation="vertical" >
<TextView
android:id="#+id/text"
android:text="Text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
/>
<LinearLayout android:id="#+id/list_offer_item_container"
android:layout_below="#+id/text"
android:layout_marginTop="15dp"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/shape"
android:orientation="horizontal"
android:padding="10dp">
<ImageView
android:id="#+id/imgcreditcompany"
android:layout_width="36dp"
android:layout_marginRight="4dp"
android:layout_marginTop="0dp"
android:layout_marginBottom="0dp"
android:contentDescription="#string/app_name"
android:layout_height="36dp" />
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:gravity="start"
android:orientation="vertical" >
<TextView
android:id="#+id/txtname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginTop="1dp"
android:textColor="#48899f"
android:textSize="#dimen/textsizeearncredit_title"
android:text="Name"
android:textAllCaps="false"
android:textStyle="normal|bold" />
<TextView
android:id="#+id/txtdesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginTop="1dp"
android:maxLines="1"
android:textColor="#80869c"
android:textSize="#dimen/textsizeearncredit_desc"
android:text="This is a description of the offer and this is just a demo to show off 3 lines stacking correctly on top of each other"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginRight="20dp"
android:gravity="end"
android:layout_gravity="center_vertical">
<TextView android:id="#+id/list_offer_badge_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/list_offer_item_container"
android:textColor="#color/md_amber_700"
android:textSize="12sp"
android:visibility="gone"
android:text=""/>
</LinearLayout>
<ImageView
android:id="#+id/nextArrow"
android:layout_alignParentRight="true"
android:tint="#color/md_grey_300"
android:rotation="180"
android:layout_width="32dp"
android:visibility="gone"
android:layout_marginRight="16dp"
android:layout_gravity="center"
android:layout_centerVertical="true"
android:layout_height="32dp"
android:contentDescription="#string/app_name"
android:padding="#dimen/two" />
</LinearLayout>
</RelativeLayout>
Above
<LinearLayout android:id="#+id/list_offer_item_container"
put your TextView or whatever you want. So something like
<TextView android:id="#+id/myTV"
// other attributes />
Then in
<LinearLayout android:id="#+id/list_offer_item_container"
add android:layout_below="#id/myTV"
By default, RelativeLayout puts it's elements at the top-left until you specify otherwise. So adding this will put your LinearLayout below the TextView.
I think the confusion is on how RelativeLayout and LinearLayout work. You have orientation in your RelativeLayout which does nothing. RelativeLayout puts it's elements in the order you tell it by the attributes such as layout_below and layout_above. LinearLayout, however, adds them in the order in which they show in the xml file which is why orientation matters in LinearLayout but not RelativeLayout.
You'll want to look at RelativeLayout.LayoutParams and other parts of the documentation to better understand how the layouts work and which is best in your specific cases.

Android Scrollview doesn't wrap child

Here's the Layout xml file:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:scrollbars = "vertical">
<LinearLayout
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="factory.Settings"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/lblShopID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/ShopId"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/txtShopID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="text"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/lblShopname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/lblShopID"
android:layout_marginTop="36dp"
android:text="#string/ShopName"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Spinner
android:id="#+id/spinShopName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/lblShopname"
android:layout_toRightOf="#+id/lblShopname" />
</LinearLayout>
<Button
android:id="#+id/btnSaveChanges"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/spinShopName"
android:layout_marginTop="22dp"
android:onClick="saveData"
android:text="Save Changes" />
<TextView
android:id="#+id/txtChangePassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:text="#string/ChangePassword"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginTop="30px" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30px">
<TextView
android:id="#+id/lblOldPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/OldPassword"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/txtOldPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/lblNewPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/NewPassword"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/txtNewPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/lblRetypePassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/RetypePassword"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/txtRetypePassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword" />
</LinearLayout>
<Button
android:id="#+id/btnChangePassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="changePassword"
android:text="Change Password" />
<ListView
android:id="#+id/lstUsers"
android:layout_width="340dp"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
And Here's the expecting output:
The expected output is : I have some items in lstUsers and there wouldn't be enough space for displaying all the users, so I would like to add a scrollview to the Layout to let users scroll down.
But the problem is: the scrollview doesn't wrap any controls at all, the outcome is:
As you can see, the scrollview is unable to wrap the lstUser
Is there anything wrong with the layout file?
I don't think you are allowed to put a listview in a scrollview. Listview implements its own scroller so it doesn't like being in a scrollview. Take a look at this link
How can I put a ListView into a ScrollView without it collapsing?
Scrollview and Listview do not work well with each other.
What I would suggest u is to make 'lstUsers' Listview the main content of the layout file.
Create a separate layout file with the views that are above the listview.
Then inflate this layout file and add it as a header view to the listview.

In a relative layout how can I make the last element adjust to the increase of data in the preceding elements?

I have a layout as follows. The problem I have is that if the TextView of friend_name is very long it “pushes” down the rest and they overlap/cover last ViewStub of friend_last_msg
How can I make the last ViewStub to also “adjust” on this “lowering of the other widgets?
Or else how could I make that last view stub to be inside the LinearLayout and displayed to the left side and down?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/parent_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
>
<ImageView
android:id="#+id/friend_img”
android:layout_width="#dimen/friend_big”
android:layout_height="#dimen/friend_big”
android:layout_alignParentLeft="true"
android:scaleType="centerCrop"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="8dp"
android:layout_toRightOf="#+id/friend_img"
>
<TextView
android:id="#+id/friend_name”
android:layout_toRightOf="#id/friend_img"
android:layout_marginBottom="#dimen/layout_small_padding"
android:includeFontPadding="false"
android:paddingBottom="0dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="fill_horizontal"
android:ellipsize="end"
android:gravity="top"
/>
<ViewStub
android:id="#+id/friend_sign”
android:inflatedId="#+id/friend_sign"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout="#layout/friend_sign_layout”/>
<TextView
android:id="#+id/friend_contact”
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="1dp"
android:layout_marginBottom="1dp"
android:padding="5dp"
/>
</LinearLayout>
<ViewStub
android:id="#+id/friend_last_msg”
android:inflatedId="#+id/friend_last_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout="#layout/friend_last_msg"/>
</RelativeLayout>
First of all enter the closing commas right like this then enter the android:layout_height and android:layout_width values according to your needs.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/parent_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
>
<ImageView
android:id="#+id/friend_img[**"**]
android:layout_width="#dimen/friend_big[**"**]
android:layout_height="#dimen/friend_big"
android:layout_alignParentLeft="true"
android:scaleType="centerCrop"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="8dp"
android:layout_toRightOf="#+id/friend_img"
>
<TextView
android:id="#+id/friend_name[**"**]
android:layout_toRightOf="#id/friend_img"
android:layout_marginBottom="#dimen/layout_small_padding"
android:includeFontPadding="false"
android:paddingBottom="0dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="fill_horizontal"
android:ellipsize="end"
android:gravity="top"
/>
<ViewStub
android:id="#+id/friend_sign"
android:inflatedId="#+id/friend_sign"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout="#layout/friend_sign_layout"/>
<TextView
android:id="#+id/friend_contact"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="1dp"
android:layout_marginBottom="1dp"
android:padding="5dp"
/>
</LinearLayout>
<ViewStub
android:id="#+id/friend_last_msg"
android:inflatedId="#+id/friend_last_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout="#layout/friend_last_msg"/>
You can to use this way:
Add an id -> #+id/container to parent
<LinearLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_toRightOf="#+id/friend_img"
android:orientation="vertical">
For your stub layout just change to this:
<ViewStub
android:id="#+id/friend_last_msg"
android:inflatedId="#+id/friend_last_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/container"
android:layout_toRightOf="#+id/friend_img"
android:layout_alignParentRight="true"
android:layout="#layout/friend_last_msg"/>

LinearLayout and FrameLayout for positioning buttons

I tried to figure this out on my own for hours already and just can't make it. Here it what I am trying to do: Image of desired layout
The "blue" rectangle is the area the TextSwitcher is supposed to encompass, but I can't manage to put the "Redo Set" directly above the "Previous Item" and the "Next Set" directly above the "Next Item" button. Here is my layout xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Spinner
android:id="#+id/spinner_1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Spinner
android:id="#+id/spinner_2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Spinner
android:id="#+id/spinner_3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal" >
<TextSwitcher
android:id="#+id/textSwitcherMain"
android:gravity="center"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="40sp"
android:keepScreenOn="true"
android:text="" >
</TextSwitcher>
<Button
android:id="#+id/problematicButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|top"
android:visibility="visible"
android:text="#string/problematic" />
<Button
android:id="#+id/playButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|top"
android:visibility="visible"
android:text="#string/play_1" />
<Button
android:id="#+id/nextSetButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|bottom"
android:visibility="visible"
android:text="#string/next_set" />
<Button
android:id="#+id/redoSetButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|bottom"
android:visibility="visible"
android:text="#string/redo_set" />
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/previousButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/previous_item_button" />
<Button
android:id="#+id/nextButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/next_item_button" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<com.google.android.gms.ads.AdView
ads:adUnitId="**************"
ads:adSize="SMART_BANNER"
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center" />
</LinearLayout>
I tried putting nextButton and previousButton in the FrameLayout, but then they overlap with redoSetButton and nextSetButton respectively. Also, nextButton and previousButton should each take up 50% of the width, so I somewhat have to put it into a LinearLayout and I tried putting various combinations inside the FrameLayout, but I can't manage to position those buttons in this way.
Furthermore, it's important that all those buttons are on top of the TextSwitcher, because it will have a background image/colouring that should be visible behind the buttons.
This is my first posting. I hope I was detailed enough. Thanks for your input in advance.
Try this way,hope this will help you to solve your problem.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Spinner
android:id="#+id/spinner_1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Spinner
android:id="#+id/spinner_2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Spinner
android:id="#+id/spinner_3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal" >
<TextSwitcher
android:id="#+id/textSwitcherMain"
android:gravity="center"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="40sp"
android:keepScreenOn="true"
android:text="" >
</TextSwitcher>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top">
<Button
android:id="#+id/problematicButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/problematic" />
<Button
android:id="#+id/playButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/play_1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom">
<Button
android:id="#+id/nextSetButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/next_set" />
<Button
android:id="#+id/redoSetButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/redo_set" />
</LinearLayout>
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/previousButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/previous_item_button" />
<Button
android:id="#+id/nextButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/next_item_button" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<com.google.android.gms.ads.AdView
ads:adUnitId="**************"
ads:adSize="SMART_BANNER"
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center" />
</LinearLayout>
</LinearLayout>

dynamic layout for 4 icons that must stay in a rational distance from each other in android

i have this layout for bottom bar of my app:
but i want to make it like this:
actually i can but i want that , this layout works in every screen size. in fact, left icon sticks to left, right icon sticks to right, two center icon stay at center but all of them stay with a nice distance to each other. i don't use absolute values and i want this icons dynamically change his distance from others. my problem is how to layout them to achieve my goal.
this is my xml for this section:
<LinearLayout
android:id="#id/bar_l2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:gravity="center_horizontal" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bottom_bar" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/bar1" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/bar2" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/bar3" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="#drawable/bar4" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
This is what you need to do :
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal" xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/bottom_bar"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:padding="30dp" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/bar1" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/bar2" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/bar3" />
<ImageView
android:id="#+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="#drawable/bar4" />
</LinearLayout>
What I have done here is set the background of parent LinearLayout as the background image and its orientation as horizontal.Most importantly its width as fill_parent .
Hence it will cover the entire width of the parent .Padding will help you add desired spacing between image views.
To make it fit all screen size properly instead of padding use android:weight attribute .
Hope it helps !
This works fine
<LinearLayout
android:id="#id/bar_l2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:gravity="center_horizontal" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bottom_bar" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_gravity="center"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/bar1" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_gravity="center"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/imageView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/bar2" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_gravity="center"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/imageView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/bar3" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_gravity="center"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/imageView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="#drawable/bar4" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</LinearLayout>

Categories