2

I'm trying to implement ProgressBar in Navigation Drawer Activity but it is covering the whole screen like this.

enter image description here

I try to set style="?android:attr/progressBarStyleSmall" in my ProgressBar but no success

My XML file

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_home_screen"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_home_screen"
        app:menu="@menu/activity_home_screen_drawer" />

    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleSmall"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_centerHorizontal="true" />

</android.support.v4.widget.DrawerLayout>

This question is not duplicate of Android ProgressBar size take whole screen

Shashanth
  • 4,995
  • 7
  • 41
  • 51
Kartik Agarwal
  • 1,129
  • 1
  • 8
  • 27
  • Your layout is incorrect See https://developer.android.com/training/implementing-navigation/nav-drawer. – ADM Sep 07 '18 at 13:52

3 Answers3

1

Finally done

When creating new NavigationDrawer Activity, Android Studio generates 4 XML files. Earlier I was implementing ProgressBar in activity_home_screen.xml. Now after trying to implement in content_home_screen.xml, it worked

Note: HomeScreenActivity is my Activity name

My content_home_screen.xml file

<?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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".HomeScreenActivity"
    tools:showIn="@layout/app_bar_home_screen">

    <LinearLayout
        android:id="@+id/header_search"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:orientation="horizontal"
        android:paddingEnd="16dp"
        android:weightSum="5">

       <!-- Some Code.... -->

    </LinearLayout>

    <View
        android:id="@+id/content_view"
        android:layout_width="wrap_content"
        android:layout_height="2dp"
        android:layout_below="@id/header_search"
        android:background="@drawable/gradient_view" />

    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyleSmall"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_centerInParent="true" />

</RelativeLayout>
Kartik Agarwal
  • 1,129
  • 1
  • 8
  • 27
0

This is happened due to hardcoded size of Progress bar

android:layout_width="100dp" android:layout_height="100dp"

Try this:

<ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true" />
Sunny
  • 3,134
  • 1
  • 17
  • 31
0

Put your ProgressBar inside NavigationView:

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_home_screen"
    app:menu="@menu/activity_home_screen_drawer">

    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleSmall"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_centerHorizontal="true" />

</android.support.design.widget.NavigationView>
TheWanderer
  • 16,775
  • 6
  • 49
  • 63
  • Same idea here, anywhere but in the `DrawerLayout ` – ChristopheCVB Sep 07 '18 at 13:46
  • after implementing this Progress bar is not showing on the screen – Kartik Agarwal Sep 07 '18 at 14:50
  • it should be shown in the DrawerLayout now @KartikAgarwal. There's no guarantee though, as you're not supposed to do this. It looks like the only option may be to get the header view and add your progress bar programmatically to the header – TheWanderer Sep 07 '18 at 14:52
  • yes, now the progressBar is coming in Drawer Header. But I don't want this. Thanks for suggestion – Kartik Agarwal Sep 07 '18 at 15:01
  • Actually, according to the answer [here](https://stackoverflow.com/questions/30626324/navigationview-and-custom-layout), you should be able to put a custom View inside. Try wrapping the ProgressBar in a LinearLayout. – TheWanderer Sep 07 '18 at 15:05