Load Dialog and add Views at Runtime

240 Views Asked by At

I have an activity with the Theme = Theme.Dialog!

In the onCreate() Method i programmatically create a view and add this to the Layout which consists of a RelativeLayout with the id: myLayout! Something like:

onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myCustomizedLayout);
RelativeLayout myLayout = (RelativeLayout) findViewById(R.id.myLayout);

Button mybutton = new Button(this);
//set RelativeLayout.LayoutParams here
myLayout.add(mybutton, my RelativeLayout.LayoutParams);
}

I mentioned that when i'm adding not one, but 50 Buttons with a Loop, he's not showing the Dialog on the Screen until all of the 50 Buttons are created and added to myLayout...

I don't understand this because i have setted the contentView before starting the Loop!

I tried to put the Loop into a Thread..something like:

runOnUiThread(new Runnable(){
    for(i=0; i<50; i++){
        Button myButton = new Button(this);
        //add LayoutParameters her
        myLayout.add(myButton, LayoutParameters);
    }
});

But even when i run it on a Thread he only Shows up the Dialog until everything is finished...

My Question is: How can i view the Dialog on the Screen as quick as possible and add the views dynamically into to Dialog at runtime.

I hope you guys understand what i mean.

Update:

This is the style for the Activity..

<style name="dialog_style" parent="@android:style/Theme.Dialog">
    <item name="android:windowNoTitle">true</item>
</style>

and in the Manifest file i add this style to the activity:

android:theme="@style/dialog_style"
1

There are 1 best solutions below

5
On

Create a method createDialog() and call this in your mainactivity.java oncreate method

private void createDialog(){

AlertDialog.Builder dialog = new AlertDialog.Builder(
                MainActivity.this);
        dialog.setTitle("APP NAME");
        dialog.setMessage("Do you want to quit?");
        dialog.setIcon(R.drawable.ic_launcher);
        dialog.setPositiveButton("NO",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {

                        dialog.cancel();
                    }
                });

        dialog.setNegativeButton("YES",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                        dialog.cancel();
                    }
                });
        dialog.show();

}