This is my code and the error that occured
I am a beginner and was trying to work on the tab layout. The app didn't give any error while compiling but crashes midway. My main activity code is as follows:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TabLayout tabLayout= findViewById(R.id.tab_layout);
TabItem t= findViewById(R.id.t1);
final ViewPager viewPager= findViewById(R.id.pager);
new PageAdapter(getSupportFragmentManager(),4);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
Please help me as I have to submit this as my assignment.
You need to make the declaration outside the oncreate function to make it global and seen by other methods, then do the initialization inside like this :
TabLayout tabLayout;
TabItem t;
ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tabLayout= findViewById(R.id.tab_layout);
t= findViewById(R.id.t1);
viewPager= findViewById(R.id.pager);
new PageAdapter(getSupportFragmentManager(),4);
Related
i create a viewPager and is work , my problem is for select a page i just can drag page but I want to click on the tabs to select the pages . now i add some new code but i cant find this error
my code
public class MainActivity extends FragmentActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
Pager adapter = new Pager(getSupportFragmentManager());
viewPager.setAdapter(adapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tablayout);
tabLayout.setupWithViewPager(viewPager);
viewPager.addOnAdapterChangeListener( new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
TabLayout.OnTabSelectedListener tabListener = new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
};
You cant use like viewPager.addOnAdapterChangeListener( new TabLayout.TabLayoutOnPageChangeListener(tabLayout)); Because addOnAdapterChangeListener() method expect OnAdapterChangeListener as paramenter. You should use void addOnPageChangeListener (ViewPager.OnPageChangeListener listener) on viewPager instead.It has several callback method, So you can detect the event when page of viewPager is changed.
Reference
I want to start activity with three fragments
Intent intent = new Intent(NewMealActivity.this, MainActivity.class);
intent.putExtra("id", 1);
startActivity(intent);
MainActivity includs three fragments (Frag1, Frag2, Frag3).
I want to open Frag3, but opens Frag1
This is my MainActivity with three tabs
public class MainActivity extends FragmentActivity {
private String txtFragmentMyData;
private String txtFragmentProductList;
private String txtFragmentCalorieCalculator;
private TabLayout tabLayout;
private long tabId;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtFragmentMyData = getString(R.string.txt_fragment_my_data);
txtFragmentProductList = getString(R.string.txt_fragment_product_list);
txtFragmentCalorieCalculator = getString(R.string.txt_fragment_calorie_calculator);
tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText(txtFragmentMyData));
tabLayout.addTab(tabLayout.newTab().setText(txtFragmentProductList));
tabLayout.addTab(tabLayout.newTab().setText(txtFragmentCalorieCalculator));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
final ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
Bundle extras = getIntent().getExtras();
if (extras != null) {
tabId = extras.getLong("id");
}
if (tabId > 0){
viewPager.setCurrentItem(2);
} else {
viewPager.setCurrentItem(0);
}
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
}
I want, that at the first start opens first tab, but if I click on buttom in another activity opens third tab
I am trying to highlight tab icon when viewPager is selected or swiped. For this I am using 'tabLayout.setOnTabSelectedListener()'. But it doesn't highlight when I swipe tab, but when I press tab selected all work perfect and tab gets highlighted. May be it's related with gradle version. I am using 'classpath 'com.android.tools.build:gradle:2.1.0'' . This is activity :
private int[] tabIcons = {
R.drawable.tab_icon_home,
R.drawable.tab_icon_cart2,
R.drawable.tab_icon_aksia,
R.drawable.tab_icon_menu
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_icon_tabs);
toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitleTextColor(Color.WHITE);
setSupportActionBar(toolbar);
frameLayout = (FrameLayout) findViewById(R.id.frameLayout);
adapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(pageChangeListener);
viewPager.setOffscreenPageLimit(10);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();
}
private OnPageChangeListener pageChangeListener = new OnPageChangeListener() {
int currentPosition = 0;
#Override
public void onPageSelected(int newPosition) {
FragmentLifecycle fragmentToHide = (FragmentLifecycle) adapter.getItem(currentPosition);
fragmentToHide.onPauseFragment();
FragmentLifecycle fragmentToShow = (FragmentLifecycle) adapter.getItem(newPosition);
fragmentToShow.onResumeFragment();
currentPosition = newPosition;
adapter.notifyDataSetChanged();
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
public void onPageScrollStateChanged(int arg0) { }
};
private void setupTabIcons() {
tabLayout.getTabAt(0).setIcon(tabIcons[0]);
tabLayout.getTabAt(1).setIcon(tabIcons[1]);
tabLayout.getTabAt(2).setIcon(tabIcons[2]);
tabLayout.getTabAt(3).setIcon(tabIcons[3]);
tabLayout.getTabAt(0).getIcon().setColorFilter(Color.WHITE, PorterDuff.Mode.MULTIPLY);
tabLayout.getTabAt(1).getIcon().setColorFilter(Color.parseColor("#81C784"), PorterDuff.Mode.SRC_IN);
tabLayout.getTabAt(2).getIcon().setColorFilter(Color.parseColor("#81C784"), PorterDuff.Mode.SRC_IN);
tabLayout.getTabAt(3).getIcon().setColorFilter(Color.parseColor("#81C784"), PorterDuff.Mode.SRC_IN);
tabLayout.setOnTabSelectedListener( new TabLayout.ViewPagerOnTabSelectedListener(viewPager) {
#Override
public void onTabSelected(TabLayout.Tab tab) {
super.onTabSelected(tab);
tab.getIcon().setColorFilter(Color.WHITE, PorterDuff.Mode.MULTIPLY);
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
super.onTabUnselected(tab);
tab.getIcon().setColorFilter(Color.parseColor("#81C784"), PorterDuff.Mode.MULTIPLY);
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
super.onTabReselected(tab);
}
});
}
I found solution,in gradle I used
dependencies {
compile 'com.android.support:appcompat-v7:23.0.0'
compile 'com.android.support:design:23.0.0' }
And I change from 23.0.0 to 23.0.1 in both appcompat-v7 and design. Now all work for me.
Try this way...
tabLayout = ((TabLayout) findViewById(R.id.tabs));
adapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(
tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
public void onTabReselected(TabLayout.Tab paramAnonymousTab) {
}
public void onTabSelected(TabLayout.Tab paramAnonymousTab) {
viewPager.setCurrentItem(paramAnonymousTab.getPosition());
tab.getIcon().setColorFilter(Color.WHITE, PorterDuff.Mode.MULTIPLY);
}
public void onTabUnselected(TabLayout.Tab paramAnonymousTab) {
tab.getIcon().setColorFilter(Color.parseColor("#81C784"), PorterDuff.Mode.MULTIPLY);
}
});
tabLayout.getTabAt(0).setIcon(tabIcons[0]);
tabLayout.getTabAt(1).setIcon(tabIcons[1]);
tabLayout.getTabAt(2).setIcon(tabIcons[2]);
tabLayout.getTabAt(3).setIcon(tabIcons[3]);
tabLayout.getTabAt(0).getIcon().setColorFilter(Color.WHITE, PorterDuff.Mode.MULTIPLY);
tabLayout.getTabAt(1).getIcon().setColorFilter(Color.parseColor("#81C784"), PorterDuff.Mode.SRC_IN);
tabLayout.getTabAt(2).getIcon().setColorFilter(Color.parseColor("#81C784"), PorterDuff.Mode.SRC_IN);
tabLayout.getTabAt(3).getIcon().setColorFilter(Color.parseColor("#81C784"), PorterDuff.Mode.SRC_IN);
viewPager.setOffscreenPageLimit(4);
Happy coding.
When you call setupWithViewPager, this will internally call setOnTabSelectedListener(new ViewPagerOnTabSelectedListener(viewPager));, overriding your OnTabSelectedListener.
you have implemented TabLayout.ViewPagerOnTabSelectedListener, and then overridden onTabSelected() and call setOnTabSelectedListener() after setupWithViewPager():
This is correct and it is working fine when tab clicked,
You are saying that when you scroll the page, it is not working. Have a look at your onPageSelected() of OnPageChangeListener(). Because this method gets called as and when particular Page selected.
how can I save and return the tab position when user press the Back button?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
ViewPager vpPager = (ViewPager) findViewById(R.id.viewpager);
adapterViewPager = new MyPagerAdapter(getSupportFragmentManager());
vpPager.setAdapter(adapterViewPager);
// Give the TabLayout the ViewPager
tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
tabLayout.setTabMode(TabLayout.MODE_FIXED);
tabLayout.setupWithViewPager(vpPager);
// Attach the page change listener inside the activity
vpPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(POSITION, tabLayout.getSelectedTabPosition());
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
viewPager.setCurrentItem(savedInstanceState.getInt(POSITION));
}
Do I use the onBackPressed() to do this?
Help is much appreciated. Thanks!
You can do it with back press in the following way:
#Override
public void onBackPressed() {
int pos = mViewPager.getCurrentItem();
}
and when you back in screen you can set position of pager
#Override
public void onResume(){
super.onResume();
// put your code here...
if(mViewPager !=null)
mViewPager.setCurrentItem(pos);
}
I have page with a button and tabs below. I want the tab which is opened to reflect the change in variable data after i click the button. I am not able to understand it.
The following is my code:-
public class Level2MainActivity_grid1 extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.level2mainlayout_place1);
// I am putting a viewpager with fragments into them and put tabs
ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
PagerAdapter pagerAdapter = new PagerAdapter(getSupportFragmentManager(), Titles, Numboftabs);
viewPager.setAdapter(pagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabLayout);
tabLayout.setupWithViewPager(viewPager);
Button buttonId = (Button) findViewById(R.id.planmytraveludupitown);
buttonId.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//I want to save some things and also refresh the current tab
}
});
}
}
Below line also works but incase it don't work then you can try another option also
pagerAdapter.notifydatasetChanged();
Try below code :-
buttonId.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
pagerAdapter = new PagerAdapter(getSupportFragmentManager(), Titles, Numboftabs);
viewPager.setAdapter(pagerAdapter);
tabLayout.setupWithViewPager(viewPager);
}
});
Note:- when you get new data redefine your Titles and Numoftabs again
Couldn't you notify the dataset in your adapter ?
pagerAdapter.notifyDatasetChanged()