Skip to content Skip to sidebar Skip to footer

How To Place Views Exactly In The Center In Coordinator Layout?

I have a fragment with recycler view and a textview which will be shown when no data is available. fragment is hosted in the activity which is having coordinator layout. Now the pr

Solution 1:

just add this to your view's attribute :

    android:layout_gravity="center" 

Solution 2:

Here try this:

<FrameLayout 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">
<LinearLayout
        android:id="@+id/llEmpty"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:orientation="vertical">
<TextView
        android:id="@+id/empty_text_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="14sp"
        android:visibility="gone"
        android:padding="@dimen/margin"
        android:text="@string/empty_string_fav"
        android:gravity="center"/>
    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:visibility="gone"
        android:id="@+id/progress_bar"/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/recycler_favourite">
</android.support.v7.widget.RecyclerView>


</FrameLayout>

Solution 3:

try to add this on your textview:

android:layout_centerVertical="true"
android:layout_centerHorizontal="true"

hope it helps.


Solution 4:

Try framelayout.

<FrameLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:gravity="center">

      <ProgressBar
          android:id="@+id/progressBar"
          android:layout_width="40dp"
          android:layout_height="40dp"
          android:layout_gravity="center" />

      <TextView
          android:id="@+id/empty"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_gravity="center"
          android:textColor="@color/black" />
</FrameLayout> 

Solution 5:

A really simple thing you can do is to simply add the bottom padding to your textview 'empty_text_tv'. In your case you can set it to ?attr/actionBarSize. At least this way the text will be centered when the toolbar is not collapsed. It looks much better.

If you need the textview be always centered, you have to addOnOffsetChangedListener to your AppBarLayout. In this listener's 'onOffsetChanged' method take the argument 'verticalOffset' and set the textview's translationY to verticalOffset / 2 (probably with some delegation/event subscription techniques).


Post a Comment for "How To Place Views Exactly In The Center In Coordinator Layout?"