-1

In this code i want to use a ring progressdialog for open and loading each activity.

My code not work well. In my code, progressbar closed and activity open after long time.

I want to add a progressdialog in onListItemClick, plz help me

public class Listsoti extends ListActivity {

String[] str = {"1","2","3","4","5","6","7","8","9","10","11","12","13","14"};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listview);

    TextView list = (TextView) findViewById(R.id.hamid);
    PersianReshape.ReshapeTextview(list, "hamid1.ttf", Listsoti.this);
    setListAdapter(new MyAdapter(this,
            android.R.layout.simple_list_item_1,R.id.textView1, str));
}

private class MyAdapter extends ArrayAdapter<String>{

    public MyAdapter(Context context, int resource, int textViewResourceId,
            String[] strings) {
        super(context, resource, textViewResourceId, strings);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = inflater.inflate(R.layout.listsoti, parent, false);
        String[] items = getResources().getStringArray(R.array.contor);

        ImageView iv = (ImageView) row.findViewById(R.id.imageView1);
        TextView tv = (TextView) row.findViewById(R.id.textView1);
        PersianReshape.ReshapeTextview(tv,"hamid1.ttf", Listsoti.this);
        tv.setText(items[position]);

        switch (position) {
        case 0:
           iv.setImageResource(R.drawable.dozdeh);
           break;
        case 1:
            iv.setImageResource(R.drawable.download);
            break;
        default:
            break;
        }
        return row;
    }
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    Toast.makeText(this, str[position] +" your choose", Toast.LENGTH_LONG).show();
    switch (position) {
    case 0:
        startActivity(new Intent(Listsoti.this, Play.class));

        break;
    case 1:
        final Dialog dialog = new Dialog(this);

        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
        dialog.setContentView(R.layout.progressdialog);
        dialog.show();
        startActivity(new Intent(Listsoti.this, Play.class));


        break;
    default:
        break;
        }
}

EDIT, loading class

public class LoadingScreenActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.out.println("LoadingScreenActivity  screen started");
setContentView(R.layout.loading_screen);
Spinner s = (Spinner) findViewById(R.id.mainSpinner1);
s.setVisibility(View.VISIBLE);
//do work if needed and then launch new activity
Intent mainIntent = new Intent(LoadingScreenActivity.this,Play.class); 
    LoadingScreenActivity.this.startActivity(mainIntent); 
    LoadingScreenActivity.this.finish(); 
}

}

H.A.M.I.D
  • 29
  • 1
  • 6

1 Answers1

1

You can use this example, this is LoadingActivity, which you create every time you need to show ProgressDialog'. You can customize it by passing activity name throughIntentwhich is gonna be launched next. So you will be able to reuse thisActivity` many times. Check it, please.

  1. Create a LoadingScreen layout file

Here you create an screen which just shows loading text and an progress bar loading_screen.xml

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"    android:gravity="center" android:orientation="vertical"
  android:layout_height="fill_parent" android:background="#E5E5E5">

   <TextView android:text="Please wait while your data gets loaded..."
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:textColor="#000000">
   </TextView>
  <ProgressBar android:id="@+id/mainSpinner1" android:layout_gravity="center" 
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:indeterminate="true" 
            style="?android:attr/progressBarStyleInverse">
   </ProgressBar>

</LinearLayout>
  1. Create a LoadingScreen class file

and in LoadingScreenActivity.class you override onCreate as usually:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    System.out.println("LoadingScreenActivity  screen started");
    setContentView(R.layout.loading_screen);

    Spinner s = findViewById(R.id.mainSpinner1);
    s.setVisibility(View.VISIBLE);

    //do work if needed and then launch new activity
    Intent mainIntent = new Intent(LoadingScreenActivity.this,ProfileData.class); 
    LoadingScreenActivity.this.startActivity(mainIntent); 
    LoadingScreenActivity.this.finish(); 
}

This will load the next activity once your custom task is finished. 3. Open LoadingScreenActivity from say your List from onListItemClick event

Create an intent to launch loading screen activity

protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);

    Intent intent = new Intent(ProfileList.this, LoadingScreenActivity.class);
    startActivity(intent);
}

UPDATE

For case to launch LoadingActivity by clicking button you should simply add this code to your current activity:

yourButton.setOnCLickListener(new OnCLickListener(){
   @Override
   public void onCLick(View view){
      Intent intent = new Intent(this, LoadingScreenActivity.class);
            startActivity(intent);
   }
}

Create dialog inside Play.class in method onCreate()

Dialog dialog = new Dialog(this);

dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
dialog.setContentView(R.layout.progressdialog);
dialog.show();

then initialize loading file and when loading is done, call this method dialog.dismiss(); and continue working.

Yurets
  • 3,999
  • 17
  • 54
  • 74
  • tanks,but in LoadingScreenActivity, ProfileData can not be resolved to a type – H.A.M.I.D Apr 25 '15 at 17:37
  • @H.A.M.I.D, because it is example, you should replace it with your "next" `Activity` name. – Yurets Apr 25 '15 at 17:41
  • is it Possible that give me cods that, when i touch a button show a progress dialog while open new activity?,not for a list only for a button – H.A.M.I.D Apr 25 '15 at 17:58
  • This is the dialog within new activity, to perform heavy tasks. While performing task the user will see progress dialog. but if you don't have heavy tasks to do in background you shouldn't create progress dialog, it is going fast and nobody do that unless your device is really slow, but now devices are strong enough to not do that. – Yurets Apr 25 '15 at 18:01
  • I mean if you don't have task and you will just try to set dialog, user even will not be able to see it, because process of launching new activity is very fast. – Yurets Apr 25 '15 at 18:02
  • no, in my activity a heavy music file be come loaded – H.A.M.I.D Apr 25 '15 at 18:14
  • This way you do everything right, just put this task in your NEW activity inside of method `onCreate()` and show progress dialog there. This issue will disappear. – Yurets Apr 25 '15 at 18:18
  • is my LoadingScreen class is correct?, in my edit in Question – H.A.M.I.D Apr 25 '15 at 18:23
  • forget about loading class, in your case it would be not so efficient. read the part after title "EDIT". This is the only one thing you should change. Show dialog not in old activity, but in new one. – Yurets Apr 25 '15 at 18:32