I'm trying to make a menu with tabs and see that TabHost and TabLayout are used in the examples. What is the fundamental difference between them, and what is now preferable to use for such a task!

  1. ability to disable scrolling
  2. the ability to insert an icon in the tab instead of text
  3. ability to dynamically add \ delete tabs

    2 answers 2

    TabLayout is the latest implementation of tabs. Use it. There are exactly 2 and 3 points you can do, for the first try in the scrollable attribute

    • If I use ViewPager with the first item, I figured it out. But about the addition of deletion, I have so far met only under TabHost tutorials, maybe there is a reference for an example? - Kirill Stoianov
    • @KirillStoianov, that's about icons - YuriySPb
    • @KirillStoianov, adding tabs is also simple: tabLayout.addTab(tabLayout.newTab().setText("TAB")); - Yuriy SPb

    An example for adding tabs through TabHost:

      findViewById(R.id.add_tab).setOnClickListener(new View.OnClickListener() { public void onClick(View v) { addTab(); } }); } private void addTab(){ LayoutInflater layoutInflate = LayoutInflater.from(Main_Activity.this); Button tabBtn = (Button)layoutInflate.inflate(R.layout.tab_event, null); tabBtn.setText("Tab "); Intent tabIntent = new Intent(Main_Activity.this, TabActivity.class); TabHost.TabSpec setContent = tabHost.newTabSpec("Tab").setIndicator(tabBtn).setContent(tabIntent); tabHost.addTab(setContent); } 

    TabActivity class:

     public class TabActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView tv = new TextView(TabActivity.this); tv.setText("Tab activity"); setContentView(tv); } }