Dr.Noname
Dr.Noname

Reputation: 73

Progressbar when load activity

I have some problem. In main activity I create buttons

 Button button=new Button(this);
                button.setId(i);
                button.setWidth(20);
                button.setBackgroundColor(Color.TRANSPARENT);
                button.setLayoutParams(params);
                button.setText(nazv);
                button.setTextSize(22);
                button.setTextColor(0xFF2C85A6);
                button.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View view) {

                        Intent is = new Intent(getApplicationContext(), termin.class);
                        is.putExtra("LetVariable", nazv);
                        startActivity(is);
                    }
                });

LetVariable - is a first letter of term. It is transmitted in the Activiti termin, Where there is a choice of the data set for this letter.

public class termin extends AppCompatActivity {

        String[] mArray;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.termin);
            getSupportActionBar().hide();
            Intent mIntent = getIntent();
            String LetValue = mIntent.getStringExtra("LetVariable");

            int length =0;

            length =getResources().getStringArray(R.array.terms).length;

            mArray = getResources().getStringArray(R.array.terms);
            final LinearLayout linearLayout=(LinearLayout)findViewById(R.id.buttonlayout);

            TextView opisTextView = (TextView)findViewById(R.id.TermTextView);
            opisTextView.setText("Terms starting with "+LetValue);

            for(int i=0;i<length;i++){

                //

                final String nazv = mArray[i];
                final String[] splittedItem = nazv.split("::");
                final String fLet=Character.toString(splittedItem[0].charAt(0));




                if(fLet.equals(LetValue))

                {

                Button button=new Button(this);
                button.setId(i);



                    button.setText(splittedItem[0]);
                    button.setBackgroundColor(Color.TRANSPARENT);
                    button.setTextSize(19);
                    button.setGravity(Gravity.LEFT);
                    button.setTextColor(0xFF2C85A6);
                    button.setOnClickListener(new View.OnClickListener() {
                        public void onClick(View view) {

                            Intent is = new Intent(getApplicationContext(), termin_full.class);
                            is.putExtra("fVariableName", nazv);
                            startActivity(is);
                        }
                    });


                linearLayout.addView(button);
                }
            }


        }
    }

But as in an array of many words, at the time of Screen "freezes". How to make standard progresbar at the time of this fading? To understand the user that the application works.

Upvotes: 0

Views: 391

Answers (1)

RoLAN210
RoLAN210

Reputation: 68

Use an AsyncTask.

https://developer.android.com/reference/android/os/AsyncTask.html

You will most likely have to rearrange your code a bit, but if your for-loop is long running that is what is "freezing" your app. AsyncTask has 3 methods to accomplish this. In onPreExecute show your progress bar. Then in doInBackground() you will need to perform your for loop logic. In onPostExecute() you can add all the buttons you created to your view. You must only modify UI in the pre/post execute methods as they are executed on the UI thread whereas doInBackground is not executed in the UI thread and will throw an exception if you attempt to modify the UI.

Upvotes: 1

Related Questions