codeme2020
codeme2020

Reputation: 853

Android:adding swipe functionality to existing tab menu?

Is it possible to add a swipe functionality to an existing Tab Menu? i.e. swipe left or right to move between the tabs rather than clicking on a tab.

I already have an exisiting 3 tab menu and do not want to greatly alter it's structure, but would love to add the swipe functionality.

How can I do so?

Main Menu/TabHost:

public class MainMenu extends TabActivity {

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

        TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);
        TabSpec tab1 = tabHost.newTabSpec("Games");
        Intent tab1Intent = new Intent(this, firstActivity.class);
        tab1.setContent(tab1Intent);
        tab1.setIndicator("Games");

        TabSpec tab2 = tabHost.newTabSpec("Search");
        Intent tab2Intent = new Intent(this, secondActivity.class);
        tab2.setContent(tab2Intent);
        tab2.setIndicator("Search");

        TabSpec tab3 = tabHost.newTabSpec("Summary");
        Intent tab3Intent = new Intent(this, thirdActivity.class);
        tab3.setContent(tab3Intent);
        tab3.setIndicator("Summary");


        tabHost.addTab(tab1);
        tabHost.addTab(tab2);
        tabHost.addTab(tab3);


    }

    @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;
    }

}

Example of tab (middle tab):

public class secondActivity extends ActionBarActivity implements View.OnClickListener {

    TextView instruction;
    Button date;
    Button game;
    Button med;
    Button att;
    Button score;

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

        initialiseVars();

    }

    /**
     * Initialises vars and sets onclick listeners for buttons
     */
    public void initialiseVars() {

        // setting up vars
        instruction = (TextView) findViewById(R.id.tvSearchHome);
        date = (Button) findViewById(R.id.btnSearchHomeDate);
        game = (Button) findViewById(R.id.btnSearchHomeGame);
        med = (Button) findViewById(R.id.btnSearchHomeMedValues);
        att = (Button) findViewById(R.id.btnSearchHomeAttValues);
        score = (Button) findViewById(R.id.btnSearchHomeScore);

        // set on click listeners for the buttons
        date.setOnClickListener(this);
        game.setOnClickListener(this);
        med.setOnClickListener(this);
        att.setOnClickListener(this);
        score.setOnClickListener(this);
    }

    public void onClick(View v) {

        switch (v.getId()) {

        case R.id.btnSearchHomeDate:

            Intent openDateSearch = new Intent(
                    "com.example.brianapp.SearchDate");
            // Start activity
            startActivity(openDateSearch);

            break;

        case R.id.btnSearchHomeGame:

            Intent openGameSearch = new Intent(
                    "com.example.brianapp.SearchGame");

            // Start activity
            startActivity(openGameSearch);

            break;

        case R.id.btnSearchHomeMedValues:

            // change this to make sure it opens the med screen
            Intent openMedSearch = new Intent(
                    "com.example.brianapp.MeditationSearchHome");
            // Start activity
            startActivity(openMedSearch);

            break;

        case R.id.btnSearchHomeAttValues:

            // change this to make sure it opens the med screen
            Intent openAttSearch = new Intent(
                    "com.example.brianapp.AttentionSearchHome");
            // Start activity
            startActivity(openAttSearch);
            break;

        case R.id.btnSearchHomeScore:

            // change this to make sure it opens the med screen
            Intent openScoreSearch = new Intent(
                    "com.example.brianapp.SearchScore");
            // Start activit
            startActivity(openScoreSearch);

            break;

        }// switch end

    }

}

Upvotes: 0

Views: 127

Answers (1)

yildirimyigit
yildirimyigit

Reputation: 3022

I can just provide you with the following snippet. Please note that I haven't tested it. I just wanted to show how I would approach this problem.

public class MainMenu extends TabActivity implements GestureDetector.OnGestureListener{
    ...
    @Override
    public boolean onFling(MotionEvent e0, MotionEvent e1, float arg2, float arg3) {
        ...
        int current = getCurrentTab();  // TabHost.getCurrentTab()
        int next;
        if(e0.getRawX() - e1.getRawX() < 0 ){
            //fling right
            next = current + 1; //TODO check for next = n, n=number of tabs
        }
        else{
            next = current - 1; //TODO check for next = -1
        }
        setCurrentTab(next);    // TabHost.setCurrentTab()
    }
}

Upvotes: 1

Related Questions