1

I am new to android and I am trying to learn by making a simple chat app in android studio.

I started by creating a navigation drawer. Now I want to add tabs to my app like this - chats / contact / calls below navigation drawer, but I could not find a simple step by step tutorial for doing this.

Before I started this chat app I worked on a simpler app and added some tabs to that old app like this:

    public class MainActivity extends TabActivity {

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Resources resources = getResources();
    TabHost tabHost = getTabHost();

    // First tab
    Intent intentTabOne = new Intent().setClass(this, TabOneActivity.class);
    TabSpec tabSpecTabOne = tabHost
            .newTabSpec("Tab One")
            .setIndicator("", resources.getDrawable(R.drawable.icon_one_config))
            .setContent(intentTabOne);

    // Second tab
    Intent intentTabTwo = new Intent().setClass(this, TabTwoActivity.class);
    TabSpec tabSpecSecondTab = tabHost
            .newTabSpec("Tab Two")
            .setIndicator("", resources.getDrawable(R.drawable.icon_two_config))
            .setContent(intentTabTwo);

    // Third tab
    Intent intentTabThree = new Intent().setClass(this, TabThree.class);
    TabSpec tabSpecSent = tabHost
            .newTabSpec("Tab Three")
            .setIndicator("", resources.getDrawable(R.drawable.icon_three_invitations_config))
            .setContent(intentTabThree);

    // add all tabs
    tabHost.addTab(tabSpecTabOne);
    tabHost.addTab(tabSpecTabTwo);
    tabHost.addTab(tabSpecTabThree);

    //set Windows tab as default 
    tabHost.setCurrentTab(0);
    }

But now my main activity looks like this:

    public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
    }

    @Override
    public void onBackPressed()...

Also, since TabActivity is now deprecated and I think I should use fragment, I got a little bit lost. I would really appreciate if someone could point me in the right direction. Thanks in advance.

Banana
  • 2,435
  • 7
  • 34
  • 60

1 Answers1

2

I'd suggest to use a TabLayout that you can put below your Toolbar/Actionbar, so you can use it in parallel with the navigation drawer.

This answer shows how setup the Tabbar with a ViewPager. In short: You need to include the Design Support Library to use the TabLayout. In your XML layout, put the TabLayout and the ViewPager somewhere below the Toolbar, such that it fits your intentions. Then you can setup the different tabs with their own Fragments in your Activity.

Community
  • 1
  • 1
Floern
  • 33,559
  • 24
  • 104
  • 119