0

I am using python in my application but i am facing issue in animation of loading like progress bar is not working when i click on button the application move towards python code and java file stuck and did not respond at that time i want to show loading animation that pyhon code is in progress can any one help me out the code is under...

        if (!Python.isStarted()) {
            Python.start(new AndroidPlatform(documentupload.this));
        }
                b2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        pgb.setVisibility(View.VISIBLE);
                        if(isConnected())
                        {



                        String strUserName = et.getText().toString();
                        String numofdocword=docnum.getText().toString();

                       if(TextUtils.isEmpty(strUserName) && TextUtils.isEmpty(numofdocword))
                       {
                            et.setError("Enter total number");
                            docnum.setError("Enter maximum number of words for document");
                            return;
                       }
                       else if(Integer.parseInt(strUserName)>100){
                           Toast.makeText(documentupload.this,"Number must be less than 100",Toast.LENGTH_SHORT).show();
                       }


                       else{
                           if(upload.length()==0) {
                               Toast.makeText(documentupload.this, "Fill the required fields", Toast.LENGTH_SHORT).show();

                           }
                           else if(adapter.getItemCount()==0){
                               Toast.makeText(documentupload.this, "Words must be present", Toast.LENGTH_SHORT).show();

                           }
                           else{
                               py = Python.getInstance();
                               final PyObject pyobj = py.getModule("count");
                               final PyObject pyspellcheck = py.getModule("grammer2");
                               final PyObject pywordsimilarity=py.getModule("forwordsimilarity");
                               final PyObject pysentencesimilarity = py.getModule("newsentencesimilarity");
                               final PyObject sencount=py.getModule("word2vec");
                               final PyObject pywordspell=py.getModule("spellcheck");



                              PyObject obj =  pyobj.callAttr("main", sf);
                               String count=obj.toString();

                               PyObject spellobjgrammer = pyspellcheck.callAttr("main", sf);
                               PyObject spellwordobj = pywordspell.callAttr("main", sf);

                               List<PyObject> wordsimilarityobj = py.getModule("forwordsimilarity").callAttr("main",sf,Words.toString()).asList();
                               String s=wordsimilarityobj.get(0).toString();
                               String res2 = wordsimilarityobj.get(1).toString();
                               String res3= wordsimilarityobj.get(2).toString();

                               List<PyObject> sentencesimilarityobj = (List<PyObject>) pysentencesimilarity.callAttr("main",sf,Definitions.toString()).asList();
                               String similarsent=sentencesimilarityobj.get(0).toString();
                               String docsentcount=sentencesimilarityobj.get(1).toString();

                               PyObject sentence_countobj = sencount.callAttr("main",Definitions.toString());
                               //Toast.makeText(documentupload.this, "uploaded" + sf, Toast.LENGTH_LONG).show();
                               //String pobj = obj.toString();
                               String grammermistake = spellobjgrammer.toString();
                               //String sentencesimilar = sentencesimilarityobj.toString();
                               String sentence_count=sentence_countobj.toString();
                               String wordspellingmistake= spellwordobj.toString();

                              // Toast.makeText(documentupload.this, res3, Toast.LENGTH_LONG).show();

                               Intent intent = new Intent(documentupload.this, result.class);
                               intent.putExtra("numberwords", count);
                               intent.putExtra("spellmistake", grammermistake);
                               intent.putExtra("defitionsimilarity", similarsent);
                               intent.putExtra("wordsimilarity",s);
                               intent.putExtra("stop",res2);
                               intent.putExtra("datacount",res3);
                               intent.putExtra("sentence_count",sentence_count);
                               intent.putExtra("totalnumber",et.getText().toString());
                               intent.putExtra("wordspelling",wordspellingmistake);
                               intent.putExtra("docsentcount",docsentcount);
                               intent.putExtra("filenumberofwords",docnum.getText().toString());
                               pgb.setVisibility(View.GONE);
                               startActivity(intent);
                               finish();

                           }

                       }


                        }
                        else {
                            Toast.makeText(documentupload.this,"No internet connection",Toast.LENGTH_SHORT).show();
                        }
                    }


                });


Noman Omer
  • 53
  • 1
  • 4

1 Answers1

0

A progress bar, or any other animation, will only work if the UI thread is unblocked. So if your code takes a long time to run, you'll have to run the code on a background thread and post UI updates to the UI thread, as described in this answer.

mhsmith
  • 6,675
  • 3
  • 41
  • 58