Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - displaying text in center of progress bar

I have already made a horizontal progress bar and it works perfectly. I would like to display a textview or something similar right in the middle of it showing a countdown as the bar is loading. Keep in mind its not a progress dialog, progress bar resides inside an activity and shows a countdown timer. Can anyone help me out, whats the best way to do this and how can I accomplish this?

like image 441
ahmad Avatar asked Aug 23 '13 20:08

ahmad


People also ask

How do I center my android ProgressBar?

Just change your Layout as RelativeLayout and add android:layout_centerInParent="true" to your ProgressBar.

What are types of ProgressBar in android?

Progress bar supports two modes to represent progress: determinate, and indeterminate. For a visual overview of the difference between determinate and indeterminate progress modes, see Progress & activity.


3 Answers

If your ProgressBar and TextView are inside a RelativeLayout you can give the ProgressBar an id, and then align the TextView with the ProgressBar using that. It should then show on top of the ProgressBar. Make sure the background is transparent so that you can still see the ProgressBar

For example:

<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"     >     <ProgressBar         android:layout_width="match_parent"         android:layout_height="match_parent"         // other attributes         android:id="@+id/PROGRESS_BAR"     >     <TextView         android:background="#00000000" // transparent         // other attributes         android:layout_alignLeft="@id/PROGRESS_BAR"         android:layout_alignTop="@id/PROGRESS_BAR"         android:layout_alignRight="@id/PROGRESS_BAR"         android:layout_alignBottom="@id/PROGRESS_BAR"     > </RelativeLayout> 
like image 161
Matt Harris Avatar answered Sep 20 '22 05:09

Matt Harris


You can use a FrameLayout to display the TextView over the ProgressBar:

...
<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ProgressBar
        android:id="@+id/progress"
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:progressDrawable="@drawable/progressbar" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true" />
        ...
    </RelativeLayout>
</FrameLayout>
like image 24
Yann Avatar answered Sep 19 '22 05:09

Yann


You can set LinearLayout with RelativeLayout inside it and make RelativeLayout Height & width equal to wrap_content then in the TextView under the ProgressBar you will add android:layout_centerInParent="true"

Example :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center">
        <ProgressBar
            android:id="@+id/progressBar"
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:max="100"
            android:progress="65" />
        <TextView
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_centerInParent="true"
            android:textColor="@color/white"
            android:text="6:00 AM"
            android:textSize="25sp"
            android:textStyle="bold"/>
    </RelativeLayout>
</LinearLayout>
like image 24
Yousef Elsayed Avatar answered Sep 22 '22 05:09

Yousef Elsayed