java - How to switch automatically to tab when press button -


i have created tabbed activity 2 tab (tab 1,tab 2) want when press button on tab 1,switch automatically tap 2.

you not define code give 2 code both scenario of tab activity

(1) using viewpager

(2)tab host (deprecated)

=========================================================================

(1)if use viewpager use below one===>

viewpager.setcurrentitem(0);

you have call parent view inside fragment.

  public class moviesfragment extends fragment {       viewpager viewpager;     @override     public view oncreateview(layoutinflater inflater, viewgroup container,             bundle savedinstancestate) {          view rootview = inflater.inflate(r.layout.fragment_movies, container, false);          button btn = (button) rootview.findviewbyid(r.id.btn);         viewpager = (viewpager) getactivity().findviewbyid(r.id.pager);             btn.setonclicklistener(new view.onclicklistener()             {                 @override               public void onclick(view v)                {                    viewpager.setcurrentitem(0);                 }           });           return rootview;     }  } 

======================================================================

(2) if use tabhost tabactivity use

gettabhost().setcurrenttab(index);

use below code====>

public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); textview textview = new textview(this); textview.settext("this tab1"); setcontentview(textview);  final button button = (button) findviewbyid(r.id.button1); button.setonclicklistener(new view.onclicklistener() {     public void onclick(view v) {         // perform action on click         // perform switch tab event here instance...         gettabhost().setcurrenttabbytag("tab2");     } }); 

}


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -