0

I've created some buttons dynamically, when i start activity all is well, but when i rotate a screen or press back Button, i lose all buttons, i've googled then i've found that the views without IDs can't be saved, so i want to set IDs for them dynamically and i want to find them inside R.java file with there name like this :

     public static final class id {

            public static final int  Button1=0x7f070027;
            public static final int  Button2=0x7f070024;

                                  }

I've tried setId() but the problem is not solved, and i think it's not the same thing when use setID(); and set id manually inside String file, so i need your help thanks in advance(^_^).

here's my source code :

TableLayout TL = (TableLayout) findViewById(R.id.tlRDV);
                TL.removeAllViews();
                j = 0;
                for (int i = 0; i < nbH; i++) {

                    TableRow tr = new TableRow(this);
                    tr.setLayoutParams(new LayoutParams(
                            ViewGroup.LayoutParams.WRAP_CONTENT,
                            ViewGroup.LayoutParams.WRAP_CONTENT));
                    tr.setId(View.generateViewId());
                    TextView text_view = new TextView(this);
                    Button b = new Button(this);

                    text_view.setWidth(1000);
                    j = j + averageConsultationTime;
                    HMA = ((BeganConsultationH * 60) + (BeganConsultationM))
                            + j - averageConsultationTime;

                    int h = HMA / 60;
                    int m = HMA % 60;
                    b.setBackgroundColor(0xff99cc00);
                    b.setText(h + "H" + m);
                    tr.addView(b);

                    for (int k = 0; k < 7; k++) {
                        Button but = new Button(this);
                        but.getId(View.generateViewId());
                        but.setText("" + but.getId());
                        but.setOnClickListener(getOnClickDoSomething(but,
                                jours[k] + "_" + h + "H" + m));

                        tr.addView(but);

                    }
                    TL.addView(tr);

                }

            }

        }

    }  
Abderazak Amiar
  • 776
  • 7
  • 22
  • Why are you not using `id` resources? – Shaishav Aug 28 '16 at 11:46
  • how do i do it dynamically ? is there a way to do it ? – Abderazak Amiar Aug 28 '16 at 11:48
  • 1
    For rotation you can look into `onSaveInstanceState`, for back press cases, you need some other persistent storage – Shaishav Aug 28 '16 at 11:51
  • The *state* of views is retained across screen orientation changes. The view hierarchy itself is *not* retained. That's why you always have to call `setContentView` and always have to manually add any dynamically created views. – Eugen Pechanec Aug 28 '16 at 12:38
  • Hi, Shaishav, my problem is when i rotate my screen i lose all button, then how could i passe this button or save it using onSaveInstanceState, or Intent ?????????????? – Abderazak Amiar Aug 28 '16 at 16:48

0 Answers0