user1923321
user1923321

Reputation:

How to show Android progress bar only sometimes

I want to show the progress bar only while new MainActivity(detail). MainActivity(detail) is used to send mail to users. All I need is when the button is clicked, the progress bar should be displayed and start. Then when mail is sent, the progress bar hides or ends.

Here is my code so far:

OnClickListener listener=new OnClickListener()
    {   
        @Override
        public void onClick(View v)
        {
        pb=(ProgressBar)findViewById(R.id.progressBar1);
        pb.setVisibility(View.VISIBLE);

        if(isNetworkAvailable())
        {
        // TODO Auto-generated method stub
            SharedPreferences pref=getSharedPreferences("Pref", MODE_PRIVATE);
            String str=pref.getString("un1", "");
            String str1=pref.getString("pa1", "");

            SQLiteDatabase db=context.openOrCreateDatabase("IPCA", MODE_PRIVATE,null);
            Databasesignup dbs=new Databasesignup(context,"IPCA", null,1);
            dbs.onCreate(db);

            Cursor cursor=dbs.view(db,str,str1);

            ArrayList<String> al=new ArrayList<String>();
            while(cursor.moveToNext())
            {
                al.add(cursor.getString(0));
                al.add(cursor.getString(1));
                al.add(cursor.getString(5));
            }

            String string=al.get(0);
            String string1=al.get(1);
            String string2=al.get(2);
            String k=iv.getTag().toString();

            dbs.insert1(db,string,str,string2,k);

            final String detail ="\nName: "+string+"\nEmail: "+string1+"\nMobile: "+string2+"\nInterest: "+k;
            new MainActivity(detail);

            cursor.close();
            db.close();

            Toast.makeText(getApplicationContext(),"You will be contacted soon", Toast.LENGTH_LONG).show();
        }`

Upvotes: 0

Views: 313

Answers (2)

CodePrimate
CodePrimate

Reputation: 6666

You hide a ProgressBar by changing the visibility of it.

ProgressBar pBar = (ProgressBar)findViewById(R.id.your_progress_bar);
// To hide the Progress Bar set visibility to GONE.
pBar.setVisibility(View.GONE);
// To show the Progress Bar again set visibility to VISIBLE
pBar.setVisibility(View.VISIBLE);

Upvotes: 0

323go
323go

Reputation: 14274

Since you're accessing the network, you'll need to move the entire operation into the background. This is easiest using an AsyncTask. You can then display your ProgressBar in onPreExecute() and hide it in onPostExecute().

Upvotes: 2

Related Questions