Linear Layout negative margin , need one layout above another - android

i got this problem while giving negative margin
after giving negative margin i want that red part go below white part, to get shadow of categories on top of red box but as you see the redbox is coming on top
that white part is my relative layout and red part is my custom listview
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/dim_10"
android:layout_marginRight="#dimen/dim_10"
android:background="#drawable/stripe_sort"
android:layout_marginTop="#dimen/dim_5"
android:layout_marginBottom="-15dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="#dimen/dim_10"
android:text="Categories"
android:textColor="#ff000000"
android:textSize="13sp"
tools:ignore="HardcodedText" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:checked="true"
android:src="#drawable/sort_downarrow"
android:layout_centerVertical="true"
android:padding="#dimen/dim_15"
android:id="#+id/dropDown_categories"/>
</RelativeLayout>
<com.eminosoft.ebookread.util.ExpandableHeightListView
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_marginLeft="#dimen/dim_12"
android:layout_marginRight="#dimen/dim_12"
android:background="#c03f2f"
android:id="#+id/ListView_Sort_categories"
android:visibility="visible"
android:paddingBottom="#dimen/dim_10"
android:divider="#null"
android:layout_gravity="center_horizontal"
/>
in this i added negative margin bottom to relative layout, i also tried to add negative margin top to my custom list view but still same result , parent of these both layouts is a linear layout
how can i make that listview go below relativelayout?

<RelativeLayout
android:id="#+id/one"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.eminosoft.ebookread.util.ExpandableHeightListView
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_marginLeft="#dimen/dim_12"
android:layout_marginRight="#dimen/dim_12"
android:layout_marginTop="-15dp"
android:layout_below="#+id/partone"
android:background="#c03f2f"
android:id="#+id/ListView_Sort_categories"
android:visibility="visible"
android:paddingBottom="#dimen/dim_10"
android:divider="#null"
android:layout_gravity="center_horizontal"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/dim_10"
android:layout_marginRight="#dimen/dim_10"
android:background="#drawable/stripe_sort"
android:layout_marginTop="#dimen/dim_5"
android:id="#+id/partone">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="#dimen/dim_10"
android:text="Categories"
android:textColor="#ff000000"
android:textSize="13sp"
tools:ignore="HardcodedText" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:checked="true"
android:src="#drawable/sort_downarrow"
android:layout_centerVertical="true"
android:padding="#dimen/dim_15"
android:id="#+id/dropDown_categories"/>
</RelativeLayout>
</RelativeLayout>
You can try this code,basically we put your layouts inside another relative layout

Related

android xml udacity gap between the sides of the screen

I have this problem while using https://labs.udacity.com/android-visualizer/, I can't move the bottom buttons (from the left, bottom side, that it wouldnt be stuck to the sides of the screen but had a bit of a gap.I added the picture, those are the buttons with white background, foto ->Sides
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#2b580c">
<TextView
android:textStyle="bold"
android:text="VTMC - Mūsų nuotraukos"
android:layout_width="match_parent"
android:layout_height="70dp"
android:textColor="#android:color/white"
android:textSize="22sp"
android:background="#2b580c"
android:gravity="center"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="300dp"
android:scaleType="centerCrop"
android:src="#drawable/grass"/>
<Button
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Atgal"
android:background="#FFFFFF"
android:textSize="20sp"
android:padding="4dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
/>
<Button
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Kitas"
android:background="#FFFFFF"
android:textSize="20sp"
android:padding="4dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
/>
</LinearLayout>
From what I understand you want the buttons to be in the bottom corners yet have some space between the buttons and the corner of the screen.
Add a margin to the buttons
android:layout_margin="(size)"
If you want to customize if further you can add a margin to the right,left,bottom, or top.
Delete
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
from first button and
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
from second one.

How to position vector image in RecyclerView item template

Is there a way to adjust the alignment of the menu dots in the RecyclerView item so that it's positioned exactly underneath the overflow dots in the Toolbar (i.e. in its original place where it should be)? I tried adjusting the item weights but that didn't work.
<LinearLayout
android:id="#+id/ll_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="100">
<TextView android:id="#+id/rvitem_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:ellipsize="marquee"
android:textColor="?android:attr/textColorPrimary"
android:fadingEdge="horizontal"
android:layout_weight="90"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="More options"
android:clickable="true"
android:focusable="true"
android:layout_gravity="end"
android:src="#drawable/ic_more_vertical"
android:background="#null"
android:tint="?attr/colorOverflow"
android:layout_weight="10"/>
</LinearLayout>
<TextView android:id="#+id/rvitem_subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:ellipsize="marquee"
android:textColor="?android:attr/textColorSecondary"
android:layout_below="#id/ll_title"
android:fadingEdge="horizontal" />
Current result
Expected result
#MacaronLover, The approach you are taking to make the view align by assigning the weight seems to be in the right direction. I have taken your xml file and made some adjustments here:
<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="wrap_content"
android:orientation="horizontal"
android:paddingStart="16dp"
android:paddingTop="8dp"
android:paddingEnd="8dp"
android:paddingBottom="8dp">
<ImageView
android:id="#+id/firstImage"
android:layout_width="90dp"
android:layout_height="90dp"
tools:background="#000" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="8dp"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/titleText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
tools:text="Title A" />
<TextView
android:id="#+id/subTitleText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
tools:text="Subtitle A" />
</LinearLayout>
<ImageView
android:id="#+id/secondImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_vertical_dots" />
</LinearLayout>
The idea here is to aligh our view with the toolbar's padding. By default, the toolbar sets 16dp padding on start and end.
So, to start with in our view, we set paddingStart and paddingEnd to be 16dp. Now, to adjust vertical padding, we set that to 8dp as it adjusts with the top and below items. Set, the orientation of parent to horizontal.
Then, we add first ImageView and sets its layoutWidth/Height to the values we want.
Then add the LinearLayout that holds the two TextViews and set it's orientation to vertical and layout_width to 0dp and add the two child TextViews.
Then finally add the overflow ImageView and provide the attributes. Since we set the first image and last image dimensions as static values, when the view is drawn, the width is allocated to these first and the remaining of the width goes to the textview container LinearLayout.
If we note at the pre-view attached, there might seem to be a bit more padding at the end. It is due to the dot image src we are assigning to the second image. It doesn't fill the ImageView and hence looks like extra padding. We can adjust this by adjusting the paddingEnd value of our parent LinearLayout.
Please comment if there are any confusions.
See this Image
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:layout_marginStart="10dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Title A"
android:textSize="20sp"
android:textColor="#000"
android:textStyle="bold"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Subtitle A"
android:textSize="17sp"
android:textColor="#000" />
</LinearLayout>
<ImageView
android:layout_width="40dp"
android:padding="13dp"
android:layout_height="match_parent"
android:src="#drawable/ic_menu"/>
</LinearLayout>

Android TextView inside RelativeLayout not acting properly

So I have a custom Header view that contains several widgets. My idea is to have a frame layout with ImageView (icon) aligned parent start and ImageButton (menu) aligned parent end. Also, TextView (title) to right of ImageView.. Those should always be visible.
To right of ImageView(icon) there should be a TextView (title) whose text should expand based on visibility of other widgets on the right side of it..
So the order from left to right goes like this: ImageView(icon) - TextView (title) - ImageView (unread messages) - ImageView (battery) - ImageButton (menu)
The problem is, the text from TextView goes behind the widgets on the right side of it, like it's visible on this image:
Here is my xml preview :
And this is the actual xml:
<RelativeLayout 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="58dp"
android:background="#color/view_controller_header">
<FrameLayout
android:id="#+id/frame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:layout_margin="10dp">
<ImageView
android:id="#+id/imageViewIC"
android:layout_width="#dimen/view_controller_header_icon_width"
android:layout_height="#dimen/view_controller_header_icon_height"
android:layout_gravity="center" />
<TextView
android:id="#+id/textViewKK"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:focusable="false"
android:gravity="center"
android:padding="10dp"
android:shadowColor="#color/text_shadow"
android:shadowDx="-1"
android:shadowDy="-1"
android:shadowRadius="1"
android:textColor="#color/main_text_color"
android:textSize="#dimen/view_controller_header_kk_texSize"
android:visibility="gone" />
<ProgressBar
android:id="#+id/progressBarVC"
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="gone" />
</FrameLayout>
<TextView
android:id="#+id/textViewDName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:layout_marginRight="#dimen/view_controller_header_margin2"
android:layout_toRightOf="#id/frame"
android:ellipsize="end"
android:fontFamily="sans-serif"
android:gravity="center|left"
android:maxLines="2"
android:textColor="#color/main_text_color"
android:textSize="17sp"
tools:text="This is the text that is too long .........." />
<ImageButton
android:id="#+id/imgSubMenu"
android:layout_width="50dp"
android:layout_height="match_parent"
android:layout_alignParentEnd="true"
android:layout_gravity="center"
android:background="?android:selectableItemBackground"
android:scaleType="center"
android:src="#drawable/ic_more_vert_white_24dp" />
<LinearLayout
android:id="#+id/bat"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_toStartOf="#id/imgSubMenu"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageViewBattery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:layout_weight="1"
android:scaleType="centerInside"
android:visibility="visible"
app:srcCompat="#drawable/ic_battery_full" />
<TextView
android:id="#+id/textViewBattery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:visibility="visible"
tools:text="100%" />
</LinearLayout>
<TextView
android:id="#+id/unreadMessages"
android:layout_width="22dp"
android:layout_height="22dp"
android:layout_centerInParent="true"
android:layout_toStartOf="#id/bat"
android:background="#color/color_widget_security_back_red"
android:gravity="center"
android:textStyle="bold"
android:visibility="visible"
tools:text="2" />
Any ideas, why does this text goes behind those widgets? I want it to resize if batery and unread messages are visible, like it goes into two lines instead of one, or to show ... if it's too long..
Probably you forgot add one more properties text as below :
android:layout_toLeftOf
android:layout_toLeftOf="#+id/imgSubMenu"
As per you requirement you want show text right to left icon and left to right menu icon but you add one relative reference toRight not toLeft so it's overlap on right side menu icon

Button with background image text positioning

I would like to position the text in my button like this:
At the moment the text is positioned in the bottom left corner of the button:
I have four rows of this type of buttons and in every row there is two buttons. Here is my code for one row of buttons:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/main_fashion_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/thumbnail_channel_fashion"
android:text="Fashion"
android:textColor="#FFFFFF"
android:gravity="bottom"
android:layout_weight="1"/>
<Button
android:id="#+id/main_science_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/thumbnail_channel_science"
android:text="Science"
android:textColor="#FFFFFF"
android:gravity="bottom"
android:layout_weight="1"/>
</LinearLayout>
how can I add some padding to the left and below of the text without moving the background image? Margin seems to add white space around button and padding creates space between background image and button's edges.
Thanks for help!
You can use this
<TextView
android:id="#+id/main_fashion_button"
android:layout_width="150dp"
android:layout_height="150dp"
android:background="#mipmap/ic_launcher"
android:text="Fashion"
android:textColor="#FFFFFF"
android:gravity="bottom"
android:padding="10dp">
Check this out
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageButton
android:id="#+id/imageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:src="#drawable/image" />
<TextView
android:id="#+id/imageViewText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical|left"
android:layout_alignBottom="#+id/imageButton"
android:text="FASHION"
android:padding="16dp"
android:textColor="#fff" />
</RelativeLayout>

Correct RelativeLayout usage please

I have a layout for a listview item and it's displayed incorrectly (book descr goes over the last update date). Can you please help me to fix?
If I set
android:layout_above="#+id/lastUpdatedDt"
to the bookDescr item the whole text disappears...
EDIT: I've updated sample according to comments
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants"
android:layout_margin="10dp"
android:background="#drawable/big_card_details"
android:clickable="true"
android:focusable="false">
<CheckBox
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/starStyle"
android:layout_marginTop="10dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="10dp" />
<TextView
android:id="#+id/bookTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Book title"
android:textSize="16sp"
android:layout_alignBaseline="#id/icon"
android:layout_alignParentTop="true"
android:layout_toRightOf="#id/icon" />
<TextView
android:id="#+id/bookSize"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="470k"
android:layout_alignBaseline="#id/icon"
android:textSize="16sp"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp" />
<TextView
android:id="#+id/lastUpdatedDt"
android:layout_width="match_parent"
android:layout_height="26dip"
android:singleLine="true"
android:text="Updated 27.04.2014 17.10"
android:textSize="12sp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_alignLeft="#id/icon" />
<TextView
android:id="#+id/bookDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="false"
android:textSize="10sp"
android:text="here should be long description"
android:layout_alignEnd="#id/bookSize"
android:layout_below="#+id/bookTitle"
android:layout_marginTop="20dp"
android:layout_alignLeft="#+id/icon" />
<!-- this is the button that will trigger sliding of the expandable view -->
<ImageButton
android:id="#+id/expand_details_button"
android:src="#drawable/slide_button_details"
android:layout_alignTop="#id/lastUpdatedDt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:layout_alignBaseline="#id/lastUpdatedDt"
android:layout_alignParentBottom="true"
android:layout_alignRight="#id/bookSize"
android:layout_alignEnd="#id/bookSize" />
</RelativeLayout>
<!-- this is the expandable view that is initially hidden and will slide out when the more button is pressed -->
<LinearLayout
android:id="#+id/details_expandable"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:orientation="horizontal"
>
<!-- put whatever you want in the expandable view -->
<Button
android:layout_width="match_parent"
android:layout_weight="0.3"
android:drawableTop="#drawable/ic_action_read"
android:text="#string/ReadBook"
style="#style/SlideButtonTheme" />
<Button
android:layout_width="match_parent"
android:layout_weight="0.3"
android:drawableTop="#drawable/ic_action_open_browser"
style="#style/SlideButtonTheme"
android:text="#string/OpenInBrowser" />
</LinearLayout>
</LinearLayout>
Hope your aware that the child view in the RelativeLayout need to placed below/above any of the sibling views or aligned from the parent.
The problem here is that , lastUpdatedDt is aligned to parent bottom , since the parent RelativeLayout height is set to wrap_content height will be decided based on the child views , so that lastUpdatedDt is placed at the bottom depends on the parent height, which overlap the sibling view.
FIX : Just set the android:layout_below="#+id/bookDescription" for the lastUpdatedDt TextView , so that the lastUpdatedDt will always be placed bottom of the parent and below bookDescription TextView sibling.

Categories

Resources