Mohsen.Sharify
Mohsen.Sharify

Reputation: 257

access content of TabHost TabWidget

i have a TabHost and my tab widgets content are activity(contain EditTexts etc) and a save button on top of tab host.

locations:

-TabHost in one activity(tab activity)

-Save button is child of TabHost activity

-each TabWidget in an activity

i want when user click save button,grab all edit text,spinner etc value in tabwidgets and save them in database

it's about 3 day i am searching and testing!

Edit

code:

        #region tabs

        TabHost.TabSpec spec;     // Resusable TabSpec for each tab
        Intent intent;            // Reusable Intent for each tab

        //--------------------------------
        intent = new Intent(this, typeof(FrmMelkPersonalInfoTabActivity));
        intent.AddFlags(ActivityFlags.NewTask);
        spec = TabHost.NewTabSpec("FrmMelkPersonalInfoTabActivity");

        spec.SetIndicator("مشخصات شخص", Resources.GetDrawable(Resource.Drawable.tab1));
        View tab = LayoutInflater.From(TabHost.Context).Inflate(Resource.Id.ta, null);

        spec.SetContent(intent);
        TabHost.AddTab(spec);

        //--------------------------------
        intent = new Intent(this, typeof(FrmMelkAbaadZaminInfoTabActivity));
        intent.AddFlags(ActivityFlags.NewTask);

        spec = TabHost.NewTabSpec("FrmMelkAbaadZaminInfoTabActivity");
        spec.SetIndicator("ابعاد", Resources.GetDrawable(Resource.Drawable.tab2));
        spec.SetContent(intent);
        TabHost.AddTab(spec);

        TabHost.CurrentTab = 0;

        Button btnSave = FindViewById<Button>(Resource.Id.btnSave);
        btnSave.Click += new EventHandler(btnSave_Click);

    void btnSave_Click(object sender, EventArgs e)
    {
        LinearLayout llMain = (LinearLayout)TabHost.GetChildAt(0);
        FrameLayout flTabContents = (FrameLayout)llMain.GetChildAt(2);

        var tab1 = flTabContents.GetChildAt(0);
        string father = tab1.FindViewById<EditText>(Resource.Id.txtFather_Name).Text;

    }

by @K. Oulebsir help,i use GetChildAt method and could access my content,but in testing,i found that tab indexs for GetChildAt(index) is not sortable,this mean that when i am in tab1,i can access tab1 elements,but i can access tab2 elements only when i navigated to tab2 after tab1,Other means ,order of my tabs navigate,make tab indexs

maybe i can access tab elements only when those are showed before.

how i can access tab contents without need to loading or how i load them for accesibility without navigating to them?

someone sayed me i can do this only with fragments,i dont know


can everyone help me? tanks

Upvotes: 0

Views: 1050

Answers (1)

K. Oulebsir
K. Oulebsir

Reputation: 51

You can access views in activities like this example:

public class Act1_Principale extends TabActivity implements OnTabChangeListener {
    private TabHost tabHost;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.lay_principale);
        tabHost = getTabHost();

        tabHost.addTab(createTab(Act1.class, title, drawable));
        tabHost.addTab(createTab(Act2.class, title, drawable));
        tabHost.addTab(createTab(Act3.class, title, drawable));
        tabHost.addTab(createTab(Act4.class, title, drawable));

        btn.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                tabHost.getTabContentView().getChildAt(0).
                findViewById(R.id.btnAdd);
            }
        });
    }

    private TabSpec createTab(final Class<?> intentClass, final String tag, final int drawable) {
        final Intent intent = new Intent().setClass(this, intentClass);
        final View tab = LayoutInflater.from(getTabHost().getContext()).inflate(R.layout.tab, null);

        ((ImageView) tab.findViewById(R.id.tab_icon)).setImageResource(drawable);
        return getTabHost().newTabSpec(tag).setIndicator(tab).setContent(intent);
    }
}

Upvotes: 2

Related Questions