0

Currently i managed to create the buttons dynamically on a view

Here is the my code to create the buttons;

    public void Add_on(View v) {
        AlertDialog.Builder mbuilder = new AlertDialog.Builder(Mb.this);
        View mview = getLayoutInflater().inflate(R.layout.activity_mb1, null);
        EditText number = (EditText) mview.findViewById(R.id.etnum);
        Button Create = (Button) mview.findViewById(R.id.etcreate);
        Button Cancel = (Button) mview.findViewById(R.id.etcancel);

        Create.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){

                if (!number.getText().toString().isEmpty())
                {
                    Toast.makeText(Mb.this, "Number can be NULL",Toast.LENGTH_SHORT).show();


                    LinearLayout yenilayout = new LinearLayout(Mb.this);
                    int n =1;
                    for(int i=0; i<n; i++)
                    {
                        Button yeniButton = new Button(Mb.this);
                        yenilayout.addView(yeniButton);

                        yeniButton.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Toast.makeText(Mb.this, "Button is working",Toast.LENGTH_SHORT).show();
                            }
                        });

                    }
                    altlayout.addView(yenilayout);


                } else {
                    Toast.makeText(Mb.this, "Number cannot be NULL",Toast.LENGTH_SHORT).show();

                }

            }
        });

But whenever i recall the activity, the buttons are no longer exist. So May i know can i place the button there permanently?

Thank you for suggestions

  • Each time the activity is recreated, you will have to click on that `Create` button, to get those dynamic buttons. PS: its a standard to use lowercase first letter for variables and uppercase first letter for Classes – zeekhuge Jul 24 '17 at 10:57

1 Answers1

0

You can use Bundle to save an activity's state and recreate it in the onCreate() method. This works for a particular instance of Activity, so can be used to save data concerning selection, or user input etc., but not data that you need to be persistent across application launches.

To use the Bundle, override the onSaveInstanceState(Bundle) and onRestoreInstanceState(Bundle) methods in the Activity class. You can use methods from Bundle to save whatever data you like in a map, and get it back in onRestoreInstanceState(Bundle), which is called in onStart().

The default implementations already handle most UI stuff though, and I would have thought this would keep track of your buttons for you, so it may be that your question is actually about associating some persistent data with your application. (this also means that if you do override the above methods, you should make sure to call the super methods in the first line of your implementation).

If you need persistent data across application launches, then the quickest and easiest way would be to use SharedPreferences, see this answer for an example.