开发者

Show a progress dialog in android tab layout

开发者 https://www.devze.com 2023-03-25 19:54 出处:网络
I need to show a progress dialog, while clicking a tab, My code id public class SIPTabWidget extends TabActivity{

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();

Show a progress dialog in android tab layout

for me before showing the Settings layout its taking some time to load the layout, so before that i need to show t开发者_运维问答he 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





    }


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
    }
    });


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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消