Why Tab Layout's layout is neither scrollable or fit screen inside a fragment? - android-layout

So i have a bottom navigation bar and in one of them it holds a tablayout consists of 3 tabs. Like the below image:
The Tablayout is inside a fragment, where it's not scrollable
The second issue other than not scrollable is, it doesn't fit the screen. Here is the android studio UI with blueprint image. (Please do compare with previous image and you will see the right most helloworld is not fitting in the screen and also couple of hello world is not scrollable!)
Tab Layout's Fragment not fitting the screen!
Partial of my home_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tool"
>
<com.google.android.material.tabs.TabLayout
android:id="#+id/tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabMaxWidth="0dp"
app:tabMode="fixed"
app:tabPaddingBottom="-1dp"
app:tabPaddingEnd="-1dp"
app:tabPaddingStart="-1dp"
app:tabPaddingTop="-1dp"
app:layout_scrollFlags="scroll|enterAlways">
</com.google.android.material.tabs.TabLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.viewpager.widget.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tablayout"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"/>
</LinearLayout>
And here is my Category.xml (where it doesn't fit the screen)
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="88dp"
android:text="Hello World"
android:textSize="22dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView9"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="280dp"
android:layout_marginLeft="280dp"
android:layout_marginTop="56dp"
android:text="Hello World"
android:textSize="22dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="136dp"
android:text="Hello World"
android:textSize="22dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="188dp"
android:text="Hello World"
android:textSize="22dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="188dp"
android:text="Hello World"
android:textSize="22dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="78dp"
android:text="YYYYYYYYYYYUU"
android:textSize="22dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="4dp"
android:layout_marginLeft="4dp"
android:layout_marginTop="48dp"
android:text="Hello World"
android:textSize="22dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="156dp"
android:layout_marginLeft="156dp"
android:layout_marginTop="56dp"
android:text="Hello World"
android:textSize="22dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="200dp"
android:layout_marginLeft="200dp"
android:text="Hello World"
android:textSize="22dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
I have tried almost all StackOverflow question regarding this problem, but nothing did work for me!May be i just screwed up somewhere, as i am a noob programmer! Any help good people?

Related

Android studio design for layout is not showing

Android Studio design for layout is not showing if I use TextInputLayout.
Before it was working. I don't update anything. If I remove the TextInputLayout then it is working.
I clean the Invalid cache.
I don't have any idea where is the problem.
What I can do now?
<androidx.constraintlayout.widget.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=".Withdrawal">
<ImageView
android:id="#+id/imageView11"
android:layout_width="159dp"
android:layout_height="159dp"
android:src="#drawable/eggs_basket"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.094"
tools:ignore="MissingConstraints" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Share with your friend your code to earn Coin"
android:textColor="#AE6F03"
android:textSize="25dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView11"
app:layout_constraintVertical_bias="0.042" />
<TextView
android:id="#+id/myRef"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="myCode"
android:textSize="30dp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.104"
app:layout_constraintStart_toEndOf="#+id/textView6"
app:layout_constraintTop_toBottomOf="#+id/textView3"
app:layout_constraintVertical_bias="0.103" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your Code is:"
android:textSize="30dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.119"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView3"
app:layout_constraintVertical_bias="0.106" />
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="9dp"
app:layout_constraintBottom_toTopOf="#+id/imageView11"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="mu" />
</com.google.android.material.textfield.TextInputLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
This is my xml code. After inpute the TextInputEditText it not working.

there is a problem with gridlayout in android studio

When I try to add grid-layout it takes the full screen. But I want it to set below these elements but I can't. what to do now?
I can't find any solution on youtube or any other platform so I have to post here.
Actually, I'm learning android app development for the first time. I copy-paste the same code from the instructor but nothing happens. maybe he is using an older version of android studio and I'm using the latest version.
Anyway please someone help me to solve this problem. And if possible please explain the problem. I'll really thankful to you guys.
and also this text also appears. How can I remove this text?
here is my XML code I paste here..please find my mistake and solve this
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">
<Button
android:id="#+id/startButton"
android:layout_width="203dp"
android:layout_height="110dp"
android:background="#02F837"
android:onClick="start"
android:text="Go!"
android:textColor="#000000"
android:textSize="50sp"
android:visibility="invisible"
app:backgroundTint="#00F10A"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/timerTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#F4DC01"
android:gravity="center"
android:padding="5dp"
android:text="30s"
android:textColor="#000000"
android:textSize="35sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.046"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.029" />
<TextView
android:id="#+id/pointsTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#F4DC01"
android:gravity="center"
android:padding="5dp"
android:text="0/0"
android:textColor="#000000"
android:textSize="35sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.953"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.029" />
<TextView
android:id="#+id/sumTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#58F15E"
android:gravity="center"
android:padding="15dp"
android:text="31 + 12"
android:textColor="#000000"
android:textSize="35sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.03" />
<androidx.gridlayout.widget.GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="202dp"
tools:layout_editor_absoluteY="197dp">
</androidx.gridlayout.widget.GridLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
I guess this the what you want to do
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/constraint1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/startButton"
android:layout_width="203dp"
android:layout_height="110dp"
android:background="#02F837"
android:onClick="start"
android:text="Go!"
android:textColor="#000000"
android:textSize="50sp"
android:visibility="visible"
app:backgroundTint="#00F10A"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="OnClick"
android:layout_marginTop="200dp"/>
<TextView
android:id="#+id/timerTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#F4DC01"
android:gravity="center"
android:padding="5dp"
android:text="30s"
android:textColor="#000000"
android:textSize="35sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.046"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.029" />
<TextView
android:id="#+id/pointsTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#F4DC01"
android:gravity="center"
android:padding="5dp"
android:text="0/0"
android:textColor="#000000"
android:textSize="35sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.953"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.029" />
<TextView
android:id="#+id/sumTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#58F15E"
android:gravity="center"
android:padding="15dp"
android:text="31 + 12"
android:textColor="#000000"
android:textSize="35sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.03" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/constraint2"
android:layout_marginTop="50sp"
app:layout_constraintTop_toBottomOf="#id/constraint1">
<androidx.gridlayout.widget.GridLayout
android:id="#+id/gridLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="202dp">
<!--
now you can add whatever button you want to add here
-->
</androidx.gridlayout.widget.GridLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

Tab indicator no longer at the bottom of AppBarLayout after migrating app to AndroidX. The tab is custom made tab

I migrated android app to AndroidX and then I noticed this
As you can see the tab indicator went up into the AppBarLayout. Here is the code
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
android:paddingTop="#dimen/appbar_padding_top"
android:theme="#style/AppTheme.AppBarOverlay"
android:elevation="4dp">
<androidx.appcompat.widget.Toolbar
... // some code here relevant to toolbar
</androidx.appcompat.widget.Toolbar>
<com.google.android.material.tabs.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout="#layout/custom_tab" />
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout="#layout/custom_tab" />
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout="#layout/custom_tab"/>
</com.google.android.material.tabs.TabLayout>
</com.google.android.material.appbar.AppBarLayout>
The custom layout I created is below:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/tab_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="CONTRIBUTIONS"
style="#style/TextAppearance.Design.Tab"/>
<TextView
android:id="#+id/tv_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="#id/tab_title"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:text="1000"
android:layout_marginBottom="8dp"
style="#style/TextAppearance.Design.Tab"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="4dp"
android:layout_marginRight="4dp"
android:text="#string/open_bracket"
app:layout_constraintBottom_toBottomOf="#+id/tv_number"
app:layout_constraintEnd_toStartOf="#+id/tv_number"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/tv_number"
app:layout_constraintHorizontal_bias="1"
style="#style/TextAppearance.Design.Tab"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="4dp"
android:layout_marginLeft="4dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="#string/closing_bracket"
app:layout_constraintBottom_toBottomOf="#+id/tv_number"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/tv_number"
app:layout_constraintTop_toTopOf="#+id/tv_number"
app:layout_constraintHorizontal_bias="0"
style="#style/TextAppearance.Design.Tab"/>
</androidx.constraintlayout.widget.ConstraintLayout>`
It looks fine in android 22 and below but not android 24 and above. However before this migration to androidx it was fine in all versions i.e the indicator was at bottom of appbarlayout not inside it.
You must set app:tabIndicatorColor="YOUR_COLOR" in TabLayout like this:
<com.google.android.material.tabs.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabIndicatorColor="#color/blue">
... // Your TabItems
</com.google.android.material.tabs.TabLayout>
if doesn't work, try to remove android:layout="#layout/custom_tab" from TabItems too.

Is the below xml file right?

Is the below xml file right? I am trying to display information that I have fetched from sqlite in recycler view.
Is the below xml file right? I am trying to display information that I have fetched from sqlite in recycler view.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="150dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView2"
android:layout_width="3dp"
android:layout_height="23dp"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.3" />
<TextView
android:id="#+id/semester"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.15"
app:layout_constraintStart_toEndOf="#+id/textView2"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.3" />
<TextView
android:id="#+id/textView32"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SGPA"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.7" />
<TextView
android:id="#+id/textView33"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Percentage"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.6"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.70" />
<TextView
android:id="#+id/textV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="#+id/textView32"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.08"
app:layout_constraintStart_toEndOf="#+id/textView32"
app:layout_constraintTop_toTopOf="#+id/textView32"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="#+id/textVw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="#+id/textView33"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.08"
app:layout_constraintStart_toEndOf="#+id/textView33"
app:layout_constraintTop_toTopOf="#+id/textView33" />
<TextView
android:id="#+id/scheme1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.95"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.95" />
<ImageButton
android:id="#+id/btndelete1"
android:layout_width="30dp"
android:layout_height="40dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.96"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.05"
app:srcCompat="#android:drawable/ic_menu_delete" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
This is what I want:
And this is what I am getting:
I don't know why. Can someone help me out?
Also, if needed, this is where I am calling the recycler view:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
tools:context=".drawernav.bottom_navi.recycler_view.sgpa_frag">
<!-- TODO: Update blank fragment layout -->
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rc1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</androidx.recyclerview.widget.RecyclerView>
<TextView
android:id="#+id/else2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="It's a bit lonely here..."
android:gravity="center"
android:visibility="gone"/>
</FrameLayout>
Try to use this layout:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp">
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.2" />
<TextView
android:id="#+id/textView2"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="1"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="#+id/btndelete1"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/btndelete1"
app:layout_constraintVertical_bias="0.3" />
<TextView
android:id="#+id/semester"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="2"
app:layout_constraintBottom_toBottomOf="#+id/textView2"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.15"
app:layout_constraintStart_toEndOf="#+id/textView2"
app:layout_constraintTop_toTopOf="#+id/textView2"
app:layout_constraintVertical_bias="0.3"
app:layout_constraintWidth_percent="0.1" />
<TextView
android:id="#+id/textView32"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="SGPA"
app:layout_constraintBottom_toTopOf="#+id/guideline"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="#+id/textView2"
app:layout_constraintHorizontal_bias="0.2"
app:layout_constraintStart_toStartOf="#+id/textView2"
app:layout_constraintTop_toBottomOf="#+id/textView2"
app:layout_constraintVertical_bias="0.7"
app:layout_constraintWidth_percent="0.1" />
<TextView
android:id="#+id/textView33"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="Percentage"
app:layout_constraintBottom_toBottomOf="#+id/textView32"
app:layout_constraintEnd_toStartOf="#+id/btndelete1"
app:layout_constraintHorizontal_bias="0.6"
app:layout_constraintStart_toEndOf="#+id/semester"
app:layout_constraintTop_toTopOf="#+id/textView32"
app:layout_constraintVertical_bias="0.70"
app:layout_constraintWidth_percent="0.18" />
<TextView
android:id="#+id/textV"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="3"
app:layout_constraintBottom_toBottomOf="#+id/textView32"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="#+id/semester"
app:layout_constraintHorizontal_bias="0.08"
app:layout_constraintStart_toStartOf="#+id/semester"
app:layout_constraintTop_toTopOf="#+id/textView32"
app:layout_constraintVertical_bias="0.0"
app:layout_constraintWidth_percent="0.1" />
<TextView
android:id="#+id/textVw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5"
app:layout_constraintBottom_toBottomOf="#+id/textView33"
app:layout_constraintEnd_toStartOf="#+id/btndelete1"
app:layout_constraintHorizontal_bias="0.08"
app:layout_constraintStart_toEndOf="#+id/textView33"
app:layout_constraintTop_toTopOf="#+id/textView33" />
<TextView
android:id="#+id/scheme1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
android:textSize="10sp"
app:layout_constraintEnd_toEndOf="#+id/btndelete1"
app:layout_constraintStart_toStartOf="#+id/btndelete1"
app:layout_constraintTop_toTopOf="#+id/guideline" />
<ImageButton
android:id="#+id/btndelete1"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.96"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.05"
app:layout_constraintWidth_percent="0.1"
app:srcCompat="#android:drawable/ic_menu_delete" />
</androidx.constraintlayout.widget.ConstraintLayout>
I have used those attributes to make your layout responsive:
app:layout_constraintGuide_percent="0.2"
app:layout_constraintDimensionRatio="1:1"
In addition, I added Guideline
And it will look like this:

How to align in center of a space using constraint layout

I have the following layout file that uses a ConstraintLayout
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:id="#+id/main_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is my header text and is long"
android:textSize="20sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<LinearLayout
android:id="#+id/vertical_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/main_header"
android:layout_marginTop="15dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Foo foo fo"
android:textSize="18sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bar bar bar"
android:textSize="18sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bar bar bar"
android:textSize="18sp"
/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="centered text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#id/vertical_layout"
app:layout_constraintTop_toBottomOf="#id/main_header"
/>
</android.support.constraint.ConstraintLayout>
The result is the following:
How can I make the highlighted centered text come to the center of that white space next to the vertical linear layout? (approx where the red arrow points?
Easiest way would be to constrain the top and the bottom of the TextView to the respective edges of the LinearLayout so that it's centered vertically between them, even when the LinearLayout's height changes. The horizontal constraints are fine as they are right now. The constraints for the TextView would look like this:
app:layout_constraintBottom_toBottomOf="#id/vertical_layout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#id/vertical_layout"
app:layout_constraintTop_toTopOf="#id/vertical_layout"
Result:
You can simply add guideline like this :
Before you look at the layout lets look on how guidelines work:
You can think of them as invisible views that won't affect your layout, from the documentation:
a guideline is a visual guide which will not be seen at runtime that is used to align other views too.
So how I did it - I created a guideline (horizontal in my case) and told him to be at 20% height of the screen - app:layout_constraintGuide_percent="0.2" and after that I connected constraint to it and now your view is centered between the top of your screen and your guidline.
<androidx.constraintlayout.widget.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">
<TextView
android:id="#+id/main_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is my header text and is long"
android:textSize="20sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:id="#+id/vertical_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:orientation="vertical"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/main_header">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Foo foo fo"
android:textSize="18sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bar bar bar"
android:textSize="18sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bar bar bar"
android:textSize="18sp" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:text="centered text"
app:layout_constraintBottom_toTopOf="#+id/guideline3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#id/vertical_layout"
app:layout_constraintTop_toBottomOf="#id/main_header" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.2" />
But , please avoid using nested views because this is not what constraintLayout is for.
Here is example for layout without nested view groups:
<androidx.constraintlayout.widget.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">
<TextView
android:id="#+id/main_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is my header text and is long"
android:textSize="20sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:text="Foo foo fo"
android:textSize="18sp"
app:layout_constraintBottom_toTopOf="#+id/guideline3"
app:layout_constraintEnd_toEndOf="#+id/textView8"
app:layout_constraintStart_toStartOf="#+id/textView8"
app:layout_constraintTop_toBottomOf="#+id/textView8" />
<TextView
android:id="#+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bar bar bar"
android:textSize="18sp"
app:layout_constraintBottom_toTopOf="#+id/textView11"
app:layout_constraintEnd_toEndOf="#+id/textView10"
app:layout_constraintStart_toStartOf="#+id/textView10"
app:layout_constraintTop_toBottomOf="#+id/textView10" />
<TextView
android:id="#+id/textView10"
android:layout_width="wrap_content"
android:layout_height="24dp"
android:layout_marginStart="8dp"
android:text="Bar bar bar"
android:textSize="18sp"
app:layout_constraintBottom_toTopOf="#+id/textView8"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/main_header" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="centered text"
app:layout_constraintBottom_toTopOf="#+id/guideline3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#id/main_header" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.2" />
It is a constraint layout do it with adding a constraint at the top of the text view. Check out this https://developer.android.com/training/constraint-layout to know details about how constraint layout work.
Just added marginTop and solved your prooblem
Here is your layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:id="#+id/main_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is my header text and is long"
android:textSize="20sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<LinearLayout
android:id="#+id/vertical_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/main_header"
android:layout_marginTop="15dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Foo foo fo"
android:textSize="18sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bar bar bar"
android:textSize="18sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bar bar bar"
android:textSize="18sp"
/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:text="centered text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#id/vertical_layout"
app:layout_constraintTop_toBottomOf="#id/main_header" />
</android.support.constraint.ConstraintLayout>
Here is the image

Resources