Background
Suppose you have a scrolling activity, with CoordinatorLayout and AppBarLayout, where you have a top header that can collapse upon scrolling at the bottom (which might have a RecyclerView or NestedScrollView).
Something like this:
Now, you need to put a loading view (say, a ProgressBar and a TextView inside a LinearLayout) in the center of the bottom area (where the text is shown), before the content appears.
The problem
I used a ViewAnimator to switch between the states, but if I choose to center the loading view, it is shown below the center, because the bottom area takes more space that is really shown (as you can scroll it).
What I tried
There are 2 solutions I've conducted that worked fine: 1. Get the size of the loading view and its parent, and use it to calculate the top margin of the loading view 2. Set the bottom padding of the loading view to be half of the upper area size (90dp in this case, which is half of the 180dp of the upper area). This is a bit easier, but if the UI of anything in the activity or the parent of the bottom area changes, it will ruin the fix for being in the center.
The code
Almost the same as the one from the IDE wizard, but here:
ScrollingActivity.java
public class ScrollingActivity extends AppCompatActivity {
private ViewAnimator mViewAnimator;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scrolling);
setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
mViewAnimator = findViewById(R.id.viewAnimator);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_scrolling, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
mViewAnimator.showNext();
return true;
}
return super.onOptionsItemSelected(item);
}
}
res/layout/activity_scrolling.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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" android:fitsSystemWindows="true"
tools:context="com.example.user.myapplication.ScrollingActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar" android:layout_width="match_parent" android:layout_height="@dimen/app_bar_height"
android:fitsSystemWindows="true" android:theme="@style/AppTheme.AppBarOverlay">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/toolbar_layout" android:layout_width="match_parent" android:layout_height="match_parent"
android:fitsSystemWindows="true" app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed" app:toolbarId="@+id/toolbar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar" android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize" app:layout_collapseMode="pin"
app:popupTheme="@style/AppTheme.PopupOverlay"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<ViewAnimator
android:id="@+id/viewAnimator" android:layout_width="match_parent" android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:id="@+id/loader" android:layout_width="match_parent" android:layout_height="match_parent"
android:gravity="center" android:orientation="vertical">
<ProgressBar
android:layout_width="wrap_content" android:layout_height="wrap_content"/>
<TextView
android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="loading"/>
</LinearLayout>
<android.support.v4.widget.NestedScrollView
android:id="@+id/contentView" android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_margin="@dimen/text_margin" android:text="@string/large_text"/>
</android.support.v4.widget.NestedScrollView>
</ViewAnimator>
</android.support.design.widget.CoordinatorLayout>
res/menu/menu_scrolling.xml
<menu 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" tools:context="com.example.user.myapplication.ScrollingActivity">
<item
android:id="@+id/action_settings" android:orderInCategory="100" android:title="click"
app:showAsAction="always"/>
</menu>
The question
Thing is, I could calculate things myself, but I wonder if there is a different, easy way to do it. Maybe a flag of some sort that I'm unaware of ?
I ask this because the bottom area in the real app is more complex (Sample above is for demonstration of the issue), having a fragment or even a TabLayout&ViewPager with multiple fragments, so it's a bit weird to calculate things of the parent Activity in those fragments for this purpose.
Not only that, but I also have a tabLayout at the bottom (belongs to the activity) which makes it even more annoying to have into account.