Como fazer uma tela similar a essa no android? Fazendo a barra encher
2 Respostas
Tente da seguinte forma:
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
}
}
}
Utilize ProgressBar, com o indeterminateOnly true, e com o estilo Widget.ProgressBar.Horizontal, dessa forma:
<ProgressBar
android:id="@+id/progress"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tvProcessing"
android:indeterminateOnly="true"/>
Style:
style="@android:style/Widget.ProgressBar.Horizontal"
E ai se você quiser crie um drawable e coloque como background, para personalizar o layout