I want to add tabs dynamically into TabLayout
. I can add different objects as different custom Views.
The problem is that I have to dynamically add various number of custom Views and I do not want to create different instance of view
for each tab if it is possible.
TabLayout mTabLayout = (TabLayout) findViewById(R.id.tabs);
TextView textView = (TextView)getLayoutInflater ().inflate ( R.layout.simple_text_layout,null,false );
textView.setText ( "HIM" );
mTabLayout.addTab(mTabLayout.newTab().setCustomView ( textView).setTag ( "0" ));
TextView textView1 = (TextView)getLayoutInflater ().inflate ( R.layout.simple_text_layout,null,false );
textView1.setText ( "HER" );
mTabLayout.addTab(mTabLayout.newTab().setCustomView ( textView1).setTag ( "1" ));
textView1 = (TextView)getLayoutInflater ().inflate ( R.layout.simple_text_layout,null,false );
textView1.setText ( "OTHERS" );
mTabLayout.addTab(mTabLayout.newTab().setCustomView ( textView1).setTag ( "2" ));
If I attach custom views like above then I get this result.
TabLayout mTabLayout = (TabLayout) findViewById(R.id.tabs);
TextView textView = (TextView)getLayoutInflater ().inflate ( R.layout.simple_text_layout,null,false );
textView.setText ( "HIM" );
mTabLayout.addTab(mTabLayout.newTab().setCustomView ( textView).setTag ( "0" ));
TextView textView1 = (TextView)getLayoutInflater ().inflate ( R.layout.simple_text_layout,null,false );
textView1.setText ( "HER" );
mTabLayout.addTab(mTabLayout.newTab().setCustomView ( textView1).setTag ( "1" ));
textView1 = (TextView)getLayoutInflater ().inflate ( R.layout.simple_text_layout,null,false );
textView1.setText ( "OTHERS" );
mTabLayout.addTab(mTabLayout.newTab().setCustomView ( textView1).setTag ( "2" ));
textView1.setText ( "ACCESS.." );
mTabLayout.addTab(mTabLayout.newTab().setCustomView ( textView1).setTag ( "3" ));
If I attach items like above into my tabLayout I get this result .
Can anyone point out at what wrong I am doing by trying to to use same object of view for multiple custom view for tabLayout with altered values?
I have been used SmartTabLayout. You can customized it with your needs.
initialize layouts:
then,
create list of tabs.
SetupTab function Below:
set pager Adapter:
TabPagerAdapter class