My app shell read data from a file on the device. I create the tables but they are not instantly shown (i create them in the onCreate() method). Maybe you can give me a hint were i can start scraping my data from the web, because when i do it in my main activity it will jump over the first step to show my data from the database.
I am trying to get notified by my main activity when its completely drawn (when the user can see the UI).
I realized that this way:
view.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener) [...]
is not working, because when this method is called i am not able to see my view on the device. So i logged some variables in this method to get more information about this view:
log("isActivated: " + linearLayout.isActivated()); --> returned false
log("isEnabled: " + linearLayout.isEnabled()); --> returned true
log("isInTouchMode: " + linearLayout.isInTouchMode()); --> returned true
log("isShown: " + linearLayout.isShown()); --> returned true
I mentioned that the only variable which is not true, was view.isActivated(). Can you tell me whats the best way to wait for your application till everything its drawn (hopefully not above API 11).
Edit: some code if you wanna see
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<Group> groups = null;
String path = getFilesDir() + separator + STORAGE_FILE;
File storageFile = new File(path);
try {
InternalGroupStorage storage = new InternalGroupStorage();
if (storageFile.exists()) {
log("Load groups from storage: " + path);
groups = storage.loadGroupStorage(path);
createGroupTables(groups);
log("Tables created");
}
}
catch (JSONException e) {
scrapeGroupsFromWeb();
log("JSONException occured: groups going to be scraped from weg now.");
e.printStackTrace();
}
}
private void scrapeGroupsFromWeb() {
try {
GroupScraperTask task = new GroupScraperTask();
List<Group> groups = task.execute("A", "B", "C", "D", "E", "F", "G", "H").get();
if (groups == null) {
return;
}
log("Groups were scraped.");
InternalGroupStorage groupStorage = new InternalGroupStorage();
String path = getFilesDir() + separator + STORAGE_FILE;
FileOutputStream fos = new FileOutputStream(path);
groupStorage.saveGroupStorage(fos, groups);
createGroupTables(groups);
log("Tables created.");
}
catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
private void createGroupTables(List<Group> groups) {
GroupTable groupTable = new GroupTable();
groupTable.createGroupTable(this, groups, 0, (TableLayout) findViewById(R.id.tableA));
groupTable.createGroupTable(this, groups, 1, (TableLayout) findViewById(R.id.tableB));
groupTable.createGroupTable(this, groups, 2, (TableLayout) findViewById(R.id.tableC));
groupTable.createGroupTable(this, groups, 3, (TableLayout) findViewById(R.id.tableD));
groupTable.createGroupTable(this, groups, 4, (TableLayout) findViewById(R.id.tableE));
groupTable.createGroupTable(this, groups, 5, (TableLayout) findViewById(R.id.tableF));
groupTable.createGroupTable(this, groups, 6, (TableLayout) findViewById(R.id.tableG));
groupTable.createGroupTable(this, groups, 7, (TableLayout) findViewById(R.id.tableH));
}
@Override
protected void onResume() {
super.onResume();
log("resume");
scrapeGroupsFromWeb();
}
}
onResume(); is called before my tables are drawn!
edit2 added this in my onCreate():
final LinearLayout layout = (LinearLayout) findViewById(R.id.LinearLayout1);
ViewTreeObserver vto = layout.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
layout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
int width = layout.getMeasuredWidth();
int height = layout.getMeasuredHeight();
log("draw?");
scrapeGroupsFromWeb();
}
});
Its only drawn after my scrapeGroupsFromWeb()
in my onGlobalLayout()