ProgressDialog - How to use it correctly?

Put your problem here if it does not fit any of the other categories.

ProgressDialog - How to use it correctly?

Postby Viperb0y » Mon Jul 05, 2010 11:30 pm

Hi,

currently I'm coding a small project where I send some message to a webserver and it returns me an answer. While the data is on its way to the internet, I want to display a ProgressDialog. Currently It doesn't work. But why? Here is an example of my Code (everything works fine, except of the ProgressDialog):

The code when clicked on the Submit button:

Code: Select all
            // Get the text of the input field
               EditText editText1 = (EditText) v.findViewById(R.id.text1);
               String String1 = editText1.getText().toString();

               // Hides the virtual keyboard
               InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
               imm.hideSoftInputFromWindow(editText1.getWindowToken(), 0);
               
               // Starts the "Please wait" window DOES NOT WORK
               ProgressDialog dialog = ProgressDialog.show(Main.this, "", "Loading. Please wait...", true);

               // Call the function for grabbing the result
               String result= getString(String1);
               
               // Sets the text of editText1 to result
               editText1.setText(result);
               
               // Hides the "Please wait" window
               dialog.hide();
               
               // Copy the result to the clipboard
               ClipboardManager ClipMan = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
               ClipMan.setText(result);
               
               // Create a "toast" which shows that the result is copyed to clipboard
               Context context = getApplicationContext();
               CharSequence text = "Copyed Result to the clipboard";
               int duration = Toast.LENGTH_SHORT;
               Toast.makeText(context, text, duration).show();


here my "getString" function:

Code: Select all
public String getString(String String1) {

         URL url = new URL("http://mywebserver.de/postMeSomething.php");
         URLConnection connection = url.openConnection();
         connection.setDoOutput(true);
         
         OutputStreamWriter out = new OutputStreamWriter(connection
               .getOutputStream());
         out.write("url=" + String1);
         out.close();

         BufferedReader in = new BufferedReader(new InputStreamReader(
               connection.getInputStream()));

         String decodedString;
         String output = "";

         while ((decodedString = in.readLine()) != null) {
            output += decodedString;
         }
         in.close();
         
         // Fetches the shortUrl from the JSON object
         JSONObject json = new JSONObject(output);
         JSONObject jsonResult = json.getJSONObject("result");
         String String2 = jsonResult.getString("Answer");
         
         return String2;}


Maybe someone could help me with this :(.
Viperb0y
Junior Developer
Junior Developer
 
Posts: 12
Joined: Mon Mar 29, 2010 6:48 pm

Top

Re: ProgressDialog - How to use it correctly?

Postby anarche » Tue Jul 06, 2010 2:57 am

tryyyy: (drum roll)

dialog.show();
anarche
Master Developer
Master Developer
 
Posts: 369
Joined: Sun Apr 11, 2010 5:21 am

Re: ProgressDialog - How to use it correctly?

Postby Viperb0y » Tue Jul 06, 2010 8:58 pm

Hi,

anarche wrote:[...]
dialog.show();


I used this after "ProgressDialog dialog = ProgressDialog.show(Main.this, "", "Loading. Please wait...", true);" and it still doesn't work.

btw. isn't ProgressDialog dialog = ProgressDialog.show(Main.this, "", "Loading. Please wait...", true); not the same?
Viperb0y
Junior Developer
Junior Developer
 
Posts: 12
Joined: Mon Mar 29, 2010 6:48 pm

Re: ProgressDialog - How to use it correctly?

Postby nicholas.hauschild » Wed Jul 07, 2010 1:30 am

It would be helpful if you could explain how it does not work. Does it blow up, does it not do anything, does it do something that you don't expect?

Provide as much information as you can, and we will help out as much as we can.

Good Luck!
-Nick
nicholas.hauschild
Master Developer
Master Developer
 
Posts: 310
Joined: Fri Dec 04, 2009 4:50 am

Re: ProgressDialog - How to use it correctly?

Postby edenbulur23 » Wed Jul 07, 2010 8:55 am

You should use thread in this case:

//initialization of your progressDialog
ProgressDialog pd = ProgressDialog.show(this,"", "Loading...", true);

//to display your progress dialog when process is doing
new Thread(new Runnable(){
public void run(){

//your process

}
}).start();

//to stop your progressDialog
pd.dismiss();
edenbulur23
Freshman
Freshman
 
Posts: 3
Joined: Fri Jun 25, 2010 7:57 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Exabot [Bot] and 13 guests