I just want to center "CenterButton" (center of the root LinearLayout) but Androids just not cooperating..
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_gravity="center_horizontal"
android:layout_weight="0.00"
android:padding="5dp"
android:background="#zegerg"
android:id="#+id/LinearLayout1">
<ImageView
android:id="#+id/ImageView1"
android:layout_height="match_parent"
android:layout_width="151dp"
android:src="#mipmap/image_1"
android:contentDescription="#string/abcdef"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:id="#+id/CenterButton"
android:layout_gravity="center_horizontal|center_vertical|center"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text !"
android:id="#+id/TextView"
android:paddingBottom="30dp"
android:textColor="#37a52c" />
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/ProgressBar"
android:paddingBottom="30dp"
android:progressTint="#37a52c" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/text_text"
android:id="#+id/Button2"
android:layout_gravity="center_horizontal" />
</LinearLayout>
</LinearLayout>
Tried putting it at the end of the XML but doesn't work because the other elements push it aside.. not really sure what's happening.
if you want to have all three first level children to be in equally-sized columns, then you need to add to all of them the followings attribute:
android:layout_width="0dp"
android:layout_weight="1"
(don't forget the dimension "dp" to the width parameter, otherwise the IDE will complain).
...
<ImageView
android:id="#+id/ImageView1"
android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:src="#mipmap/image_1"
android:contentDescription="#string/abcdef"/>
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Button"
android:id="#+id/CenterButton"
android:layout_gravity="center_horizontal|center_vertical|center"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:padding="20dp">
....
EDITED:
If you want your CenterButton to overlap all the other views, then what you have to do is:
wrap everything in a relative layout with height equal to 200dp, as its first child
remove the button CenterButton from the current position and add it at the bottom of everything, still inside the relative layout, but below the linear layout
add those properties to the button to make it center in the parent
android:layout_centerInParent="true"
So finally you will have something like this (I have changed some resources):
<?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="200dp">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_gravity="center_horizontal"
android:layout_weight="0.00"
android:padding="5dp"
android:background="#android:color/holo_orange_dark"
android:id="#+id/LinearLayout1">
<ImageView
android:id="#+id/ImageView1"
android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:src="#drawable/plus"
android:contentDescription="abcdef"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:padding="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text !"
android:id="#+id/TextView"
android:paddingBottom="30dp"
android:textColor="#37a52c" />
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/ProgressBar"
android:paddingBottom="30dp"
android:progressTint="#37a52c" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text"
android:id="#+id/Button2"
android:layout_gravity="center_horizontal" />
</LinearLayout>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Button"
android:id="#+id/CenterButton"
android:layout_gravity="center_horizontal|center_vertical|center"/>
</RelativeLayout>
You can use weight for such purposes for child items. Make layout_width for all of them as 0dp and set layout_weight as 1.
Give the three children in the topmost linear layout a gravity of 1 and a width of 0.
It's not clear what exactly is your problem. However, here are some general notes :
To center a View or a Layout inside RelativeView use layout_centerInParent="true"
To make a Layout on top of other layouts use FrameLayout
The order of the views/layouts in your xml file is important. The one you write the last will be on the top (in our example : the FrameLayout or just the CenterButton should be written after the other views and layouts in your xml file)
To distribute the available space inside LinearLayout among its views use android:layout_weight="1" (change the value according to your needs)
you can play aground with this example :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="200dp">
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#fff"
android:orientation="horizontal"
android:padding="5dp">
<ImageView
android:id="#+id/ImageView1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:src="#mipmap/ic_launcher" />
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:orientation="vertical">
<TextView
android:id="#+id/TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="30dp"
android:text="Text !"
android:textColor="#37a52c" />
<ProgressBar
android:id="#+id/ProgressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="30dp"
android:progressTint="#37a52c" />
<Button
android:id="#+id/Button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text_text" />
</LinearLayout>
</LinearLayout>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true">
<Button
android:id="#+id/CenterButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical|center"
android:text="Button" />
</FrameLayout>
</RelativeLayout>
Related
I have a recycler view that can have it's layout manager changed with the click of a button. It can cycle through some LinearLayout managed stuff and some GridLayout managed items. They both work fine, but the on the grid views, the final row always has uneven heights.
Not the end of the world, but I'd like to fix it.
The cells have dynamic height based on the content length of the title of the game.
images
Many users with similar problems were due to constraint layouts, but I don't have any in these particular views
Here is one of the XMLs that represents my grid cell.
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/cell_top_bottom_marginDiv2"
android:layout_marginBottom="#dimen/cell_top_bottom_marginDiv2"
android:layout_marginLeft="#dimen/cell_side_marginDiv2"
android:layout_marginRight="#dimen/cell_side_marginDiv2">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:paddingTop="#dimen/cell_top_bottom_paddingDiv2"
android:paddingBottom="#dimen/cell_top_bottom_paddingDiv2"
android:paddingLeft="#dimen/cell_side_paddingDiv2"
android:paddingRight="#dimen/cell_side_paddingDiv2"
android:id="#+id/large_game_list_row_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/foreground" >
<FrameLayout
android:padding="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include layout="#layout/ownership_tag_on_cell"
android:id="#+id/ownershipTagLayout"
android:layout_gravity="center_vertical|end"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</include>
<!--Console Image and Name-->
<LinearLayout
android:layout_gravity="center_vertical|start"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/consoleArtImageView"
android:scaleType="fitCenter"
android:contentDescription="Console Art"
android:layout_width="#dimen/list_item_console_decorator_image_box_dimension"
android:layout_height="#dimen/list_item_console_decorator_image_box_dimension" />
<View
android:layout_width="#dimen/cell_element_spacingDiv2"
android:layout_height="0dp" />
<TextView
android:textColor="#color/default_text_color"
android:id="#+id/consoleNameTextView"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="name"
android:textSize="#dimen/list_item_console_decorator_image_text_size" />
</LinearLayout>
</FrameLayout>
<View
android:layout_gravity="center_horizontal"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1.0" />
<!--Box art-->
<ImageView
android:src="#drawable/list_item_placeholder"
android:id="#+id/boxArtImageView"
android:layout_width="match_parent"
android:layout_height="#dimen/list_item_medium_image_box_dimension"
android:layout_gravity="center_horizontal"
android:layout_marginEnd="2.5dp"
android:layout_marginLeft="2.5dp"
android:layout_marginRight="2.5dp"
android:layout_marginStart="2.5dp"
android:contentDescription="BoxArt"
android:gravity="center_vertical"
android:scaleType="fitCenter"
android:adjustViewBounds="true" />
<!--Metadata (title, developer, action button)-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/firstLineTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
tools:text="Title"
android:layout_gravity="center_horizontal"
android:textColor="#color/default_text_color"
android:textSize="#dimen/cell_small_grid_title_text_size"
android:textStyle="bold"
android:paddingTop="#dimen/cell_top_bottom_padding" />
<TextView
android:id="#+id/firstSubLineTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#color/default_text_color"
android:textSize="#dimen/cell_subtitle_text_size"
android:maxLines="1"
tools:text="Dev"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
<View
android:layout_gravity="center_horizontal"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1.0" />
<LinearLayout
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingBottom="#dimen/cell_top_bottom_padding"
android:paddingTop="#dimen/cell_top_bottom_padding">
<TextView
android:id="#+id/statusTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="#dimen/cell_element_spacingDiv2"
android:background="#drawable/ownership_bubble_background"
android:maxLines="1"
android:paddingBottom="#dimen/bubble_top_bottom_padding"
android:paddingLeft="#dimen/bubble_side_padding"
android:paddingRight="#dimen/bubble_side_padding"
android:paddingTop="#dimen/bubble_top_bottom_padding"
tools:text="status"
android:textColor="#color/bubble_ownership_text_color"
android:textSize="#dimen/bubble_text_size" />
<TextView
android:id="#+id/priceTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/price_bubble_background_gradient"
android:maxLines="1"
android:paddingBottom="#dimen/bubble_top_bottom_padding"
android:paddingLeft="#dimen/bubble_side_padding"
android:paddingRight="#dimen/bubble_side_padding"
android:paddingTop="#dimen/bubble_top_bottom_padding"
tools:text="Price"
android:textColor="#color/bubble_price_text_color"
android:textSize="#dimen/bubble_text_size" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
I am trying to put two TextViews next to each other but somehow I can't get it to work. I wanted to have the following: txtViewAddress: txtAddress
I've tried putting android:layout_toRightOf="#id/txtViewAddress"
I also tried putting layout_toLeftOf, but both did not work.
Hope you guys can help me out. Thanks in advance.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/CalendarID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium"
android:visibility="gone" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="480dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/txtViewAddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Address: "
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="22dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/txtAddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="#id/txtViewAddress"
android:text="Hello"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="22dp"
android:textStyle="bold"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:gravity="right"
android:orientation="horizontal">
<Button
android:id="#+id/btn_update"
android:layout_width="45dp"
android:layout_height="35dp"
android:layout_marginRight="5dp"
android:layout_marginTop="100dp"
android:background="#drawable/edit"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="" />
<Button
android:id="#+id/btn_delete"
android:layout_width="38dp"
android:layout_height="24dp"
android:layout_marginRight="5dp"
android:layout_marginTop="100dp"
android:background="#drawable/delete"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="" />
</LinearLayout>
</LinearLayout>
If you need views inside a linear layout next to each other, the orientation of your layout must be horizontal.
You can also use the constraint layout, it has much more flexibility in placing view on your screen.
android:layout_toRightOf="#id/txtViewAddress" is atrribute part of Relative Layout
Use this code
<LinearLayout
android:layout_width="480dp"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/txtViewAddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Address: "
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="22dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/txtAddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="#id/txtViewAddress"
android:text="Hello"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="22dp"
android:textStyle="bold"/>
</LinearLayout>
<LinearLayout
android:layout_width="480dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:id="#+id/txtViewAddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Address: "
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="22dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/txtAddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="#id/txtViewAddress"
android:layout_weight="1"
android:text="Hello"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="22dp"
android:textStyle="bold"/>
</LinearLayout>
change the android:orientation="vertical" to android:orientation="horizontal"
and you can also use android:weightSum="2" weightSum is used to define the priority of the view
if you give equal weight to all views inside linear layout then all the views take on equal amount of space inside the Linear Layout
this should be like following
<LinearLayout
android:layout_width="480dp"
android:layout_height="wrap_content"
android:orientation="horizontal">
.....
......
.....
</LinearLayout>
and also I suggest that do not give fix width to layout instead you set it releted to screen size
Here is a screenshot of layout as it currently appears on my screen:
Here is the xml code snippet for the layout:
<?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="match_parent">
<ImageView
android:id="#+id/icon"
android:layout_width="110dp"
android:layout_height="110dp"
android:layout_alignParentRight="true"
android:layout_marginRight="-10dp" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:height="20dp"
android:fontFamily="Cabin-Regular"
android:paddingLeft="10dp"
android:textColor="#E35B5B"
android:textSize="14dp" />
<TextView
android:id="#+id/desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/title"
android:height="44dp"
android:fontFamily="Cabin-Regular"
android:paddingLeft="10dp"
android:paddingRight="100dp"
android:textColor="#2D2D2D"
android:textSize="16dp" />
<TextView
android:id="#+id/url_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/title"
android:layout_marginTop="50dp"
android:height="15dp"
android:paddingLeft="10dp"
android:paddingRight="80dp"
android:textColor="#BDBDBD"
android:textSize="10dp" />
</RelativeLayout>
Just want to have the three textviews properly aligned and have equal space among each other.
Here is your layout,
<?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="match_parent">
<ImageView
android:id="#+id/icon"
android:layout_width="110dp"
android:layout_height="110dp"
android:layout_alignParentRight="true"
android:layout_marginRight="-10dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="110dp"
android:layout_toLeftOf="#+id/icon"
android:orientation="vertical"
android:weightSum="3">
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:fontFamily="Cabin-Regular"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:text="Title"
android:textColor="#E35B5B"
android:textSize="14dp" />
<TextView
android:id="#+id/desc"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="#+id/title"
android:layout_weight="1"
android:fontFamily="Cabin-Regular"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:text="Description"
android:textColor="#2D2D2D"
android:textSize="16dp" />
<TextView
android:id="#+id/url_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="#+id/title"
android:layout_weight="1"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:text="URL"
android:textColor="#BDBDBD"
android:textSize="10dp" />
</LinearLayout>
</RelativeLayout>
Pawan Thakur Use the same paddingLeft for all test and layoutBelow like below
<TextView
android:id="#+id/url_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/desc"
android:height="15dp"
android:paddingRight="80dp"
android:paddingLeft="10dp"
android:text="url"
android:textColor="#BDBDBD"
android:textSize="10dp" />
here is the code. Try maximum to use LinearLayout instead of Relative. Relative layout are used in cases where 2 or more elements have to be overlapped each other. Don't include every elements in a single layout. Divide your design into different sections, and use LinearLayout for each section and within each section, include elements belonging to that section.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight=".25"
android:orientation="vertical"
>
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="#E35B5B"
android:fontFamily="Cabin-Regular"
android:textSize="14dp"
android:layout_weight="0.33"
android:singleLine="true"
android:ellipsize="end"
android:gravity="center|left"
android:paddingLeft="10dp"
android:text="Google"
/>
<TextView
android:id="#+id/desc"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="#2D2D2D"
android:fontFamily="Cabin-Regular"
android:textSize="16dp"
android:layout_weight="0.33"
android:singleLine="true"
android:ellipsize="end"
android:gravity="center|left"
android:paddingLeft="10dp"
android:text="Google is a Search engine"
/>
<TextView
android:id="#+id/url_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/title"
android:textColor="#BDBDBD"
android:textSize="10dp"
android:layout_weight="0.33"
android:singleLine="true"
android:ellipsize="end"
android:gravity="center|left"
android:paddingLeft="10dp"
android:text="http://www.google.com"
/>
</LinearLayout>
<LinearLayout
android:layout_width="110dp"
android:layout_height="110dp"
>
<ImageView
android:id="#+id/icon"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
</LinearLayout>
u can use a android:weight="" to child of relative layout
Create LinearLayout for title, description and url with vertical orientation and weightsum=3 in Layout and for each of them use width=wrap_content, height=0dp, weight=1.
Okey.. First remove android:height from each TextView and in last TextView you have set android:layout_marginTop="50 remove it.
Tip : Use LinearLayout if your want to show your all view vertically or horizontally. if you want to use RelativeLayout make sure you give margin and padding properly so they look good.
Use Linear layout as parent with weightsum and equally distribute weight amongs the child layouts. ex : If android:weightsum = "3" for parent then android:layout_weight = "1" for each child layout(if you want equal space for each one of the 3 child).
ps: dont forget to mention orientation for Linearlayout.
Just Check this,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1
android:padding="10dp"
android:orientation="vertical">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#E35B5B"
android:fontFamily="Cabin-Regular"
android:textSize="14dp"
android:text="Google"/>
<TextView
android:id="#+id/desc"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="#2D2D2D"
android:fontFamily="Cabin-Regular"
android:textSize="16dp"
android:margin_top="5dp"
android:text="Google is a Search engine"/>
<TextView
android:id="#+id/url_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="#BDBDBD"
android:textSize="10dp"
android:margin_top="5dp"
android:text="http://www.google.com"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"/>
</LinearLayout>
</LinearLayout>
My layout contains three buttons, one EditText field and one button. I want to place the three buttons in one row, but in the center (horizontal) of the screen. The EditText field should be below the buttons and below the EditText field, there should be one button.
Here is my layout:
<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:background="#color/blueAct"
android:orientation="vertical"
android:paddingBottom="100.0dip"
android:paddingLeft="10.0dip"
android:paddingRight="10.0dip"
android:paddingTop="100.0dip" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingBottom="4.0dip"
android:paddingLeft="100.0dip"
android:paddingRight="100.0dip"
android:paddingTop="5.0dip" >
<ImageButton
android:id="#+id/buttonGreen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginRight="4.0dip"
android:background="#drawable/green_button"
android:contentDescription="#string/green" />
<ImageButton
android:id="#+id/buttonRed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginRight="4.0dip"
android:background="#drawable/red_button"
android:contentDescription="#string/red" />
<ImageButton
android:id="#+id/buttonBlue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginRight="4.0dip"
android:background="#drawable/blue_button"
android:contentDescription="#string/blue" />
</LinearLayout>
<EditText
android:id="#+id/phone_code"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/phone_code"
android:paddingTop="100.0dip"
android:textColor="#color/white"
android:textColorHint="#color/white" >
<requestFocus />
</EditText>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:background="#color/orangeAct"
android:onClick="searchUsers"
android:paddingTop="100.0dip"
android:text="#string/button_go"
android:textColor="#color/white" />
</LinearLayout>
Now the problem is, that the three buttons are not in the center if the user changes from portrait to landscape mode.
How is it possible to make this 'responsive', without creating a separate layout file for landscape mode?
Any help would be greatly appreciated.
Don't use padding on your horizontal LinearLayout to center the views. Instead you can add views before and after that expand to fill the remaining space.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />
<ImageButton
android:id="#+id/buttonGreen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
... />
<ImageButton
android:id="#+id/buttonRed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
... />
<ImageButton
android:id="#+id/buttonBlue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
... />
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="#mipmap/background"
android:orientation="vertical"
android:id="#+id/homemain124"
android:weightSum="3">
<RelativeLayout
android:id="#+id/paneltamrin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="something"
android:textColor="#fff"
android:id="#+id/top1"
android:gravity="center_horizontal"
android:layout_marginTop="10dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="40dp"
android:layout_marginLeft="40dp"
android:padding="8dp"
android:background="#drawable/textcorner"
android:layout_below="#+id/top1"
android:text="something"
android:textSize="20sp"
android:textColor="#fff"
android:id="#+id/top2"
android:fontFamily="monospace"
android:textStyle="bold"
android:gravity="center_horizontal"
android:layout_marginTop="30dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/top2"
android:text="APPLIED BY"
android:textSize="15sp"
android:id="#+id/top3"
android:textColor="#fff"
android:gravity="center_horizontal"
android:layout_marginTop="30dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/top3"
android:text="COURT OF JUSTICE IN INDIA"
android:id="#+id/top4"
android:textSize="15sp"
android:textColor="#fff"
android:textStyle="bold"
android:gravity="center_horizontal"
android:layout_marginTop="10dp"/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/paneltamrin2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center">
<ImageView
android:layout_width="220dp"
android:layout_height="160dp"
android:src="#mipmap/book1"
android:id="#+id/image1"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/paneltamrin3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="READ NOW"
android:textColor="#fff"
android:lineSpacingExtra="10dp"
android:textSize="20sp"
android:layout_marginTop="10dp"
android:layout_marginBottom="15dp"
android:id="#+id/buttonorder"
android:padding="20dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#drawable/orderbutton"
android:layout_centerHorizontal="true"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#fff"
android:textSize="13sp"
android:layout_marginTop="10dp"
android:gravity="center_horizontal"
android:layout_below="#+id/buttonorder"
android:text="something"/>
</RelativeLayout>
</LinearLayout>
Linear layout added for 3 relative layouts
I've got an example layout that I'm trying to create in android using xml. I am able to create similar layouts but I feel as if my approach might be wrong.
What I have been doing in these cases is nesting relative layouts to act as a "row". The below image demonstrates what I would do.
How would you guys create a layout similar to this? I feel as if nesting relative layouts is overkill but I'm not sure how I can keep everything centred if I don't.
When I didn't nest the layouts I used
android:layout_toRightOf="..."
android:layout_below="#+id/t1"
on t5, t6 and t7 (from the image). The result looked incorrect. t1, t2, t3 and t4 wasn't centred horizontally any more.
Is there a way to like tell the relative layout that everything after this point should appear on a new row? Or is relative layouts the wrong way to go for things like this? I don't think a table layout would work correctly since each row doesn't necessarily need to have the same amount of views and they need to be centred.
Any suggestions appreciated!
You could nest two horizontal LinearLayouts within a vertical LinearLayout. Maybe not so efficient as a RelativeLayout, but easier to get the centering behavior you want.
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C" />
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="D" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<Button
android:id="#+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="E" />
<Button
android:id="#+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="F" />
<Button
android:id="#+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="G" />
</LinearLayout>
</LinearLayout>
Here's another version of the same layout using RelativeLayout. I started with the nested LinearLayouts above, selected "Refactor > Android > Change Layout...", selected RelativeLayout, and then tweaked the resulting layout until I got things centered.
I used a couple of tricks to get it right. I started by centering the middle button on the parent, then built out to the left and right. On the top row, where there is an even number of buttons and space in the center, I used a centered, 0-width TextView to anchor the buttons. That's a bit hacky, I guess, but it gets the job done. :-)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:orientation="vertical" >
<TextView
android:id="#+id/TextView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="#string/hello" />
<TextView
android:id="#+id/Dummy"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_centerInParent="true"
android:layout_below="#+id/TextView1" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/Dummy"
android:layout_toLeftOf="#+id/Dummy"
android:text="B" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button2"
android:layout_alignTop="#+id/Dummy"
android:layout_toLeftOf="#+id/button2"
android:text="A" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button2"
android:layout_alignTop="#+id/Dummy"
android:layout_toRightOf="#+id/Dummy"
android:text="C" />
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button2"
android:layout_alignTop="#+id/Dummy"
android:layout_toRightOf="#+id/button3"
android:text="D" />
<Button
android:id="#+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_below="#+id/Dummy"
android:text="F" />
<Button
android:id="#+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/button6"
android:layout_alignBaseline="#+id/button6"
android:layout_toLeftOf="#+id/button6"
android:text="E" />
<Button
android:id="#+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button6"
android:layout_alignTop="#+id/button6"
android:layout_toRightOf="#+id/button6"
android:text="G" />
</RelativeLayout>
You could also try it using the weight.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="10" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="2.5"
android:gravity="center"
android:text="hello" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/button1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="A" />
<Button
android:id="#+id/button2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="B" />
<Button
android:id="#+id/button3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="C" />
<Button
android:id="#+id/button4"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="D" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/button5"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="E" />
<Button
android:id="#+id/button6"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="F" />
<Button
android:id="#+id/button7"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="G" />
</LinearLayout>
</LinearLayout>
Check this out:
<?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="match_parent"
>
<LinearLayout
android:id="#+id/l1"
android:layout_width="fill_parent"
android:layout_height="50dp"></LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#android:color/white"
android:layout_below="#+id/l1"
android:id="#+id/v1"/>
<LinearLayout
android:id="#+id/l2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#+id/v1"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp">
<Button
android:id="#+id/pos"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:background="#drawable/ic_launcher"
/>
<Button
android:id="#+id/neu"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:background="#drawable/ic_launcher"/>
<Button
android:id="#+id/neg"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:background="#drawable/ic_launcher"/>
<Button
android:id="#+id/neg"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:background="#drawable/ic_launcher"/>
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#android:color/white"
android:layout_below="#+id/l2"
android:id="#+id/v2"
/>
<LinearLayout
android:id="#+id/l3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#+id/v2"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp">
<Button
android:id="#+id/pos"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:background="#drawable/ic_launcher"
/>
<Button
android:id="#+id/neu"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:background="#drawable/ic_launcher"/>
<Button
android:id="#+id/neg"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:background="#drawable/ic_launcher"/>
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#android:color/white"
android:layout_below="#+id/l3"
android:id="#+id/v3"
/>
</RelativeLayout>
You could also try using linear layout instead. in the first linear layout set the weightsum to 4 and the layout weight to 1 to make it equal divided and set each views gravity to center.
Do the same thing in send Linear Layout.