how to keep center relativelayout inside DragLayer in android - android

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_gravity="top"
android:layout_weight=".1"
android:background="#color/colorPaleGrey2"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="20dp">
<TextView
android:id="#+id/meeting_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-medium"
android:gravity="center_horizontal"
android:text="Entrepreneur Meet Up"
android:textColor="#color/blackDark"
android:textSize="20.1sp"
android:textStyle="normal" />
<TextView
android:id="#+id/time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/meeting_text"
android:layout_centerInParent="true"
android:layout_marginTop="10dip"
android:fontFamily="sans-serif-medium"
android:gravity="center_horizontal"
android:lineSpacingExtra="6sp"
android:text="(11:30AM - 4:00PM)"
android:textColor="#color/bluey_grey_three"
android:textSize="14sp"
android:textStyle="normal" />
</RelativeLayout>
<com.kdcos.contsync.utilities.dragview.DragLayer
android:id="#+id/framelayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/rl_dropView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:padding="40dp">
<ImageView
android:id="#+id/imageView"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="#drawable/ic_drop_cards" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView"
android:layout_centerInParent="true"
android:layout_gravity="center_horizontal|center"
android:fontFamily="sans-serif"
android:gravity="center_horizontal"
android:lineSpacingExtra="10sp"
android:text="Drop Cards Here"
android:textColor="#color/blackDark"
android:textSize="10sp"
android:textStyle="normal" />
</RelativeLayout>
</com.kdcos.contsync.utilities.dragview.DragLayer>
</LinearLayout>
this is my xml i want keep rl_dropView center inside com.kdcos.contsync.utilities.dragview.DragLayer i wrote xml like this i am unable to set it please look my screen please look i want to set dropView center but from my code its coming top left please suggest how to set it in center.
below is my current Screen:

if DragLayer is SubClass of RelativeLayout then it should work otherwise try below solution
Solution 1
<com.kdcos.contsync.utilities.dragview.DragLayer
android:id="#+id/framelayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/rl_dropView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:padding="40dp">
<ImageView
android:id="#+id/imageView"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="#drawable/ic_drop_cards" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView"
android:layout_centerInParent="true"
android:layout_gravity="center_horizontal|center"
android:fontFamily="sans-serif"
android:gravity="center_horizontal"
android:lineSpacingExtra="10sp"
android:text="Drop Cards Here"
android:textColor="#color/blackDark"
android:textSize="10sp"
android:textStyle="normal" />
</RelativeLayout>
</RelativeLayout>
</com.kdcos.contsync.utilities.dragview.DragLayer>
UPDATE
Solution 2
as I can see android:orientation="vertical" in your code under DragLayer.its mean DragLayer is SubClass of LinearLayout, so in your RelativeLayout just change
android:layout_centerInParent="true"
to:
android:layout_gravity="center_vertical"

Related

center multiple RelativeLayout inside LinearLayout

Inside a linearLayout I have put three RelativeLayouts with weight = 1. I want RelativeLayouts to be in the center of the screen. How can I manage their position?
below is my xml code:
<LinearLayout
android:id="#+id/layout_circular_items"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/view_bg_profile_img"
android:padding="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:background="#color/color_backgrounds_light">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/circle_txt_view"
android:layout_margin="8dp"
android:gravity="center"
android:layout_gravity="center">
<TextView
android:id="#+id/txt_view_scores"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2843"
android:textSize="#dimen/dim_txt_size_times"
android:gravity="center"
android:foregroundGravity="center"
android:layout_centerHorizontal="true"/>
<TextView
android:id="#+id/txt_view_scores_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="scores"
android:layout_below="#id/txt_view_scores"
android:textSize="#dimen/dim_txt_size_times_title"
android:gravity="center"
android:foregroundGravity="center"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/circle_txt_view"
android:layout_margin="8dp"
android:gravity="center"
android:layout_gravity="center">
<TextView
android:id="#+id/txt_view_purchase"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="12"
android:textSize="#dimen/dim_txt_size_times"
android:gravity="center"
android:foregroundGravity="center"
android:layout_centerHorizontal="true"/>
<TextView
android:id="#+id/txt_view_purchase_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="purchase"
android:layout_below="#id/txt_view_purchase"
android:textSize="#dimen/dim_txt_size_times_title"
android:gravity="center"
android:foregroundGravity="center"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/circle_txt_view"
android:layout_margin="8dp"
android:gravity="center"
android:layout_gravity="center">
<TextView
android:id="#+id/txt_view_cash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="21450"
android:textSize="#dimen/dim_txt_size_times"
android:gravity="center"
android:foregroundGravity="center"
android:layout_centerHorizontal="true"/>
<TextView
android:id="#+id/txt_view_cash_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="cash"
android:layout_below="#id/txt_view_cash"
android:textSize="#dimen/dim_txt_size_times_title"
android:gravity="center"
android:foregroundGravity="center"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
</LinearLayout>
and this is the screen shot of output:
I want the red spaces be equal.
In your top LinearLayout
simply use android:gravity="center"
It will definitely work. If it does not work remove all gravity from RelativeLayout. Only use android:gravity="center" in top LinearLayout.
You can try to this code and edit according your need
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/layout_circular_items"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="8dp"
android:gravity="center">
<TextView
android:id="#+id/txt_view_scores"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:foregroundGravity="center"
android:gravity="center"
android:text="2843" />
<TextView
android:id="#+id/txt_view_scores_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txt_view_scores"
android:layout_centerHorizontal="true"
android:foregroundGravity="center"
android:gravity="center"
android:text="scores" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="8dp"
android:gravity="center">
<TextView
android:id="#+id/txt_view_purchase"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:foregroundGravity="center"
android:gravity="center"
android:text="12" />
<TextView
android:id="#+id/txt_view_purchase_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txt_view_purchase"
android:layout_centerHorizontal="true"
android:foregroundGravity="center"
android:gravity="center"
android:text="purchase" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center">
<TextView
android:id="#+id/txt_view_cash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:foregroundGravity="center"
android:gravity="center"
android:text="21450" />
<TextView
android:id="#+id/txt_view_cash_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txt_view_cash"
android:layout_centerHorizontal="true"
android:foregroundGravity="center"
android:gravity="center"
android:text="cash" />
</RelativeLayout>
</LinearLayout>
Although this question is answered correctly but just want to add more explanation.
When you want to change the children positions you must work with their parent gravity, No need to change the children gravity. In your case you must simply change your LinearLayout's gravity to center or center_horizontal like this:
<LinearLayout
android:id="#+id/layout_circular_items"
android:layout_width="0dp"
android:layout_height="wrap_content"
<!-- Add this line-->
android:gravity="center"
> ...
android:gravity is the Inside gravity of that View. This means, in
which direction it's contents should align.
This link is also useful to find more info about gravity and layout_gravity:
Gravity and layout_gravity on Android

Layout is reversed?

Please view the image below:
The first image is the layout I did and it show normally on my device and another test devices. The second image is the layout showed on my friend device, it is reversed. I do not know why, and it just happened on few devices.
This is my layout code on xml.
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:auto="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.ComposeSmsActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar_action" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="#dimen/padding_5">
<com.hnib.smslater.views.FlowLayout
android:id="#+id/layout_name_recipient"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/margin_10"
android:fontFamily="sans-serif-light"
android:orientation="horizontal">
</com.hnib.smslater.views.FlowLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="#dimen/padding_10">
<com.hnib.smslater.views.ClearableAutoContactCompleteTextView
android:id="#+id/et_recipient"
android:layout_width="#dimen/width_et_contact"
android:layout_height="#dimen/height_et_default"
android:layout_centerVertical="true"
android:layout_marginTop="#dimen/margin_10"
android:background="#drawable/drawable_radius_gray_gray"
android:fontFamily="sans-serif-light"
android:hint="#string/to_"
android:imeOptions="actionDone"
android:padding="#dimen/padding_5"
android:textColorHint="#color/gray"
android:textSize="18sp"
android:textStyle="bold"
auto:displayPhotoIfAvailable="false"
auto:styleOfTypedLetters="bold"
auto:typeOfData="phone"
auto:typedLettersHaveDifferentStyle="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/margin_5"
android:layout_marginTop="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:fontFamily="sans-serif-light"
android:text="#string/message"
android:textColor="#color/gray"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tv_message_length"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:fontFamily="sans-serif-light"
android:text="160/1" />
</RelativeLayout>
<com.hnib.smslater.views.ActionEditText
android:id="#+id/et_text_content"
android:layout_width="match_parent"
android:layout_height="#dimen/height_et_big"
android:layout_marginLeft="#dimen/margin_5"
android:background="#drawable/drawable_radius_gray_gray"
android:ems="10"
android:fontFamily="sans-serif-light"
android:gravity="top"
android:inputType="textMultiLine"
android:maxLines="10"
android:padding="#dimen/padding_5"
android:scrollbars="vertical" />
<RelativeLayout
android:id="#+id/layout_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:padding="#dimen/padding_10"
android:showDividers="middle">
<TextView
android:id="#+id/tv_title_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:background="#drawable/drawable_radius_acent_gray"
android:fontFamily="sans-serif-light"
android:padding="#dimen/padding_5"
android:text="#string/time"
android:textAllCaps="true" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_toRightOf="#+id/tv_title_time"
android:divider="?android:attr/dividerVertical"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="#dimen/padding_10"
android:showDividers="middle">
<TextView
android:id="#+id/tv_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/selector_textview"
android:fontFamily="sans-serif-light"
android:gravity="center_horizontal"
android:text="12/06/2016"
android:textColor="#color/gray"
android:textSize="27sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tv_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/selector_textview"
android:fontFamily="sans-serif-light"
android:gravity="center_horizontal"
android:text="09:10"
android:textColor="#color/gray"
android:textSize="27sp"
android:textStyle="bold" />
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="#dimen/padding_10">
<TextView
android:id="#+id/tv_title_repeat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginRight="30dp"
android:background="#drawable/drawable_radius_acent_gray"
android:fontFamily="sans-serif-light"
android:padding="#dimen/padding_5"
android:text="#string/repeat"
android:textAllCaps="true" />
<Spinner
android:id="#+id/spinner_repeat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Spinner>
</LinearLayout>
<LinearLayout
android:id="#+id/layout_choose_sim"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="#dimen/padding_10">
<TextView
android:id="#+id/title_choose_sim"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="30dp"
android:background="#drawable/drawable_radius_acent_gray"
android:fontFamily="sans-serif-light"
android:padding="#dimen/padding_5"
android:text="#string/choose_sim"
android:textAllCaps="true" />
<Spinner
android:id="#+id/spinner_choose_sim"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Spinner>
</LinearLayout>
<LinearLayout
android:id="#+id/layout_confirm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="#dimen/padding_10">
<TextView
android:id="#+id/tv_title_confirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginRight="15dp"
android:background="#drawable/drawable_radius_acent_gray"
android:fontFamily="sans-serif-light"
android:padding="#dimen/padding_5"
android:text="#string/ask_before_send"
android:textAllCaps="true" />
<CheckBox
android:id="#+id/checkbox_confirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="20dp"
android:layout_toRightOf="#+id/tv_title_confirm" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
Please have a help on this issue. Thanks.
The language on your friend's phone is probably different from yours, leading to the app giving a read from right to left, rather than your left to right. Does your friend have his phone's settings on language set to Hebrew or Arabic?
Edit: please take note of the answers below mine, those are the viable options for your friend's phone, besides for changing the language of course.
You should remove android:supportsRtl="true"
from the <application> element in your AndroidManifest.xml file.
You should ADD : android:supportRTL="false"
In the manifest

ScrollView with sub RelativeLayout does not scroll

I have a ScrollView with 1 RelativeLayout which has 3 sub RelativeLayouts:
I set top RelativeLayout to alignParentTop="true",
The bottom RelativeLayout to alignParentBottom="true".
The middle layout to
android:layout_below="#id/topLayout"
android:layout_above="#+id/bottomLayout"
Problem: Scrollview does not scroll when screen is small, instead topLayout stays at top and bottomLayout stays at the bottom. The middle layout gets small and (even lost) as like below:
Desired: I want topLayout stay at top and bottomLayout stay at bottom. But when there is no space there must be scroll so they middle layout must not get lost.
Code:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
tools:context="com.jemshit.itu.fragments.TakeAttendanceFragment"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:paddingBottom="#dimen/halfClassic"
android:paddingLeft="#dimen/halfClassic"
android:paddingRight="#dimen/halfClassic"
android:paddingTop="#dimen/halfClassic">
<RelativeLayout
android:id="#+id/layoutWeekChoice"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:padding="0dp"
android:background="#color/white"
android:layout_alignParentTop="true">
<Spinner
android:id="#+id/spinnerWeekChoice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/buttonSetDate"
android:layout_toStartOf="#+id/buttonSetDate"
/>
<Button
android:id="#+id/buttonSetDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:maxWidth="150dp"
android:textSize="16sp"
android:text="Set Date"
android:gravity="center"
android:textColor="#color/white"
android:textAllCaps="false"
android:background="#drawable/button_background"
android:paddingLeft="#dimen/halfClassic"
android:paddingRight="#dimen/halfClassic"
android:layout_alignBottom="#+id/spinnerWeekChoice"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/layoutCardRead"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:gravity="center"
android:layout_marginTop="32dp"
android:layout_below="#id/layoutWeekChoice"
android:layout_above="#+id/layoutShowAttendance"
android:layout_centerHorizontal="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageCard"
android:src="#drawable/std_card"
android:layout_alignParentTop="true"
android:layout_marginBottom="16dp"
android:layout_centerHorizontal="true"/>
<TextView
android:id="#+id/textWarningCard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Checking..."
android:textColor="#color/black"
android:textSize="14sp"
android:layout_below="#+id/imageCard"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/layoutShowAttendance"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="#dimen/halfClassic"
android:background="#color/white"
android:layout_alignParentBottom="true">
<TextView
android:id="#+id/textAttended"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Attended: "
android:textColor="#color/black"
android:layout_alignParentTop="true"
android:textSize="16sp"/>
<TextView
android:id="#+id/textNotAttended"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Not Attended: "
android:textColor="#color/black"
android:textSize="16sp"
android:layout_marginBottom="32dp"
android:layout_below="#+id/textAttended"/>
<Button
android:id="#+id/buttonManualAttendance"
android:layout_width="wrap_content"
android:layout_centerHorizontal="true"
android:layout_height="wrap_content"
android:text="Manual Attendance"
android:gravity="center"
android:textAllCaps="false"
android:textSize="16sp"
android:textColor="#color/white"
android:background="#drawable/button_background"
android:minHeight="50dp"
android:layout_below="#id/textNotAttended"
android:paddingLeft="#dimen/halfClassic"
android:paddingRight="#dimen/halfClassic"/>
</RelativeLayout>
</RelativeLayout>
</ScrollView>
Note: I do not want to use LinearLayout with weight="1" which will make my 3 layouts same height
EDIT: now ScollView scrolls with new code below but my bottom TextViews get lost:
Code Updated:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
tools:context="com.jemshit.itu.fragments.TakeAttendanceFragment"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:paddingBottom="#dimen/halfClassic"
android:paddingLeft="#dimen/halfClassic"
android:paddingRight="#dimen/halfClassic"
android:paddingTop="#dimen/halfClassic">
<RelativeLayout
android:id="#+id/layoutWeekChoice"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:padding="0dp"
android:background="#color/white"
android:layout_alignParentTop="true">
<Spinner
android:id="#+id/spinnerWeekChoice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/buttonSetDate"
android:layout_toStartOf="#+id/buttonSetDate"
/>
<Button
android:id="#+id/buttonSetDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:maxWidth="150dp"
android:textSize="16sp"
android:text="Set Date"
android:gravity="center"
android:textColor="#color/white"
android:textAllCaps="false"
android:background="#drawable/button_background"
android:paddingLeft="#dimen/halfClassic"
android:paddingRight="#dimen/halfClassic"
android:layout_alignBottom="#+id/spinnerWeekChoice"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/layoutCardRead"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:gravity="center"
android:layout_marginTop="32dp"
android:layout_below="#id/layoutWeekChoice"
android:layout_centerHorizontal="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageCard"
android:src="#drawable/std_card"
android:layout_alignParentTop="true"
android:layout_marginBottom="16dp"
android:layout_centerHorizontal="true"/>
<TextView
android:id="#+id/textWarningCard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Checking..."
android:textColor="#color/black"
android:textSize="14sp"
android:layout_below="#+id/imageCard"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/layoutShowAttendance"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="#dimen/halfClassic"
android:background="#color/white"
android:layout_below="#id/layoutCardRead"
android:layout_alignParentBottom="true">
<TextView
android:id="#+id/textAttended"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Attended: "
android:textColor="#color/black"
android:layout_above="#+id/textNotAttended"
android:textSize="16sp"/>
<TextView
android:id="#+id/textNotAttended"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Not Attended: "
android:textColor="#color/black"
android:textSize="16sp"
android:layout_marginBottom="32dp"
android:layout_above="#+id/buttonManualAttendance" />
<Button
android:id="#+id/buttonManualAttendance"
android:layout_width="wrap_content"
android:layout_centerHorizontal="true"
android:layout_height="wrap_content"
android:text="Manual Attendance"
android:gravity="center"
android:textAllCaps="false"
android:textSize="16sp"
android:textColor="#color/white"
android:background="#drawable/button_background"
android:minHeight="50dp"
android:layout_alignParentBottom="true"
android:paddingLeft="#dimen/halfClassic"
android:paddingRight="#dimen/halfClassic"/>
</RelativeLayout>
</RelativeLayout>
</ScrollView>
Set your parents RelativeLayout height to wrap_content. Match_parent needs only ScrollView, everything inside it can spend all the space it needs.
From your 2nd relative layout,that is layoutcard remove android:layout_above="#+id/layoutShowAttendance"
and from layoutShowAttendance remove
android:layout_alignParentBottom="true"
and add android:layout_below="#+id/layoutCardRead" to layoutShowAttendance
It will work.

Android RelativeLayout align element on another elements border

Im trying to make a text view to align to the left border of another imageview in a RelativeLayout, but I'm having trouble and I cannot achieve this. Can anyone tell me how can I achieve this?
Here is my current XML layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:orientation="vertical" >
<ImageView
android:id="#+id/main_item_bg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:contentDescription="#string/app_name"
android:scaleType="fitXY" />
<RelativeLayout
android:id="#+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#drawable/blackalha"
android:gravity="center_vertical"
android:orientation="horizontal"
android:weightSum="100" >
<TextView
android:id="#+id/main_item_cont"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/main_item_nmb"
android:paddingLeft="5dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/white" />
<ImageView
android:id="#+id/main_item_ic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#color/app_purple"
android:src="#drawable/bacal" />
<TextView
android:id="#+id/main_item_nmb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/main_item_ic"
android:background="#drawable/saric"
android:gravity="center"
android:textColor="#333333" />
</RelativeLayout>
<TextView
android:id="#+id/main_item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/main_item_bg"
android:layout_margin="5dp"
android:maxWidth="200dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/white" />
</RelativeLayout>
Try this
<?xml version="1.0" encoding="utf-8"?>
<ImageView
android:id="#+id/main_item_bg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:contentDescription="#string/app_name"
android:scaleType="fitXY" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="80dip"
android:gravity="center"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >
<TextView
android:id="#+id/main_item_cont"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:gravity="center"
android:paddingLeft="5dp"
android:text="dsgffdufsdfdfsdfdsfsdfsdfdsfdf"
android:inputType="textMultiLine"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/white" />
<ImageView
android:id="#+id/main_item_ic"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="5dip"
android:layout_alignParentRight="true"
android:layout_toRightOf="#+id/main_item_cont"
android:background="#android:color/black"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/main_item_nmb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/main_item_cont"
android:layout_toRightOf="#+id/main_item_cont"
android:background="#android:color/white"
android:text="10"
android:textColor="#android:color/white" />
</RelativeLayout>
<TextView
android:id="#+id/main_item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/main_item_bg"
android:layout_margin="5dp"
android:maxWidth="200dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/white" />
As #Salauyou mentioned, try with android:layout_alignLeft because it "Makes the left edge of this view match the left edge of the given anchor view ID"
and android:layout_toLeftOf means "Positions the right edge of this view to the left of the given anchor view ID."
Now, this is what I achieved:
I used this picture in drawable-mdpi (as the badge) - note the trick of the dual background:
and the standard launcher icon, since I didn't have a wine glass icon handy.
This is my layout:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
android:layout_margin="0dp"
android:padding="0dp"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="0dp"
android:padding="4dp"
>
<TextView
android:id="#+id/txtIcon"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentRight="true"
android:gravity="center"
android:padding="4dp"
android:background="#6b2339"
android:drawableRight="#drawable/ic_launcher"
/>
<TextView
android:id="#+id/txtBadge"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_toLeftOf="#id/txtIcon"
android:gravity="center"
android:padding="4dp"
android:background="#drawable/wines_crossing_badge"
android:text="8"
android:textColor="#312c23"
android:textSize="16sp"
/>
<TextView
android:id="#+id/txtDesc"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#id/txtBadge"
android:background="#312c23"
android:gravity="left|center_vertical"
android:padding="4dp"
android:text="Available Androids in stock for rental"
android:textColor="#fff"
android:textSize="16sp"
/>
</RelativeLayout>
</RelativeLayout>

GridLayout - Aligning Element

I am trying to reduce the space between 2 columns, have tried using negative margins, but did not work. Is there a way to bring the 2 elements in the image closer ?
<?xml version="1.0" encoding="utf-8"?>
<GridLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:useDefaultMargins="true"
android:alignmentMode="alignBounds"
android:columnOrderPreserved="false"
android:layout_gravity="center"
android:orientation="horizontal"
android:stretchMode="columnWidth"
android:background="#color/orange"
android:textSize="12sp"
android:columnCount="8">
<ImageButton
android:id="#+id/settings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnSpan="1"
android:layout_gravity="left"
android:src="#drawable/settings" />
<TextView
android:id="#+id/currentCityName"
android:layout_column="2"
android:textSize="22sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="City name"
android:layout_columnSpan="4"
android:layout_gravity="center_horizontal"
/>
<ImageButton
android:id="#+id/refresh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="7"
android:layout_columnSpan="1"
android:layout_gravity="right"
android:src="#drawable/reload" />
<ImageView
android:id="#+id/currentWeatherImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnSpan="8"
android:layout_rowSpan="3"
android:layout_gravity="center_horizontal"
android:src="#drawable/cloud" />
<ImageView
android:id="#+id/humidityIcon"
android:layout_width="20dp"
android:paddingTop="10dp"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:layout_marginRight="0dp"
android:layout_gravity="left"
android:background="#000000"
android:src="#drawable/humidity" />
<TextView
android:id="#+id/humidity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="13sp"
android:paddingTop="10dp"
android:paddingLeft="0dp"
android:layout_marginLeft="0dp"
android:layout_gravity="left"
android:background="#FFFFFF"
/>
<TextView
android:id="#+id/temperature"
android:layout_columnSpan="4"
android:layout_rowSpan="2"
android:textSize="49sp"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:layout_gravity="center_horizontal"/>
<ImageView
android:id="#+id/windSpeedIcon"
android:layout_width="30dp"
android:layout_marginRight="1dp"
android:paddingRight="1dp"
android:layout_gravity="right"
android:src="#drawable/wind" />
<TextView
android:id="#+id/windSpeed"
android:textSize="12sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
/>
<!-- android:layout_width="40dp" -->
<TextView
android:id="#+id/precipitationIcon"
android:textSize="28sp"
android:layout_width="40dp"
/>
<TextView
android:id="#+id/precipitation"
android:layout_column="0"
android:textSize="13sp"
android:layout_width="40dp"
/>
<TextView
android:id="#+id/windDirection16Point"
android:textSize="13sp"
android:layout_width="80dp"
android:layout_columnSpan="2"
/>
<TextView
android:id="#+id/weatherDescription"
android:layout_gravity="center_horizontal"
android:layout_row="7"
android:layout_columnSpan="8"/>
<TextView
android:id="#+id/section_label"
android:layout_margin="5dp"
android:layout_columnSpan="8" />
<EditText
android:minLines="4"
android:maxLines="4"
android:id="#+id/edit"
android:layout_columnSpan="8"/>
I suggest implementing the entire view using a RelativeLayout, should be the best solution for the entire view.
You are tackling the wrong problem – you are asking how to reduce the space between columns in a grid layout, but maybe you should’t have used such a layout in the first place!
What you should have been using for your specific layout, is a combination of relative positioning and compound drawables (here, with android:drawableStart).
<?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="80dp"
android:background="#d89018"
android:paddingBottom="4dp"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="4dp"
>
<TextView
android:id="#+id/temperature"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:text="25 ℃"
android:textColor="#fff"
android:textSize="49sp"
/>
<TextView
android:id="#+id/humidity"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:drawableStart="#drawable/humidity"
android:gravity="center_vertical"
android:text="83 %"
android:textColor="#fff"
/>
<TextView
android:id="#+id/windSpeed"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:drawableStart="#drawable/wind"
android:gravity="center_vertical"
android:text="19 km/h"
android:textColor="#fff"
/>
<TextView
android:id="#+id/precipitation"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:drawableStart="#drawable/precipitation"
android:gravity="center_vertical"
android:text="0.1 mm"
android:textColor="#fff"
/>
<TextView
android:id="#+id/windDirection16Point"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_alignLeft="#id/windSpeed"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:drawableStart="#drawable/windDirection"
android:gravity="center_vertical"
android:text="NNW"
android:textColor="#fff"
/>
</RelativeLayout>
Not only this layout is simpler, you also let Android take care of all the spacing for you. To change the spacing between the compound drawable and the text, use android:drawablePadding in the layout’s code above; in Java code, that would be TextView.setCompoundDrawablePadding).
Icons used for the sample: water, windsock, rainy-weather and wind-rose.
I think what's happening is that your Views are being stretched because they are trying to match the width of another view in their columns.
Try decreasing the columnSpan of your Views or separate your layout into two separate GridLayouts.
Or, consider using a different layout altogether. I would recommend RelativeLayout here instead of GridLayout, but I don't know how this fits into a larger window.
try this:
<?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="100dp">
<LinearLayout
android:id="#+id/col1"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_weight="2.4"
>
<LinearLayout
android:id="#+id/col1_row1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="83%"/>
</LinearLayout>
<LinearLayout
android:id="#+id/col1_row2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1.0mm"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/col2"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_weight="2.0"
android:gravity="center"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="25'"
android:textSize="50dp"
/>
</LinearLayout>
<LinearLayout
android:id="#+id/col3"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_weight="2.4"
>
<LinearLayout
android:id="#+id/col3_row1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="19Km"/>
</LinearLayout>
<LinearLayout
android:id="#+id/col3_row2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="NNW"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
I think you should change usedefaultMargins to false and give your own margins to the elements.
android:useDefaultMargins="false";

Categories

Resources