0

I'll try another shot. Hopefully now i get the answere i totally Need.

Just imagine: I have a widget which calls(after onClick) a blank activity with no hardcoded code, just a Relative Layout with some views.(Layout provided by XML-layout-file).

My Activity:

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myLayout);

When i add a for-loop to the onCreate-Method which adds 50 Buttons(take no care about layoutparams, orientation and so on). Just 50 simply Buttons

Like:

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myLayout);
for(int i = 0; i<50; i++) {
Button btn = new Button(this);
myLayout.add(btn);
}

I recognize that he only Shows the activity on the Screen until he finished to add all the Buttons.

My Question is: How can i prevent this!? How can i Show up the Activity with the Content from the XML-layout file and then(ONLY then) add one Button after another to the Layout.

Is this possible? If so, do i neew to redraw the whole activity and so on. Please give me advise to my issue.

Mike
  • 857
  • 1
  • 7
  • 11
  • Put your onCreate for loop button code in onResume once your main layout has already been inflated... – zgc7009 Nov 11 '14 at 14:11
  • Take a look at this http://stackoverflow.com/questions/19013960/android-addview-in-background-thread – Egor Neliuba Nov 11 '14 at 14:12
  • @zgc7009 this doesn't did the trick.... – Mike Nov 11 '14 at 14:17
  • I mean code processes are pretty fast. If you want to be able to visibly distinguish the difference between everything happening you are going to need a thread with a handler that has a running loop that sleeps the thread in between adding buttons. – zgc7009 Nov 11 '14 at 14:18
  • @EgorN i will have a closer look, can you provide me a small Explanation as an answere? – Mike Nov 11 '14 at 14:21
  • @zgc7009 i don't think so either zgc7009. If i start the code above, it's really fast. Not able to see if the Content is viewed before the Buttons are added. But if i add an Image to every button(imagine the Buttons now as an ImageButton) then i can totally mention a longer Startup time and the Content is also only viewed till all the ImageButtons and Images are added..(with my method in the onResume().. so this can't be true.. i also tried to run the procedure in an UIthread... always the same, could it be that the added Theme of the Layout is Theme.Dialog? – Mike Nov 11 '14 at 14:24
  • ... what? Sorry that doesn't really make sense – zgc7009 Nov 11 '14 at 14:26
  • @zgc7009 i've updated my comment. Can you please have a closer look. Could it be that my Activity is a Dialog. And that Dialogs are somehow forced to open as soon as every code is processed? I can just tell you what i see, i have my method in the onResum, i expand the for-Loop to 100 and add an ImageView to every ImageButton. And the Layout doesn't popup till all of the code is processed. But i mention a much longer Startup-time. So for me that means, the the Activity gets all the ImageButtons and Images and then he starts up. Otherwise the activity must be viewed in the same time without loop – Mike Nov 11 '14 at 14:31
  • If you are using an Activity with a Dialog theme it should still follow the Activity lifecycle. The thing is, it is bulk loading all of that data at once which is causing the laggy look. If you want to add them individually you will likely need to do a separate runnable and do them in chunks (or individually). That is what the link @EgorN posted is doing. Breaking the visuals into chunks. – zgc7009 Nov 11 '14 at 14:41

0 Answers0