1

I need to show a progress dialog, while clicking a tab,

My code id

public class SIPTabWidget extends TabActivity{
 Intent intent;
  @Override
    protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);


    setContentView(R.layout.main);



    final Resources res = getResources(); // Resource object to get Drawables
    final TabHost tabHost = getTabHost(); // The activity TabHost
    TabHost.TabSpec spec; // Reusable TabSpec for each tab



    // Create an Intent to launch an Activity for the tab (to be reused)
    intent = new Intent().setClass(this, ListContacts.class);

    // Initialize a TabSpec for each tab and add it to the TabHost
    spec = tabHost
    .newTabSpec("contacts")
    .setIndicator("",
        res.getDrawable(R.drawable.tab_contacts))
        .setContent(intent);



    tabHost.addTab(spec);

    intent = new Intent().setClass(this, Dialer.class);
    spec = tabHost
    .newTabSpec("dialer")
    .setIndicator("", res.getDrawable(R.drawable.tab_dial))
    .setContent(intent);
    tabHost.addTab(spec);


    intent = new Intent().setClass(this, Favorites.class);
    spec = tabHost
    .newTabSpec("favorites")
    .setIndicator("", res.getDrawable(R.drawable.tab_favorites))
    .setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, org.sipdroid.sipua.ui.Settings.class);
    spec = tabHost
    .newTabSpec("settings")
    .setIndicator("", res.getDrawable(R.drawable.tab_settings))
    .setContent(intent);

    tabHost.addTab(spec);

}

when i click the settings tab, Setting activity is called, in its oncreate i gave

final ProgressDialog dialog = ProgressDialog.show(getParent(), "Please wait..", "Doing stuff..", true);
        dialog.setCancelable(false);
        dialog.show();

enter image description here

for me before showing the Settings layout its taking some time to load the layout, so before that i need to show the ProgressDialog, means the time when i click the Settings tab and i need to dismiss the Dialog when the tab changes to Settings

tabHost.setOnTabChangedListener(new OnTabChangeListener() {

        @Override
        public void onTabChanged(String tabId) {
        System.out.println(" Tab ID:  "+tabId);
if(tabId.equals("dial")){
dialog .dismiss();
}

    }
});

Settings.class

public class Settings extends PreferenceActivity implements OnSharedPreferenceChangeListener, OnClickListener {

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        System.out.println("Settings oncreate");
        final ProgressDialog dialog = ProgressDialog.show(getParent(), "Please  wait..", "Doing stuff..", true);
        handler = new Handler() {

              @Override
              public void handleMessage(Message msg) {
              dialog.dismiss();

              }
           };


           Thread t = new Thread() {
                public void run() {
                    //send message
                handler.sendEmptyMessage(0);      
                }
            };  





       if (Receiver.mContext == null) Receiver.mContext = this;
        addPreferencesFromResource(R.xml.preferences);
        setDefaultValues();
        Codecs.check();
        t.start





    }
jennifer
  • 8,133
  • 22
  • 69
  • 96

4 Answers4

5

Well each tab is an activity... I suppose that what is taking time is in the settings activity...

Put this in the setting activity:

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final ProgressDialog dialog = ProgressDialog.show(getParent(), "Please wait..", "Doing stuff..", true);
dialog.setCancelable(false);
dialog.show(); 
Runnable myRun = new Runnable(){
public void run(){
    Looper.myLooper().prepare();
    //DO EVERYTHING YOU WANT!

    //Finally
    runOnUiThread(new Runnable() {
    @Override
    public void run() {
    dialog.dismis();
    }
    });
}
};
Thread T = new Thread(myRun);
T.start();

Remember: In you run() in this block

//DO EVERYTHING YOU WANT!

If anything needs to change the UI, you must do it using

    runOnUiThread(new Runnable() {
    @Override
    public void run() {
        //DO IT
    }
    });
Sherif elKhatib
  • 45,786
  • 16
  • 89
  • 106
  • i have tried it on Settings tab, After but i need to show the progress dialog before the settings layout is displayed. here when i click the settings tab, there is delay for showing the layout, the progress dailog is give in the settings activity context,..but i need to show it inside the tabactivity context – jennifer Aug 02 '11 at 12:19
  • Do you think the work that is taking time is in the `onDestroy` of the previous tab ? or is it in the `onCreate` of the settings tab? – Sherif elKhatib Aug 02 '11 at 12:26
  • Runnable myRun = new Runnable(){ public void run(){ //DO EVERYTHING YOU WANT! is not calling – jennifer Aug 02 '11 at 13:21
  • i have a method inside the run method, but its not calling – jennifer Aug 02 '11 at 13:21
  • oh sorry @jennifer I edited the answer :$ I added Thread T = new Thread(myRun); T.start(); – Sherif elKhatib Aug 02 '11 at 13:28
  • 08-02 19:06:34.068: ERROR/AndroidRuntime(32315): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 08-02 19:06:34.068: ERROR/AndroidRuntime(32315): at android.os.Handler.(Handler.java:121) exception is thrown – jennifer Aug 02 '11 at 13:37
  • ok add `Looper.myLooper().prepare()` or `Looper.prepare()` in `//DO EVERYTHING YOU WANT! ` .. I will edit – Sherif elKhatib Aug 02 '11 at 13:41
  • @Sherif let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/2061/discussion-between-jennifer-and-sherif) – jennifer Aug 02 '11 at 13:53
1

you can use the asyntask feature of android to show Progress dialog refer this http://developer.android.com/reference/android/os/AsyncTask.html

Mohammed Azharuddin Shaikh
  • 41,633
  • 14
  • 96
  • 115
1

Refer to my answer here,

"Rotating wheel" progress dialog while deleting folder from SD card

or you must go for async task to do this.

Community
  • 1
  • 1
Andro Selva
  • 53,910
  • 52
  • 193
  • 240
  • i need to show the progress dialog before the settings layout is displayed. here when i click the settings tab, there is delay for showing the layout, the progress dailog is give in the settings activity context,..but i need to show it inside the tabactivity context – jennifer Aug 02 '11 at 12:19
  • Try modifying it accordingly. Try calling progressdialog.show before setContent to your activity. But this has to be done as I have done it. Use separate thread. – Andro Selva Aug 02 '11 at 12:32
  • i tries like that, an exception is thrown when i tried to close the dialog inside that handler – jennifer Aug 02 '11 at 13:23
  • 08-02 19:06:34.068: ERROR/AndroidRuntime(32315): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 08-02 19:06:34.068: ERROR/AndroidRuntime(32315): at android.os.Handler.(Handler.java:121) – jennifer Aug 02 '11 at 13:39
  • Have you written Progressdialog.show() inside thread? Don't use it inside the thread. Use it exactly as shown there – Andro Selva Aug 02 '11 at 13:45
0

If getParent() doesn't work for you, try using just TabsActivity.context (or substitute the name of your parent tab activity class). I am using nested activities and as a result using getParent() is still not returning the right context for the dialog.

After trying 20 different variations of the suggestions above I replaced this line:

AlertDialog.Builder builder = new AlertDialog.Builder(this); 

With:

AlertDialog.Builder builder = new AlertDialog.Builder(TabsActivity.context); 

and it worked like a charm. You'll also need to create the context variable in the TabsActivity class. Something like public static TabsActivity context; and context=this in the onCreate method.

Kyle Clegg
  • 38,547
  • 26
  • 130
  • 141