Thursday, September 1, 2011

Two layouts to be loaded for single activity

Two layouts to be loaded for single activity with custom dialog

How to display a custom dialog in your Android application
Yesterday Jozsi showed you, how to make an alert dialog, today I'm going to show you, how to make a custom dialog/popup window.
Sometimes, it's better to make your own dialog, because this way, you can display whatewer you want., the way you want it.
First, make your own layout, with the needed elements. Here, I'm going to use two buttons, a textview inside a scrollview, and an imageview...

 main.class

  public class main extends Activity {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            //set up main content view
            setContentView(R.layout.main);
            //this button will show the dialog
            Button button1main = (Button) findViewById(R.id.Button01main);
     
            button1main.setOnClickListener(new OnClickListener() {
            @Override
                public void onClick(View v) {
                    //set up dialog
                    Dialog dialog = new Dialog(main.this);
                    dialog.setContentView(R.layout.maindialog);
                    dialog.setTitle("This is my custom dialog box");
                    dialog.setCancelable(true);
                    //there are a lot of settings, for dialog, check them all out!
     
                    //set up text
                    TextView text = (TextView) dialog.findViewById(R.id.TextView01);
                    text.setText(R.string.lots_of_text);
     
                    //set up image view
                    ImageView img = (ImageView) dialog.findViewById(R.id.ImageView01);
                    img.setImageResource(R.drawable.nista_logo);
     
                    //set up button
                    Button button = (Button) dialog.findViewById(R.id.Button01);
                    button.setOnClickListener(new OnClickListener() {
                    @Override
                        public void onClick(View v) {
                            finish();
                        }
                    });
                    //now that the dialog is set up, it's time to show it    
                    dialog.show();
                }
            });
        }
     }

No comments:

Post a Comment

Check out this may be help you

Related Posts Plugin for WordPress, Blogger...