Uneven cell heights in final row of GridLayoutManaged RecylerView - java

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>

Related

linear layout not filling parent down the remaining part of the screen in a scrollview

I am designing a layout for an activity where some contents like Textview, spinner and buttons are arranged in the upper part of the screen. I want to position image view to the bottom of the screen with a match_parent attribute to fill to the remaining bottom of the screen and it has become a challenge to me as the image view is filling to the bottom of the remaining part of the screen. This is the entire layout of the upper section/part of the screen
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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" >
<ScrollView
android:layout_width="fill_parent"
android:background="#drawable/xxxVal"
android:padding="10dp"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:id="#+id/dataVaues"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textSize="25sp"
android:gravity="center_horizontal"
android:textColor="#000"
/>
<Spinner
android:id="#+id/spinBundle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="6sp"
android:textColor="#000"
android:prompt="#string/_prompt" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textColor="#000"
android:text="Plan" />
<TextView
android:id="#+id/bundle"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:textSize="30sp"
android:textColor="#000"/>
<!-- android:inputType="phone" -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textColor="#000"
android:text="PValue:" />
<TextView
android:id="#+id/value"
android:inputType="value"
android:layout_height="wrap_content"
android:textSize="30sp"
android:layout_width="match_parent"
android:textColor="#000"/>
<TextView
android:id="#+id/validValue"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Valid:"
android:textColor="#000000" />
<TextView
android:id="#+id/valid"
android:inputType="valid"
android:layout_height="wrap_content"
android:textSize="30sp"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:textColor="#ff0000"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical" >
<Button
android:id="#+id/proceed"
android:text="Proceed"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:textColor="#000"/>
<Button
android:id="#+id/data"
android:text="Check"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_marginTop="10dp"
android:textColor="#000"/>
</LinearLayout>
this is remaining part of the layout with a view flipper for an image that I want it to fit down to the remaining part of the screen but its not fitting as it shrinks up
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="3dp"
android:gravity="center_horizontal"
android:orientation="vertical" >
<ViewFlipper
android:id="#+id/flipper1"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:flipInterval="5000"
android:inAnimation="#anim/slide_in_right"
android:outAnimation="#anim/slide_out_left" >
</ViewFlipper>
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
please kindly assist!
Try adding the imageview to the Viewflipper like so:
<ViewFlipper
android:id="#+id/simpleViewFlipper"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src=IMAGE />
</ ViewFlipper >
Having said that you might find some issues regarding swiping when using a viewflipper inside a scrollview

Stick header at top when Softkeyboard is opened android

I have a layout, in which I need that the top header should should not hide when I open softKeyBoard. I am using Fragment here.I am using recylerView.
when i am opening softkeyboard the top header is scrolling up. I need that header to stay there and Recyclerview to scroll.
Below is my xml.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rel_parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bggradient">
<RelativeLayout
android:id="#+id/rel_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:background="#drawable/drawable_below_line">
<ImageView
android:id="#+id/iv_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/txtSettingHeader"
android:layout_alignTop="#+id/txtSettingHeader"
android:layout_centerVertical="true"
android:background="#drawable/button_selector"
android:onClick="onClickEvent"
android:padding="10dp"
android:src="#mipmap/ic_back" />
<TextView
android:id="#+id/txtUserName"
style="#style/FragHeader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" />
<ImageView
android:id="#+id/iv_userImage"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/txtUserName"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/txtUserName"
android:layout_centerVertical="true"
android:onClick="onViewClick"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:src="#mipmap/ic_male" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/lin_comment"
android:layout_below="#+id/rel_header"
android:gravity="top"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/RecyelerViewChat"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible">
</android.support.v7.widget.RecyclerView>
<TextView
android:id="#+id/tv_txtNoRecords"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:minHeight="100dp"
android:text="#string/txtNoChat"
android:textColor="#android:color/white"
android:visibility="gone" />
</LinearLayout>
<LinearLayout
android:id="#+id/lin_comment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#70FFFFFF"
android:gravity="center"
android:orientation="horizontal"
android:padding="5dp">
<LinearLayout
android:id="#+id/linEditComment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:focusableInTouchMode="true"
android:orientation="horizontal">
<ImageView
android:id="#+id/iv_imgToggleKeyboardEmoji"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:clickable="true"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:src="#mipmap/ic_emoji" />
<com.lawo.emojicon.EmojiconEditText
android:id="#+id/et_edtChat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/drawable_black_border"
android:hint="#string/hintTypeMessage"
android:inputType="textNoSuggestions|textMultiLine"
android:maxLength="500"
android:maxLines="3"
android:padding="10dp"
android:textColor="#android:color/black"
android:textColorHint="#android:color/darker_gray" />
</LinearLayout>
<ImageView
android:id="#+id/iv_imgSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:onClick="onClickEvent"
android:padding="10dp"
android:src="#mipmap/ic_sent" />
</LinearLayout>
</RelativeLayout>
Add the following to your "activity" tag in AndroidManifest.xml:
android:windowSoftInputMode="adjustResize"
Reference: Specify How Your UI Should Respond.

How to align title,description and url in equal space

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>

Center button in a LinearLayout

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>

Android LinearLayout place elements in the middle with landscape and portrait mode

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

Categories

Resources