0

//I am using action bar tabs in my app,I want to switch from one activity to another activity with in tabs when I press on tabs,how can I call the activity with in tabs. I want to display any activity with in actionbar tabs.

            public class MainActivity extends Activity implements TabListener {
                // Refresh menu item
                private MenuItem action_search;

                Tab tab1, tab2, tab3;

                @Override
                protected void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
            //creating tabs
                    ActionBar actionBar = getActionBar();
                    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    //adding tabs to actionbar
                    tab1 = actionBar.newTab();
                    tab1.setText("camera");
                    tab1.setTabListener(this);
                    actionBar.addTab(tab1);

                    tab2 = actionBar.newTab();
                    tab2.setText("contacts");
                    tab2.setTabListener(this);
                    actionBar.addTab(tab2);



                }




                @Override
                public void onTabReselected(Tab tab, FragmentTransaction ft) {
                    // TODO Auto-generated method stub

                }

                @TargetApi(Build.VERSION_CODES.HONEYCOMB)
                @Override
                public void onTabSelected(Tab tab, FragmentTransaction ft) {
                    // TODO Auto-generated method stub
                    switch (tab.getPosition()) {
                    case 0:
                        Intent i = new Intent(getApplicationContext(), MainActivity2.class);
                        startActivity(i);

                        break;

                    case 1:



                        Intent i = new Intent(getApplicationContext(), MainActivity2.class);
                        startActivity(i);

                        break;

                    }
                }



                @Override
                public void onTabUnselected(Tab tab, FragmentTransaction ft) {
                    // TODO Auto-generated method stub

                }

            }
user3114723
  • 401
  • 1
  • 6
  • 17

1 Answers1

0

MainActivity.java

package com.example.moviesswipe;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.Menu;

@SuppressWarnings("unused")
@SuppressLint("NewApi")
public class MainActivity extends FragmentActivity implements
        ActionBar.TabListener {

    private ViewPager viewPager;
    private TabsPagerAdapter mAdapter;
    private ActionBar actionBar;

    private String[] tabs = { "English", "Tamil", "Hindi" };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        viewPager = (ViewPager) findViewById(R.id.pager);
        actionBar = getActionBar();
        mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

        viewPager.setAdapter(mAdapter);
        actionBar.setHomeButtonEnabled(false);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        for (String tab_name : tabs) {
            actionBar.addTab(actionBar.newTab().setText(tab_name)
                    .setTabListener(this));
        }

        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {

                actionBar.setSelectedNavigationItem(position);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onTabReselected(Tab tab, android.app.FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTabSelected(Tab tab, android.app.FragmentTransaction ft) {
        // TODO Auto-generated method stub
        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }

}

TabsPagerAdapter.java

package com.example.moviesswipe;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class TabsPagerAdapter extends FragmentPagerAdapter {

    public TabsPagerAdapter(FragmentManager fm) {
        super(fm);
        // TODO Auto-generated constructor stub
    }

    @Override
    public Fragment getItem(int index) {

        switch (index) {
        case 0:

            return new English();
        case 1:

            return new Tamil();
        case 2:

            return new Hindi();
        }
        return null;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 3;
    }

}

Here i placed three tabs , for each tab i created three activity. English.java , Tamil.java and Hindi.java

sample one:

package com.example.moviesswipe;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


@SuppressWarnings("unused")
public class English extends Fragment{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.english, container, false);

        return rootView;
    }



}

For each class you need to create a layout:

No need to entry these classes in your manifest coz these are all fragments.

Try it like this dude :) Happy coding :)

Say if there any queries.

kathir
  • 489
  • 2
  • 11