Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build a Self-Destructing Message Android App Adding Users Using Parse.com Adding a Progress Indicator

Luigi Zhou
Luigi Zhou
16,121 Points

Progress Bar doesn't appear in Android project with setProgressBarIndeterminateVisibility()

I'm following the Android track and i'm trying to build the self destructing message android app.

I'm currently using the latest version of Android Studio and I don't understand why the progress bar doesn't appear in the action bar.

To address the problem, I created another project where the progress bar is handled by two button (Start and Stop), so to understand if the problem was in my code, but it still doesn't work.

protected void onCreate(Bundle savedInstanceState) {
        supportRequestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    stopButton = (Button)findViewById(R.id.stopProgressButton);
    startButton = (Button)findViewById(R.id.progressButton);
    setSupportProgressBarIndeterminateVisibility(true);

    stopButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            setSupportProgressBarIndeterminateVisibility(false);
        }
    });

    startButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            setSupportProgressBarIndeterminateVisibility(true);
        }
    });
}

This is my onCreate method! Is something wrong with my code or the problem is something else?

17 Answers

Istead of Trying to add the Window Feature, I decided to a add a Progress Bar in the layout and the set it Visible or Invisible in the code.

For example to make it visible:

mSignUpProgressBar.setVisibility(View.VISIBLE);

mSignUpProgressBar is how I declared the progress Bar in my activity.

Pablo Rocha
Pablo Rocha
10,142 Points

This is the correct way to do this now. The built-in progress bar is deprecated.

Hi coders, I fixed that issue. At least, it works for me :) Try that and let me know about updates. you need to follow this simple steps:

1 Extending Activity not ActionBarActivity

public class LoginActivity extends Activity

2 Changing Activity theme to Holo theme in manifest file

 <activity
            android:name=".LoginActivity"
            android:label="@string/title_activity_login"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.Holo.Light.DarkActionBar">
</activity>

3 Requesting window feature in onCreate

protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        //...
        //...there will be your code
        //...
        setProgressBarIndeterminateVisibility(true);
        //...
        //...
}//ending onCreate method
Jacob Bergdahl
Jacob Bergdahl
29,118 Points

Nice work! However, this is not recommended. An up-to-date app should use AppCompat and your activity should implement ActionBarActivity. You're basically going into an older version, which is fine, however it's not recommended to do so.

this helped me, thanks

Stephen Little
Stephen Little
8,312 Points

Ok what I did and I'm sure it's likely written above as well...

First I put ButterKnife into the project (didn't think it could hurt) so I added the my build.gradle file this line of code under dependencies

compile 'com.jakewharton:butterknife:6.1.0'

I then hit the Sync Project with Gradle files button up top. once I had that done I went to the SignUpActivity.java file and added the butterknife code below where I set mSignUpButton and put this

@InjectView(R.id.progressBar) ProgressBar mProgressBar;

Please not that I did add a normal progressbar to my SignupActivity.xml. Ok once I did the above I just added this code right under the onCreate method under the

setContentView(R.layout.activity_sign_up);

I added

ButterKnife.inject(this);
mProgressBar2.setVisibility(View.INVISIBLE);

OK! after that I scrolled down to the comment //Create the new user and added this code

 if(mProgressBar2.getVisibility() == View.INVISIBLE){
                        mProgressBar2.setVisibility(View.VISIBLE);
                    }else {
                        mProgressBar2.setVisibility(View.INVISIBLE);
                    }

and there you have it that is what I did to make it work for me. Of course I had to do it again for the LoginActivity.java file and counter XML file activity_login.xml. I added a progress bar the the xml file and gave it a different id (mine was loginProgressBar) then just cut and paste the code above with the new progressbar name.... I hope this helps somebody, I am new and just trying to help out some... hope I didn't waste anybodys time with this...

Cheers! Stephen

same problem here, and no one have any solution???

It worked for me when I did it in this way:

/* LoginActivity.java */ ```public class LoginActivity extends Activity { ... protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); setContentView(R.layout.activity_login); ... }else{ // login user setProgressBarIndeterminateVisibility(true); ... public void done(ParseUser parseUser, ParseException e) { setProgressBarIndeterminateVisibility(false);

/* AndroidManifest.xml */
```<activity
            android:name=".LoginActivity"
            android:label="@string/title_activity_login"
            android:parentActivityName=".MainActivity"
            android:screenOrientation="portrait"
           android:theme="@android:style/Theme.Holo.Light.DarkActionBar" >```


/* build.gradle (app) */
```minSdkVersion 14```

Using Android Studio 1.1.0.

Hope this helps :D

the request should be after the super.oncreate.....like this

\\ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); setContentView(R.layout.activity_main); \\

Luigi Zhou
Luigi Zhou
16,121 Points

By putting the request after the super.oncreate, the app crashes as soon as it start... I fixed it by putting it before the super.oncreate but like this the progressBar does not appear

what does your logcat say?....post it here

Luigi Zhou
Luigi Zhou
16,121 Points

12-29 21:18:35.506 1505-1505/com.luigi_zhou.ribbit E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.luigi_zhou.ribbit, PID: 1505 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.luigi_zhou.ribbit/com.luigi_zhou.ribbit.LoginActivity}: android.util.AndroidRuntimeException: requestFeature() must be called before adding content at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) at android.app.ActivityThread.access$800(ActivityThread.java:144) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5221) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) Caused by: android.util.AndroidRuntimeException: requestFeature() must be called before adding content at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:301) at android.app.Activity.requestWindowFeature(Activity.java:3596) at com.luigi_zhou.ribbit.LoginActivity.onCreate(LoginActivity.java:32) at android.app.Activity.performCreate(Activity.java:5933) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)             at android.app.ActivityThread.access$800(ActivityThread.java:144)             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)             at android.os.Handler.dispatchMessage(Handler.java:102)             at android.os.Looper.loop(Looper.java:135)             at android.app.ActivityThread.main(ActivityThread.java:5221)             at java.lang.reflect.Method.invoke(Native Method)             at java.lang.reflect.Method.invoke(Method.java:372)             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

This is what the logcat says when the requestFeature is placed below the super call... By placing it before, the exception disappear...

whats on line 32?

Luigi Zhou
Luigi Zhou
16,121 Points

that would be the requestWindowFeature

same problem here

I'm getting this same problem as well

Robert Proulx
Robert Proulx
6,538 Points

Had this same problem. From googling around it looks like the action bar is deprecated in API 21 and they are switching everything over to a "toolbar" widget. I got it to not crash buy using this recommendation from the IDE:

supportRequestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

But I can't get the progress bar to display by using:

setSupportProgressBarIndeterminateVisibility(true|false);

Since it's being deprecated, probably better off just developing without it.

Andrea Kotys
Andrea Kotys
7,396 Points

using a progress bar in the layout and changing it's visibility as previously mentioned works great!

Andre Colares
Andre Colares
5,437 Points

Like   StephenLittle said,... Here are the code for the correct spinners:

LOGIN ACTIVITY CLASS:::::

package com.andrecolares.ribbit;

import android.app.AlertDialog; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ProgressBar; import android.widget.TextView;

import com.parse.LogInCallback; import com.parse.ParseException; import com.parse.ParseUser;

import butterknife.Bind; import butterknife.ButterKnife;

public class LoginActivity extends AppCompatActivity {

protected EditText mUsername;
protected EditText mPassword;
protected Button mLoginButton;
protected TextView mSignUpTextView;
@Bind(R.id.progressBar2)ProgressBar mProgressBar2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    ButterKnife.bind(this);
    mProgressBar2.setVisibility(View.INVISIBLE);

    //O indicador do progresso tem que ser chamado antes do SetContentView

    mSignUpTextView = (TextView)findViewById(R.id.signUpText);
    mSignUpTextView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(LoginActivity.this, SignUpActivity.class);
            startActivity(intent);
        }
    });

    mUsername = (EditText)findViewById(R.id.usernameField);
    mPassword = (EditText)findViewById(R.id.passwordField);

    mLoginButton = (Button)findViewById(R.id.loginButton);
    mLoginButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String username = mUsername.getText().toString();
            String password = mPassword.getText().toString();


            username = username.trim();
            password = password.trim();

            if(username.isEmpty() || password.isEmpty() ){
                AlertDialog.Builder builder= new AlertDialog.Builder(LoginActivity.this);
                builder.setMessage(R.string.login_error_message)
                        .setTitle(R.string.login_error_title)
                        .setPositiveButton(android.R.string.ok, null);
                AlertDialog dialog = builder.create();
                dialog.show();
            }else{
                //Login
                if(mProgressBar2.getVisibility() == View.INVISIBLE){
                    mProgressBar2.setVisibility(View.VISIBLE);
                }else {
                    mProgressBar2.setVisibility(View.INVISIBLE);
                }
                ParseUser.logInInBackground(username, password, new LogInCallback() {
                    @Override
                    public void done(ParseUser parseUser, ParseException e) {
                        //Como o login foi concluído o indicador de progresso some.
                        mProgressBar2.setVisibility(View.INVISIBLE);
                        if (e==null){
                            //Success!
                            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                            startActivity(intent);
                        }else {
                            AlertDialog.Builder builder= new AlertDialog.Builder(LoginActivity.this);
                            builder.setMessage(e.getMessage())
                                    .setTitle(R.string.login_error_title)
                                    .setPositiveButton(android.R.string.ok, null);
                            AlertDialog dialog = builder.create();
                            dialog.show();
                        }
                    }
                });
                }


        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_login, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

SIGN UP ACTIVITY CLASS:::::

package com.andrecolares.ribbit;

import android.app.AlertDialog; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ProgressBar;

import com.parse.ParseException; import com.parse.ParseUser; import com.parse.SignUpCallback;

import butterknife.Bind; import butterknife.ButterKnife;

public class SignUpActivity extends AppCompatActivity {

protected EditText mUsername;
protected EditText mPassword;
protected EditText mEmail;
protected Button mSignUpButton;
@Bind(R.id.progressBar) ProgressBar mProgressBar;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sign_up);
    ButterKnife.bind(this);
    mProgressBar.setVisibility(View.INVISIBLE);

    mUsername = (EditText)findViewById(R.id.usernameField);
    mPassword = (EditText)findViewById(R.id.passwordField);
    mEmail = (EditText)findViewById(R.id.emailField);

    mSignUpButton = (Button)findViewById(R.id.signupButton);
    mSignUpButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String username = mUsername.getText().toString();
            String password = mPassword.getText().toString();
            String email = mEmail.getText().toString();

            username = username.trim();
            password = password.trim();
            email = email.trim();

            if(username.isEmpty() || password.isEmpty() || email.isEmpty()){
                AlertDialog.Builder builder= new AlertDialog.Builder(SignUpActivity.this);
                builder.setMessage(R.string.signup_error_message)
                        .setTitle(R.string.signup_error_title)
                        .setPositiveButton(android.R.string.ok, null);
                AlertDialog dialog = builder.create();
                dialog.show();
            }else{
                //create the new user!
                if(mProgressBar.getVisibility() == View.INVISIBLE){
                    mProgressBar.setVisibility(View.VISIBLE);
                }else {
                    mProgressBar.setVisibility(View.INVISIBLE);
                }
                ParseUser newUser = new ParseUser();
                newUser.setUsername(username);
                newUser.setPassword(password);
                newUser.setEmail(email);
                newUser.signUpInBackground(new SignUpCallback() {
                    @Override
                    public void done(ParseException e) {
                        mProgressBar.setVisibility(View.INVISIBLE);

                        if(e==null){
                            //Success!

                            Intent intent = new Intent(SignUpActivity.this, MainActivity.class);
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                            startActivity(intent);
                        }else {
                            AlertDialog.Builder builder= new AlertDialog.Builder(SignUpActivity.this);
                            builder.setMessage(e.getMessage())
                                    .setTitle(R.string.signup_error_title)
                                    .setPositiveButton(android.R.string.ok, null);
                            AlertDialog dialog = builder.create();
                            dialog.show();
                        }
                    }
                });

            }
        }
    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_sign_up, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

FOR THIS WORK YOU MUST ADD THE BUTTERKNIFE TO GRADLE DEPENDENCIES LIKE THIS:::::

apply plugin: 'com.android.application'

android { compileSdkVersion 23 buildToolsVersion "23.0.0"

defaultConfig {
    applicationId "com.andrecolares.ribbit"
    minSdkVersion 14
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

}

dependencies { compile fileTree(dir: 'libs', include: ['.jar']) compile 'com.android.support:appcompat-v7:23.0.0' compile 'com.google.android.gms:play-services:7.8.0' compile 'com.parse.bolts:bolts-android:1.+' compile fileTree(dir: 'libs', include: 'Parse-.jar') compile 'com.jakewharton:butterknife:7.0.1' //THIS HERE IS THE BUTTERKNIFE

}

AND LAST, REMEMBER TO GO TO XML LAYOUT FOR BOTH CLASSES AND ADD THE PROGRESS BAR (MEDIUM), the xml will be:

<ProgressBar android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/progressBar2" android:layout_centerVertical="true" android:layout_centerHorizontal="true"/>

THATS ALL!