ScrollView with sub RelativeLayout does not scroll - android

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.

Related

Android App Layout collapses on physical device

The layouts and elements in Android Studio look exactly how I want it to be but as soon as I install the APK on my device, the layout collapses. All views are collapsed one on top of another which is not what was shown in Android Studio's preview...
I'm using linear layouts and relative layout. I had used android studio 1-2yrs ago and there was no such thing as constrained layout, now every time I create a layout I get an constrained error.
XML Main activity (only one):
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="match_parent" tools:context="com.simultaneousequations.spacelaunchx.spacelaunchx.MainActivity">
<LinearLayout
android:id="#+id/linearLayoutTop"
android:layout_width="0dp"
android:layout_height="101dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="11dp"
android:orientation="vertical"
app:layout_constraintBottom_toTopOf="#+id/linearLayoutCenter"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<RelativeLayout
android:id="#+id/relativeLayoutTop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorLightGrey">
<LinearLayout
android:id="#+id/linearLayoutTopImage"
android:layout_width="110dp"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="#+id/iridumNEXT40_50"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_weight="1"
android:adjustViewBounds="false"
android:cropToPadding="false"
android:scaleType="fitStart"
app:srcCompat="#mipmap/iridiummission5" />
</LinearLayout>
<TextView
android:id="#+id/iridium5Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="12dp"
android:layout_toEndOf="#+id/linearLayoutTopImage"
android:layout_toRightOf="#+id/linearLayoutTopImage"
android:fontFamily="sans-serif-condensed"
android:text="Iridium NEXT 40 50"
android:textAlignment="textStart"
android:textAllCaps="false"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"
android:textColor="#color/colorRedDragon"
android:textSize="16sp"
android:textStyle="bold|italic" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayoutCenter"
android:layout_width="400dp"
android:layout_height="101dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="101dp"
android:background="#color/colorLightGrey"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="109dp">
<LinearLayout
android:id="#+id/linearLayoutCenterImage"
android:layout_width="110dp"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="#+id/crs14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_weight="1"
android:scaleType="fitStart"
app:srcCompat="#mipmap/crs14" />
</LinearLayout>
<TextView
android:id="#+id/crs14Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
ndroid:layout_marginTop="16dp"
android:layout_toEndOf="#+id/linearLayoutCenterImage"
android:layout_toRightOf="#+id/linearLayoutCenterImage"
android:fontFamily="sans-serif-condensed"
android:text="SpaceX NASA - CRS-14"
android:textAlignment="center"
android:textAllCaps="false"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"
android:textColor="#color/colorRedDragon"
android:textSize="18sp"
android:textStyle="bold|italic" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayoutCenterBottom"
android:layout_width="400dp"
android:layout_height="101dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="101dp"
android:background="#color/colorLightGrey"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="109dp">
<LinearLayout
android:id="#+id/linearLayoutCenterBottomImage"
android:layout_width="110dp"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="#+id/bhu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_weight="1"
android:scaleType="fitCenter"
app:srcCompat="#mipmap/spacexlogo"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="101dp" />
</LinearLayout>
<TextView
android:id="#+id/bhuText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="12dp"
android:layout_toEndOf="#+id/linearLayoutCenterBottomImage"
android:layout_toRightOf="#+id/linearLayoutCenterBottomImage"
android:fontFamily="sans-serif-condensed"
android:text="Bangabandhu-1"
android:textAlignment="center"
android:textAllCaps="false"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"
android:textColor="#color/colorRedDragon"
android:textSize="18sp"
android:textStyle="bold|italic" />
</RelativeLayout>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
What am I doing wrong?
Seems like you're not familiar with ConstraintLayout.
That's OK. Don't use ConstraintLayout when you don't want to use it. Using this is not necessary.
What you have to do is just change the root layout to either LinearLayout, RelativeLayout or any other Layout you want to use as per your requirements.
I'm giving you the code that you should use in this case. In that code, I'm using RelativeLayout as the root layout, because you are using layout_alignParentBottom like attributes.
<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
tools:context="com.companys.appx.appx.MainActivity">
<LinearLayout
android:id="#+id/linearLayoutTop"
android:layout_width="0dp"
android:layout_height="101dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="11dp"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/relativeLayoutTop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorLightGrey">
<LinearLayout
android:id="#+id/linearLayoutTopImage"
android:layout_width="110dp"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="#+id/iridumNEXT40_50"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_weight="1"
android:adjustViewBounds="false"
android:cropToPadding="false"
android:scaleType="fitStart"
app:srcCompat="#mipmap/iridiummission5" />
</LinearLayout>
<TextView
android:id="#+id/iridium5Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="12dp"
android:layout_toEndOf="#+id/linearLayoutTopImage"
android:layout_toRightOf="#+id/linearLayoutTopImage"
android:fontFamily="sans-serif-condensed"
android:text="Iridium NEXT 40 50"
android:textAlignment="textStart"
android:textAllCaps="false"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"
android:textColor="#color/colorRedDragon"
android:textSize="16sp"
android:textStyle="bold|italic" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayoutCenter"
android:layout_width="400dp"
android:layout_height="101dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="101dp"
android:background="#color/colorLightGrey"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="109dp">
<LinearLayout
android:id="#+id/linearLayoutCenterImage"
android:layout_width="110dp"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="#+id/crs14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_weight="1"
android:scaleType="fitStart"
app:srcCompat="#mipmap/crs14" />
</LinearLayout>
<TextView
android:id="#+id/crs14Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="16dp"
android:layout_toEndOf="#+id/linearLayoutCenterImage"
android:layout_toRightOf="#+id/linearLayoutCenterImage"
android:fontFamily="sans-serif-condensed"
android:text="SpaceX NASA - CRS-14"
android:textAlignment="center"
android:textAllCaps="false"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"
android:textColor="#color/colorRedDragon"
android:textSize="18sp"
android:textStyle="bold|italic" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayoutCenterBottom"
android:layout_width="400dp"
android:layout_height="101dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="101dp"
android:background="#color/colorLightGrey"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="109dp">
<LinearLayout
android:id="#+id/linearLayoutCenterBottomImage"
android:layout_width="110dp"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="#+id/bhu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_weight="1"
android:scaleType="fitCenter"
app:srcCompat="#mipmap/spacexlogo"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="101dp" />
</LinearLayout>
<TextView
android:id="#+id/bhuText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="12dp"
android:layout_toEndOf="#+id/linearLayoutCenterBottomImage"
android:layout_toRightOf="#+id/linearLayoutCenterBottomImage"
android:fontFamily="sans-serif-condensed"
android:text="Bangabandhu-1"
android:textAlignment="center"
android:textAllCaps="false"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"
android:textColor="#color/colorRedDragon"
android:textSize="18sp"
android:textStyle="bold|italic" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
If there is any mistake(typo) in the code change it.
The only point is to use RelativeLayout as the root Layout instead of ConstraintLayout
In the above code, I've removed all the ConstraintLayout Attributes from the first LinearLayout because the root Layout is no more a ConstraintLayout.
This Trick will remove the problem. The problem was occurring because ContraintLayout has its own rules that you were not following. But now as you've changed the root Layout the problem will no longer exist.

How to align TextView + RecyclerView at screen bottom within ScrollView?

I have the next structure of XML file in my Fragment. I have an issue with RelativeLayout 3 (id=referralsContainer). It consist of textView (id=inviteText) + RecyclerView (id=rv_referrals). I need to show textView at the bottom of screen with any resolution. And after that textView must be RecyclerView with elements (when user will scroll down). I tried to do something like this in method, when all elements of recyclerview loaded, but recyclerview is replaced in different positions by Y axe from time to time, I can't understand how to correctly place that textview at the bottom of the scrren. And also ScrollView cuts several items of recyclerView. Does anybody can help with this? All positions in debugger looks right. Code+xml is below image. Thanks.
this.dataSourceListForAdapter.addAll(incomeListOfItemsFromServer);
recyclerView.setY(scrollView.getBottom() - textView.getHeight());
recyclerView.setY(coordinatorView.getBottom() - textView.getHeight());
adapter.notifyDataSetChanged();
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/invite_friends_swipe_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="#+id/invite_friends_content_part"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:visibility="visible">
<RelativeLayout
android:id="#+id/invite_friends_container"
android:layout_width="match_parent"
android:layout_height="190dp"
android:layout_alignParentTop="true"
android:background="#color/re_black_light_new">
<TextView
android:id="#+id/invite_friends_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/invite_friends_button_container"
android:gravity="center"
android:paddingEnd="40dp"
android:paddingStart="40dp"
android:textColor="#color/white"
android:textSize="16sp"
tools:text="#string/friends_invite_message" />
<RelativeLayout
android:id="#+id/invite_friends_button_container"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="true">
<View
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignParentTop="true"
android:layout_marginTop="2dp"
android:background="#drawable/combined_shape_white" />
<View
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_alignParentBottom="true"
android:background="#color/white" />
<android.support.v7.widget.CardView
android:id="#+id/invite_friends_button_invite"
android:layout_width="147dp"
android:layout_height="40dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:layout_marginTop="10dp"
card_view:cardBackgroundColor="#color/white"
card_view:cardCornerRadius="3dp"
card_view:cardElevation="2dp"
card_view:cardPreventCornerOverlap="false"
card_view:cardUseCompatPadding="true">
<TextView
android:id="#+id/invite_friends_button_invite_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/yellow_button_selector"
android:clickable="true"
android:focusable="true"
android:fontFamily="sans-serif-medium"
android:gravity="center"
android:text="#string/friends_invite"
android:textAllCaps="true"
android:textColor="#color/re_black_light_new"
android:textSize="14sp" />
</android.support.v7.widget.CardView>
</RelativeLayout>
</RelativeLayout>
<TextView
android:id="#+id/copy_choice_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/invite_friends_container"
android:layout_centerHorizontal="true"
android:textSize="12sp"
android:text="#string/copy_link_text"
android:textColor="#color/re_gray_new" />
<RelativeLayout
android:id="#+id/invite_friends_referal_link_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/copy_choice_label"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="16dp"
android:background="#drawable/rectangle_gray">
<TextView
android:id="#+id/invite_friends_referal_link_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:fontFamily="sans-serif-light"
android:gravity="center"
android:padding="7dp"
android:textColor="#color/re_black_light"
android:textSize="12sp" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/invite_friends_full_info_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/invite_friends_referal_link_button"
android:layout_marginTop="32dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_marginStart="24dp"
android:layout_toLeftOf="#+id/invite_friends_container_central"
android:gravity="center_vertical">
<TextView
android:id="#+id/invite_friends_text_friends"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-medium"
android:text="#string/friends_invite_friends"
android:textColor="#color/re_black_light_new"
android:textSize="12sp" />
<TextView
android:id="#+id/invite_friends_friends_count"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/invite_friends_text_friends"
android:layout_marginTop="4dp"
android:fontFamily="sans-serif-medium"
android:textColor="#color/re_black_light_new"
android:textSize="24sp"
tools:text="4" />
<View
android:id="#+id/invite_friends_divider_horizontal"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#+id/invite_friends_friends_count"
android:layout_marginTop="8dp"
android:background="#drawable/dash_horizontal_gray"
android:layerType="software" />
<TextView
android:id="#+id/invite_friends_text_orders"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_below="#+id/invite_friends_divider_horizontal"
android:layout_marginTop="14dp"
android:fontFamily="sans-serif-medium"
android:text="#string/friends_invite_orders"
android:textColor="#color/re_black_light_new"
android:textSize="12sp" />
<TextView
android:id="#+id/invite_friends_orders_count"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/invite_friends_text_orders"
android:layout_marginTop="4dp"
android:fontFamily="sans-serif-medium"
android:textColor="#color/re_black_light_new"
android:textSize="24sp"
tools:text="32" />
</RelativeLayout>
<View
android:id="#+id/invite_friends_container_central"
android:layout_width="1dp"
android:layout_height="1dp"
android:layout_centerInParent="true"
android:background="#drawable/dotted_line"
android:layerType="software" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="10dp"
android:layout_toRightOf="#+id/invite_friends_container_central">
<TextView
android:id="#+id/invite_friends_income_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="6dp"
android:fontFamily="sans-serif-medium"
android:text="#string/friends_invite_income"
android:textColor="#color/re_black_light_new"
android:textSize="12sp" />
<LinearLayout
android:id="#+id/invite_friends_income_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/invite_friends_income_text"
android:layout_marginTop="4dp"
android:orientation="horizontal">
<TextView
android:id="#+id/invite_friends_income_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="6dp"
android:fontFamily="sans-serif-medium"
android:lines="1"
android:maxLines="1"
android:textColor="#color/re_black_light_new"
android:textSize="24sp"
tools:text="1488.77" />
<TextView
android:id="#+id/invite_friends_income_currency"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:fontFamily="sans-serif-medium"
android:lines="1"
android:maxLines="1"
android:textColor="#color/re_black_light_new"
android:textSize="12sp"
tools:text="руб." />
</LinearLayout>
<View
android:id="#+id/invite_friends_divider_horizontal_2"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#+id/invite_friends_income_container"
android:layout_marginEnd="16dp"
android:layout_marginTop="8dp"
android:background="#drawable/dash_horizontal_gray"
android:layerType="software" />
<TextView
android:id="#+id/invite_friends_text_income_pending"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/invite_friends_divider_horizontal_2"
android:layout_marginStart="6dp"
android:layout_marginTop="14dp"
android:fontFamily="sans-serif-medium"
android:text="#string/friends_invite_income_pending"
android:textColor="#color/re_black_light"
android:textSize="12sp" />
<LinearLayout
android:id="#+id/invite_friends_text_income_pending_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/invite_friends_text_income_pending"
android:layout_marginStart="6dp"
android:layout_marginTop="4dp"
android:orientation="horizontal">
<TextView
android:id="#+id/invite_friends_text_income_pending_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-medium"
android:textColor="#color/re_gray_new"
android:textSize="24sp"
tools:text="0" />
<TextView
android:id="#+id/invite_friends_text_income_pending_count_currency"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:fontFamily="sans-serif-medium"
android:lines="1"
android:maxLines="1"
android:textColor="#color/re_gray_new"
android:textSize="12sp"
tools:text="руб."/>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:id="#+id/referralsContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/invite_friends_content_part"
android:visibility="gone"
android:orientation="vertical">
<TextView
android:id="#+id/inviteText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:fontFamily="sans-serif-medium"
android:text="Вы уже пригласили:"
android:textColor="#color/re_black_light_new"
android:textSize="20sp"
android:textStyle="normal" />
<ImageView
android:id="#+id/scrollMore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/inviteText"
android:layout_centerHorizontal="true"
android:layout_marginBottom="6dp"
android:layout_marginTop="6dp"
android:src="#drawable/ic_scroll_more" />
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_referrals"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/scrollMore"
android:scrollbars="vertical" />
</RelativeLayout>
</RelativeLayout>
<LinearLayout
android:id="#+id/invite_friends_error_part"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:gravity="center"
android:orientation="vertical"
android:visibility="gone">
<ImageView
android:id="#+id/error_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:layout_marginTop="20dp"
android:scaleType="fitCenter"
android:src="#drawable/lost_connection_holder" />
<TextView
android:id="#+id/error_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/error_image"
android:layout_marginBottom="60dp"
android:layout_marginTop="10dp"
android:gravity="center_horizontal"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:text="#string/no_connection_message"
android:textColor="#color/black_tr_38"
android:textSize="19sp" />
</LinearLayout>
</RelativeLayout>
</ScrollView>
</android.support.v4.widget.SwipeRefreshLayout>
ScrollView is not purposed for sub-child layouts aligned to the bottom of screen as its direct child doesn't know where the bottom is. From your question is not clear how do you need (want) to scroll bottom part.
If it is ok to scroll by dragging bottom part (RelativeLayout 3) it is better to use BottomSheetBehaviour in Coordinatorlayout for this layout.
If not then just put RelativeLayout 3 under (below) SwipeRefreshLayout and handle ScrollView scrollChanged events to animate scrolling of bottom layout if you need or just show/hide this bottom layout.
Don't forget that on small resolutions layout aligned to the bottom will cover the content of ScrollView and user won't be able to scroll top layout.

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

RelativeLayout limits after adding ScrollView

I am adding a ScrollView as parent of a RelativeLayout, but when I do so my RelativeLayout limits to a little area and I can't change it.
When I change the padding I can fix the things in their place, but it is not a good way.
Here is my code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relatOne"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="20dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="20dp">
<!--textArea Title-->
<EditText
android:id="#+id/textArea"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="#id/textView"
android:hint="Write your message here!"
android:linksClickable="true"
android:maxLines="6"
android:paddingLeft="20dp"
android:scrollbars="vertical"
android:textColorHint="#8e8e8e" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#id/textArea"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="Text:"
android:textColor="#color/textAreaTitle"
android:textSize="20sp" />
<Button
android:id="#+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#color/btnColor"
android:onClick="btnSetter"
android:text="Set on Channel"
android:textAllCaps="false"
android:textColor="#ffffffff"
android:textSize="20sp"
android:textStyle="bold"
/>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
You should make ScrollView as parent Root element not as Child in your case Change your Layout like this
<?xml version="1.0" encoding="utf-8"?>
<ScrollView android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical"
android:gravity="center">
<!--textArea Title-->
<EditText
android:id="#+id/textArea"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:hint="Write your message here!"
android:linksClickable="true"
android:maxLines="6"
android:paddingLeft="20dp"
android:scrollbars="vertical"
android:textColorHint="#8e8e8e" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:layout_alignBaseline="#id/textArea"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="Text:"
android:textSize="20sp" />
<Button
android:id="#+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:background="#000"
android:onClick="btnSetter"
android:text="Set on Channel"
android:textAllCaps="false"
android:textColor="#ffffffff"
android:textSize="20sp"
android:textStyle="bold"
/>
</LinearLayout>
</ScrollView>
Change Your Linear Layout gravity and margin according to your need
Screenshot of problem
I have this problem now after making ScrollView to root parent.

Set one element above another and have width of both the element equal

I want to set the text field above the black image and i want the width of text field should be equal to image.
I dont want to put hardcoded dimensions. Also both the blocks should qually cover the screen horizontally.
Here is my code `
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/RL_childLeft1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<com.ism.eshita.incentivemanagmentsystem.Speedometer.Speedometer
android:id="#+id/Speedometer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#000000"
custom:currentSpeed="100"
custom:maxSpeed="300" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical">
<TextView
android:id="#+id/tv_bg1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/PrimaryDark2"
android:gravity="center"
android:text="BG"
android:textColor="#color/white"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tv_performance1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/PrimaryDark2"
android:gravity="center"
android:paddingBottom="5dp"
android:text="Performance"
android:textColor="#color/white" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
>
<com.ism.eshita.incentivemanagmentsystem.Speedometer.Speedometer
android:id="#+id/Speedometer2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#000000"
custom:currentSpeed="100"
custom:maxSpeed="300" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical">
<TextView
android:id="#+id/tv_bg2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/PrimaryDark2"
android:gravity="center"
android:text="BG"
android:textColor="#color/white"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tv_performance2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/PrimaryDark2"
android:gravity="center"
android:paddingBottom="5dp"
android:text="Performance"
android:textColor="#color/white" />
</LinearLayout>
</RelativeLayout>
</RelativeLayout>

Categories

Resources