tags:

views:

7417

answers:

4

Hi

I am using a custom title view and would like to show/hide a progressbar in the title view when a thread is working.

This is my title view's XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
>
    <Button
        android:id="@+id/image_left_btn"
        android:layout_width="75dip" 
        android:layout_height="wrap_content" 
        android:text="Back"
    />
    <TextView
     android:id="@+id/image_title_text"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="1"
     android:textSize="20dip"
        android:textStyle="bold"
        android:textColor="#fff"
        android:layout_gravity="center"
        android:gravity="center"
        android:paddingLeft="8dip"
        android:paddingRight="8dip"
    />
    <ProgressBar 
     android:id="@+android:id/progress_small_title"
        style="?android:attr/progressBarStyleSmall"
        android:layout_width="75dip"
        android:layout_height="wrap_content" 
        android:paddingRight="8dip"/>
</LinearLayout>

In my Activity, after setting this as the custom title bar, I do this

titleProgress = (ProgressBar)findViewById(R.id.progress_small_title);
titleProgress.setVisibility(View.INVISIBLE);

where titleProgress is ProgressBar object.

This is what I do in my thread

runOnUiThread(new Runnable() {
    public void run() {
     titleProgress.setVisibility(View.VISIBLE);
    }
});
//long operation here
runOnUiThread(new Runnable() {
    public void run() {
     titleProgress.setVisibility(View.INVISIBLE);
    }
});

But there is no change to the progress bar. It is never displayed. Can someone please tell me what is wrong with the code?

Is it possible to display the title progressbar in a custom title?

Thanks.

A: 

Try changing the style of the ProgressBar to horizonal:

style="?android:attr/progressBarStyleHorizontal"

The original

style="?android:attr/progressBarStyleSmall"

gives a circular "progressbar" which can't be fully seen in the titlebar.

AshtonBRSC
I even tried with progressBarStyleSmallTitle which is supposed to fit into the default title bar. Even this does not show.
lostInTransit
+1  A: 

What you should use in this case is AsyncTask, see http://developer.android.com/reference/android/os/AsyncTask.html

If you're not targeting a pre-1.5 version of Android, you will have to use a different class called UserTask. See http://android-developers.blogspot.com/2009/05/painless-threading.html

Eddified
+2  A: 

Few Things to try:

1) (This probably isn't it) Make sure "titleProgress" is volatile.

2) Try throwing a few 'postInvalidate()' or 'titleProgress.postInvalidate()' in there to trigger a redraw.

3) Have you sacraficed a x486 machine on an alter resembling a giant green robot? (just kidding)

Let me know if those first two (and if you're really desperate, the third) get you anywhere.

haseman
Already tried the first two but didn't help! And desperate I am so I guess that calls for 3? ;)
lostInTransit
A: 

Is your layout correct? Did you try to get this working with a vertical layout first? Just to see if the progress bar is visible when you start your activity?

MarvinLabs