2
Try it this way:
activity_main.xml
<TextView
android:text="Loading ... Please Wait"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/progressBar"
android:layout_centerHorizontal="true"
android:layout_marginBottom="36dp"
android:id="@+id/textView" />
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/progressBar"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginTop="170dp" />
Mainactivity.java
public class MainActivity extends AppCompatActivity {
ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = ProgressBar.class.cast(findViewById(R.id.progressBar));
}
@Override
protected void onStart() {
super.onStart();
new Loader().execute((Void)null);
}
class Loader extends AsyncTask<Void, Void, Void> {
private Integer update = 1;
@Override
protected Void doInBackground(Void... params) {
// iteramos ate 100
while (update <= 100){
// Criamos um Runnable q ira mandar a atualizacao desta Thread par a tela!
final Runnable runnable = new Runnable() {
@Override
public void run() {
// seta o valor atualizado na tela
progressBar.setProgress(update);
}
};
// executa o Runnable na ThreadUI
runOnUiThread(runnable);
update++;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
// AQUI SERA CHAMDO AO TERMINAR DE CARREGAR
}
}
}