AviAndroid AviAndroid - 21 days ago 6
Android Question

How to give gravilty center to progressbar inside scroll view?

I don't know what's wrong with my code I am unable to put my progress bar center to the screen . This is my XML code I just want to put progress bar before loading the webWiew page . All things are working fine but progress bar did not centralised .

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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:showIn="@layout/app_bar_aboutus">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">

<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
app:srcCompat="@drawable/pinnainfotech_logo" />


<WebView
android:id="@+id/webview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"


android:layout_marginTop="0dp" />
<ProgressBar
android:layout_height="wrap_content"
android:layout_width="60dp"


android:gravity="center_vertical|center_horizontal"


android:id="@+id/progressBar1"/>



</LinearLayout>



</ScrollView>


Thanks in advanced

Answer

Keep your progressbar out of scrollview, Reason: when scrolling the view then the progressbar should be at center always.

I have edited your xml view and it looks like this.

<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
    android:id="@+id/progressBar1"
    android:layout_width="60dp"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true" />

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerHorizontal="true"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="20dp"
            app:srcCompat="@drawable/pinnainfotech_logo" />

        <WebView
            android:id="@+id/webview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="0dp"
            android:gravity="center" />
    </LinearLayout>
</ScrollView>

Comments