Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7872711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:17:25+00:00 2026-06-03T02:17:25+00:00

As the title says I am trying to get help simplifing my code. I

  • 0

As the title says I am trying to get help simplifing my code. I have multiple buttons, now only three while I test this out but I plan on having approximately thirty button choices. Currently I have a very long section of code and I think it can be shortened drastically maybe using an array? But I’m not sure how to implement it correctly. IF anyone has done this before or has an idea please share! Thanks to everyone!

public class myDL extends Activity { 

public static final int DIALOG_DOWNLOAD_PROGRESS = 0; 
private Button startBtn; 
private ProgressDialog mProgressDialog; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.filechoice); 
mProgressDialog = new ProgressDialog(myDL.this); 
mProgressDialog.setMessage("The file is now downloading, it will be saved on the SD card for future use.  Please use WiFi to avoid operator charges."); 
mProgressDialog.setIndeterminate(false); 
mProgressDialog.setMax(100); 
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 

Button section = (Button) findViewById(R.id.section); 
section.setOnClickListener(new View.OnClickListener() { 

    public void onClick(View view) { 
        // TODO Auto-generated method stub 
        Intent section = new Intent(view.getContext(), 
                section.class); 
        startActivity(section); 

    } 
}); 

Button back = (Button) findViewById(R.id.back); 
back.setOnClickListener(new View.OnClickListener() { 

    public void onClick(View v) { 
        // TODO Auto-generated method stub 
        setResult(RESULT_OK); 
        finish(); 
    } 
}); 

secOne = (Button) findViewById(R.id.secOne); 
secOne.setOnClickListener(new OnClickListener() { 
    public void onClick(View v) { 
        startSecOne(); 
    } 

}); 
} 

private void startSecOne() { 
StartSecOne secOne = new StartSecOne(); 
secOne 
        .execute("www.website.com/document.pdf"); 

} 

class StartSecOne extends AsyncTask<String, Integer, String> { 

@Override 
protected void onPreExecute() { 
    super.onPreExecute(); 
    mProgressDialog.show(); 
} 

@Override 
protected void onProgressUpdate(Integer... progress) { 
    super.onProgressUpdate(progress); 
    mProgressDialog.setProgress(progress[0]); 
} 

@Override 
protected String doInBackground(String... aurl) { 

    String isFileThere = Environment.getExternalStorageDirectory() 
            + "/Android/Data/" 
            + getApplicationContext().getPackageName() 
            + "/files/test1.pdf"; 
    File f = new File(isFileThere); 

    if (f.exists()) { 
        showPdf(); 
    } else { 

        try { 

            URL url = new URL(aurl[0]); 
            URLConnection connection = url.openConnection(); 

            connection.connect(); 
            int fileLength = connection.getContentLength(); 
            int tickSize = 2 * fileLength / 100; 
            int nextProgress = tickSize; 

            Log.d( 

            "ANDRO_ASYNC", "Lenght of file: " + fileLength); 

            InputStream input = new BufferedInputStream( 
                    url.openStream()); 

            String path = Environment.getExternalStorageDirectory() 
                    + "/Android/Data/" 
                    + getApplicationContext().getPackageName() 
                    + "/files"; 
            File file = new File(path); 
            file.mkdirs(); 
            File outputFile = new File(file, "test1.pdf"); 

            OutputStream output = new FileOutputStream(outputFile); 

            byte data[] = new byte[1024 * 1024]; 
            long total = 0; 
            int count; 
            while ((count = input.read(data)) != -1) { 
                total += count; 
                if (total >= nextProgress) { 
                    nextProgress = (int) ((total / tickSize + 1) * tickSize); 
                    this.publishProgress((int) (total * 100 / fileLength)); 
                } 
                output.write(data, 0, count); 
            } 

            output.flush(); 
            output.close(); 
            input.close(); 
            mProgressDialog.dismiss(); 
            showPdf(); 

        } catch (Exception e) { 
        } 
    } 
    return null; 
} 
}
secTwo = (Button) findViewById(R.id.secTwo); 
secTwo.setOnClickListener(new OnClickListener() { 
    public void onClick(View v) { 
        startSecTwo (); 
    } 

}); 
} 

private void startSecTwo () { 
StartSecTwo secTwo = new StartSecTwo (); 
secTwo 
        .execute("www.website.com/document2.pdf"); 

} 

class StartSecTwo extends AsyncTask<String, Integer, String> { 

@Override 
protected void onPreExecute() { 
    super.onPreExecute(); 
    mProgressDialog.show(); 
} 

@Override 
protected void onProgressUpdate(Integer... progress) { 
    super.onProgressUpdate(progress); 
    mProgressDialog.setProgress(progress[0]); 
} 

@Override 
protected String doInBackground(String... aurl) { 

    String isFileThere = Environment.getExternalStorageDirectory() 
            + "/Android/Data/" 
            + getApplicationContext().getPackageName() 
            + "/files/test2.pdf"; 
    File f = new File(isFileThere); 

    if (f.exists()) { 
        showPdf(); 
    } else { 

        try { 

            URL url = new URL(aurl[0]); 
            URLConnection connection = url.openConnection(); 

            connection.connect(); 
            int fileLength = connection.getContentLength(); 
            int tickSize = 2 * fileLength / 100; 
            int nextProgress = tickSize; 

            Log.d( 

            "ANDRO_ASYNC", "Lenght of file: " + fileLength); 

            InputStream input = new BufferedInputStream( 
                    url.openStream()); 

            String path = Environment.getExternalStorageDirectory() 
                    + "/Android/Data/" 
                    + getApplicationContext().getPackageName() 
                    + "/files"; 
            File file = new File(path); 
            file.mkdirs(); 
            File outputFile = new File(file, "test2.pdf"); 

            OutputStream output = new FileOutputStream(outputFile); 

            byte data[] = new byte[1024 * 1024]; 
            long total = 0; 
            int count; 
            while ((count = input.read(data)) != -1) { 
                total += count; 
                if (total >= nextProgress) { 
                    nextProgress = (int) ((total / tickSize + 1) * tickSize); 
                    this.publishProgress((int) (total * 100 / fileLength)); 
                } 
                output.write(data, 0, count); 
            } 

            output.flush(); 
            output.close(); 
            input.close(); 
            mProgressDialog.dismiss(); 
            showPdf(); 

        } catch (Exception e) { 
        } 
    } 
    return null; 
} 
}

secThree = (Button) findViewById(R.id.secThree); 
secThree.setOnClickListener(new OnClickListener() { 
    public void onClick(View v) { 
        startSecThree (); 
    } 

}); 
} 

private void startSecThree () { 
StartSecThree secThree = new StartSecThree (); 
secThree 
        .execute("www.website.com/document3.pdf"); 

} 

class StartSecThree extends AsyncTask<String, Integer, String> { 

@Override 
protected void onPreExecute() { 
    super.onPreExecute(); 
    mProgressDialog.show(); 
} 

@Override 
protected void onProgressUpdate(Integer... progress) { 
    super.onProgressUpdate(progress); 
    mProgressDialog.setProgress(progress[0]); 
} 

@Override 
protected String doInBackground(String... aurl) { 

    String isFileThere = Environment.getExternalStorageDirectory() 
            + "/Android/Data/" 
            + getApplicationContext().getPackageName() 
            + "/files/test3.pdf"; 
    File f = new File(isFileThere); 

    if (f.exists()) { 
        showPdf(); 
    } else { 

        try { 

            URL url = new URL(aurl[0]); 
            URLConnection connection = url.openConnection(); 

            connection.connect(); 
            int fileLength = connection.getContentLength(); 
            int tickSize = 2 * fileLength / 100; 
            int nextProgress = tickSize; 

            Log.d( 

            "ANDRO_ASYNC", "Lenght of file: " + fileLength); 

            InputStream input = new BufferedInputStream( 
                    url.openStream()); 

            String path = Environment.getExternalStorageDirectory() 
                    + "/Android/Data/" 
                    + getApplicationContext().getPackageName() 
                    + "/files"; 
            File file = new File(path); 
            file.mkdirs(); 
            File outputFile = new File(file, "test3.pdf"); 

            OutputStream output = new FileOutputStream(outputFile); 

            byte data[] = new byte[1024 * 1024]; 
            long total = 0; 
            int count; 
            while ((count = input.read(data)) != -1) { 
                total += count; 
                if (total >= nextProgress) { 
                    nextProgress = (int) ((total / tickSize + 1) * tickSize); 
                    this.publishProgress((int) (total * 100 / fileLength)); 
                } 
                output.write(data, 0, count); 
            } 

            output.flush(); 
            output.close(); 
            input.close(); 
            mProgressDialog.dismiss(); 
            showPdf(); 

        } catch (Exception e) { 
        } 
    } 
    return null; 
} 
}



private void showPdf() { 
    // TODO Auto-generated method stub 
    mProgressDialog.dismiss(); 
    File file = new File(Environment.getExternalStorageDirectory() 
            + "/Android/Data/" 
            + getApplicationContext().getPackageName() 
            + "/files/test1.pdf"); 
    PackageManager packageManager = getPackageManager(); 
    Intent testIntent = new Intent(Intent.ACTION_VIEW); 
    testIntent.setType("application/pdf"); 
    List list = packageManager.queryIntentActivities(testIntent, 
            PackageManager.MATCH_DEFAULT_ONLY); 
    Intent intent = new Intent(); 
    intent.setAction(Intent.ACTION_VIEW); 
    Uri uri = Uri.fromFile(file); 
    intent.setDataAndType(uri, "application/pdf"); 
    startActivity(intent); 
} 


} 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T02:17:27+00:00Added an answer on June 3, 2026 at 2:17 am
    final OnClickLisener listener = new OnClickListener() {
         public void onClick(View v){
             switch(v.getId()){ 
             case R.id.zero:
                break;
             case R.id.one:
                break;
             case R.id.two:
                break;
             }
         }
    }
    final int[] btnIds = new int[]{R.id.one, R.id.two, R.id.zero};
    for(int i = 0; i < btnIds.length; i++) {
        final Button btn = (Button)findViewById(btnIds[i]);
        btn.setOnClickListener(listener);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As the title says, I am trying to get the following Javascript working in
As the title says, I'm trying to get an automated release job working on
The title pretty much says it all, I am trying to get a java
hey the title pretty much says it all. i have been trying different methods
As the title says I'm trying to get a simple email working with Heroku
Title says what i'm trying to do. I can successfully generate an assembly if
Like the title says, I'm trying to determine if data is a standard attribute
As the title says, I'm trying to create a UIImage that will go inside
Basically what the title says. I created a new MVC application. I'm trying to
Title says all. Sample code: ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>(); HashMap<String, Object>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.