vtni vtni - 1 year ago 59
Android Question

android: set visibility from AsyncTask

after this thread, i tried to make a variable status bar with this code:

private int[] loadingElementIDs;
private void initLoadingBar() {
final DisplayMetrics displayMetrics=getResources().getDisplayMetrics();
final float screenWidthInDp = displayMetrics.widthPixels/displayMetrics.density;
final int elementAmount = (int) (Math.floor(screenWidthInDp * 0.5f / 30) * 5);

//set margins
LinearLayout container = (LinearLayout)findViewById(R.id.loading_outer);
...
container.requestLayout();

//declare length
loadingElementIDs = new int[elementAmount];

LayoutParams LLParams = new LayoutParams(0, LayoutParams.MATCH_PARENT);
LLParams.weight = 1f;
LinearLayout element;
for (int i=0; i<elementAmount; i++) {
int id = generateViewId(); //creates unique id

element = new LinearLayout(this);
element.setVisibility(View.INVISIBLE);
element.setLayoutParams(LLParams);
element.setBackgroundDrawable(getResources().getDrawable(R.drawable.loading_inner));
element.setId(id);
element.requestLayout();
container.addView(element);

loadingElementIDs[i] = id;
}

}


this is working fine for me, but now i want to calculate sth with an asynctask and make the elements visible (code within my activity class):

private class PrefetchData extends AsyncTask<Void, Void, Void> {

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

@SuppressWarnings("static-access")
@Override
protected Void doInBackground(Void... arg0) {

try {
int step = 0;
float totalSteps = 100f;
while (...) {
step++;

// ...................

//show status
setLoadingStatus( step / totalSteps);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}


@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
Intent i = new Intent(SplashScreen.this, MainActivity.class);
startActivity(i);
finish();
}

}

public void setLoadingStatus(float percentage) {
int max = (int) Math.min( Math.floor(percentage * loadingElementIDs.length),
for (int d=0; d<max; d++ ) {
((LinearLayout)findViewById(loadingElementIDs[d])).setVisibility(View.VISIBLE);
LinearLayout el = (LinearLayout)this.findViewById(loadingElementIDs[d]);
el.setVisibility(LinearLayout.VISIBLE);
}
}


And this does not work. if i call
setLoadingStatus(20f);
from onCreate it works perfectly, but not in the AsyncTask. Of course i do start
initLoadingBar();
and
new PrefetchData().execute();
in activities onCreate().

do you know what i'm doing wrong?

Answer Source

thank you guys, i solved it with onProgressUpdate. instead of setLoadingStatus i call this:

private class PrefetchData extends AsyncTask<Void, Float, Void> {
    ....
    protected void onProgressUpdate(Float... values) {
        for (int d=0; d<Math.min( Math.floor(values[0] * loadingElementIDs.length), loadingElementIDs.length); d++ ) {
            LinearLayout el = (LinearLayout)findViewById(loadingElementIDs[d]);
            el.setVisibility(LinearLayout.VISIBLE);
        }
    }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download