The easiest way is to add the ProgressBar directly to the XML-Layout files.
1. One-time solution
Using RelativeLayout as root and use android:layout_below to save the ProgressBar and main content under the toolbar.
<RelativeLayout 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.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?attr/colorPrimaryDark"/> <fr.castorflex.android.smoothprogressbar.SmoothProgressBar android:id="@+id/loadProgressBar" style="@style/LoadProgressBar" android:layout_width="match_parent" android:layout_height="4dp" android:layout_below="@+id/toolbar" android:indeterminate="true"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/toolbar" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Content Text"/> </LinearLayout> </RelativeLayout>
You can now access the Toolbar and ProgressBar in the Activitiy onCreate method
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); toolbar = (Toolbar) findViewById(R.id.toolbar); progressBar = (SmoothProgressBar) findViewById(R.id.loadProgressBar); if (toolbar != null) { setSupportActionBar(toolbar); } }
2. General solution using include
A more general approach is to put the Toolbar and ProgressBar in a separate XML-Layout file and include it in the action layout.
toolbar.xml
<merge xmlns:android="http://schemas.android.com/apk/res/android"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?attr/colorPrimaryDark"/> <fr.castorflex.android.smoothprogressbar.SmoothProgressBar android:id="@+id/loadProgressBar" style="@style/LoadProgressBar" android:layout_width="match_parent" android:layout_height="4dp" android:layout_below="@+id/toolbar" android:indeterminate="true"/> </merge>
activity_main.xml
<RelativeLayout 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"> <include layout="@layout/toolbar"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/toolbar" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Content Text"/> </LinearLayout> </RelativeLayout>
source share