0

In my application, I'm creating dynamic tabs.Inside framelayout I've created a edittext.For each tab i create content of tab will be edittext. When i type in editext of one tab and later if i create a new tab and when I switch between these tabs, it shows the content of editext as empty.

Update:

I don't want to create a separate activity for each tab.I'l explain along with the code.

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final TabHost tabs = (TabHost)this.findViewById(R.id.tabs);
      tabs.setup();
        TabHost.TabSpec spec1=tabs.newTabSpec(f);
   spec1.setContent(R.id.content1);
   spec1.setIndicator(f);
tabs.addTab(spec1);
tabs.setCurrentTab(i);
list.add(spec1);  
i++;
    Addbtn.setOnClickListener(new OnClickListener()
{
    @Override
   public void onClick(View arg0) 
  {
          final TabHost tabs = (TabHost)this.findViewById(R.id.tabs);
      tabs.setup();
          TabHost.TabSpec spec1=tabs.newTabSpec(f);
     spec1.setContent(R.id.content1);
     spec1.setIndicator(f);
    tabs.addTab(spec1);
    tabs.setCurrentTab(i);
    list.add(spec1);  
    i++;
    });

Default one tab will be there. here content of tab is edittext. When i click on button new tab will be created. problem is with the content of tab. In default tab if i type in edittext, and create a new tab and switch to the default tab the content of default would be lost.

Community
  • 1
  • 1
user428231
  • 193
  • 1
  • 1
  • 7

2 Answers2

0

You need to store the value of the EditText in some manner. A good way is to show tab content through an activity and use this method to store instance state.

Community
  • 1
  • 1
Sameer Segal
  • 21,813
  • 7
  • 42
  • 56
0

You can use an EditorActionListener, which is called when someone presses the 'Enter' or IME_ACTION key to store the values of your EditText.

Instead of doing spec1.setContent(R.id.content1);, create an EditText dynamically (new EditText(context)) and put a number (Tab Number) as a Tag, which you can retrieve and store in a List.

mEditText.setOnEditorActionListener(new OnEditorActionListener() {

                        @Override
                        public boolean onEditorAction(TextView v, int actionId,
                                KeyEvent event) {

                                       int position = (int) v.getTag();

                                       String s = ((EditText) v).getText()
                                       // store in an object

                      return false;
       }
});

Not sure how you will retrieve this without launching subActivities!

Sameer Segal
  • 21,813
  • 7
  • 42
  • 56