Tabs doesn't show - android

I want to add tab in myActivity and when I click on some tabs Fragment is created and added.But now when I clicked tab nothing happened.
TabsActivity:
public class TabsActivity extends FragmentActivity {
Fragment directFragmentTab = new Direct();
Fragment adventuresFragmentTab = new AdventuresFragment();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabs_activity);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 2"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
FragmentManager fragmentManager = getSupportFragmentManager();
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
final ViewPagerAdapter adapter = new ViewPagerAdapter(fragmentManager, tabLayout.getTabCount())
{
#Override
public int getCount() {
return 0;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return false;
}
};
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(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) {
}
});
}
}
ViewPagerAdapter:
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
public ViewPagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
#Override
public android.support.v4.app.Fragment getItem(int position) {
switch (position) {
case 0:
Direct tab1 = new Direct();
return tab1;
case 1:
AdventuresFragment tab2 = new AdventuresFragment();
return tab2;
default:
return null;
}
}
#Override
public int getCount() {
return mNumOfTabs;
}
}
What I did wrong?Code compile without error but something in logic I was missed.

You should not override methods from ViewPagerAdapter further. Just do
final ViewPagerAdapter adapter = new ViewPagerAdapter(fragmentManager, tabLayout.getTabCount());

try this I am Using The same Code
final ViewPagerAdapter adapter = new ViewPagerAdapter(fragmentManager, tabLayout.getTabCount())
{
#Override
public int getCount() {
return 0;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return false;
}
};
Replace With
final PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
Edit Your Code Like This
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 2"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
final PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(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) {
}
});

Related

call view of activity from tab fragment

I have a main activity which has a tab layout in which there are two framgents.Following is the code for same.
public void setViewPager(){
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
viewPager = (ViewPager) findViewById(R.id.pager);
final PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
chipGroup.removeAllViews();
utils.createChip(getLayoutInflater(),R.layout.chipview,chipGroup,arrayListFilter);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
Log.d("hgfdgf","+"+tab.getPosition());
if(tab.getPosition()==0){
fragment=new StudyNotes_fragment();
chipGroup.removeAllViews();
utils.createChip(getLayoutInflater(),R.layout.chipview,chipGroup,arrayListFilter);
//filter_iv_booklet.setVisibility(GONE);
}
else if (tab.getPosition()==1){
fragment2=new Specialbooklet_fragment();
chipGroup.removeAllViews();
utils.createChip(getLayoutInflater(),R.layout.chipview,chipGroup,arrayListFilter2);
//filter_iv_booklet.setVisibility(VISIBLE);
}
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
#RequiresApi(api = Build.VERSION_CODES.M)
#Override
public void onClick(View v) {
}
public class PagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
public PagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
for (int i=0;i<=studyM.size()-1;i++){
arrayListFilter.add(studyM.get(i).get(i).getCSubjectName());
}
LayoutInflater li = LayoutInflater.from(context);
HashSet<String> hashSet = new HashSet<String>();
hashSet.addAll(arrayListFilter);
arrayListFilter.clear();
arrayListFilter.addAll(hashSet);
Bundle args = new Bundle();
args.putStringArrayList("array2",arrayListFilter);
args.putSerializable("array4",studyM);
args.putSerializable("tag",makeFragmentName(viewPager.getId(), 1));
fragment.setArguments(args);
return fragment;
case 1:
for (int i=0;i<=specialB.size()-1;i++){
arrayListFilter2.add(specialB.get(i).get(i).getCSubjectName());
}
LayoutInflater li1 = LayoutInflater.from(context);
HashSet<String> hashSet1 = new HashSet<String>();
hashSet1.addAll(arrayListFilter2);
arrayListFilter2.clear();
arrayListFilter2.addAll(hashSet1);
String fragmentTag2 = makeFragmentName(viewPager.getId(), 2);
Bundle args1 = new Bundle();
args1.putSerializable("array", specialB);
args1.putStringArrayList("array2",arrayListFilter2);
args1.putStringArrayList("array3",videosubtitlelist);
args1.putString("tag",fragmentTag2);
fragment2.setArguments(args1);
return fragment2;
//return Specialbooklet_fragment.newInstance(specialB,arrayListFilter2,videosubtitlelist,fragmentTag2);
default:
return null;
}
}
#Override
public int getCount() {
return mNumOfTabs;
}
}
I am using a button which is in toolbar of activity in fragment.I am accessing it in fragment by using getactivity. Below is my code.
rl_filter = activity.findViewById(R.id.rl_filter);
filter_iv = activity.findViewById(R.id.filter_iv);
chipGroup = activity.findViewById(R.id.chipgroup);
applyfilter_tv = activity.findViewById(R.id.applyfilter_tv);
clearfilter_tv = activity.findViewById(R.id.clearfilter_tv);
filter_iv.setOnClickListener(this::onClick);
rl_filter.setOnClickListener(this::onClick);
clearfilter_tv.setOnClickListener(this::onClick);
applyfilter_tv.setOnClickListener(this::onClick);
I am doing same thing in second fragment.My problem is that clicks of only second framgent is working when i am defining the views in both fragments. If i am removing listener and initialisation of buttons from second fragment then first fragment clicks are working. Is there any way i can use the view of activity in both the fragments.

How to set tab at position 1 in viewpager

I've two tabs. By default 0's position tab is selected but i wan to select tab at 1st position. I've tried by
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
TabLayout.Tab tab = tabLayout.getTabAt(someIndex);
tab.select();
also but it didn't work. I also search on stackoverflow and apply at position 1 but my app get crashed.
tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Add Expense"));
tabLayout.addTab(tabLayout.newTab().setText("Add Ledger"));
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager2);
final TabAdapter adapter = new TabAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.getTabAt(1).select();
// }
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) {
}
});
}
TabAdapter:
class TabAdapter extends FragmentStatePagerAdapter {
private int mNumOfTabs;
TabAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new AddExpenseFragment();
case 1:
return new AddLedgerFragment();
}
return null;
}
#Override
public int getCount() {
return mNumOfTabs;
}
}
Try
viewPager.setCurrentItem(position);
I am using following code in my project and is working fine.But before this you have to set up your tab layout with View Pager
tabLayout = (TabLayout)view.findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
tabLayout.getTabAt(1).select();

Tablayout tab with multiple fragment

I am working on an app which use tab layout with 5 tabs. My need is to navigate multiple fragment inside every tab, and open the first fragment when user again reselected the tab. Please help me.
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabLayout);
tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.scene_tab_selector));
tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.my_scene_tab_selector));
tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.conversation_tab_selector));
tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.notification_tab_selector));
tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.more_tab_selector));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
final PagerAdaptor adapter = new PagerAdaptor
(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
tab.getIcon().setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN);
if(tab.getPosition()==4)
{
// pageTitleLabel.setText("My Profile");
}
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
tab.getIcon().setColorFilter(Color.parseColor("#a8a8a8"), PorterDuff.Mode.SRC_IN);
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
My PagerAdaptor is as follows
public class PagerAdaptor extends FragmentStatePagerAdapter {
int mNumOfTabs;
public PagerAdaptor(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
Tab1ContainerFragment tab1 = new Tab1ContainerFragment();
return tab1;
case 1:
MyCuddllFragment tab2 = new MyCuddllFragment();
return tab2;
case 2:
// CuddllConversationFragment tab3 = new CuddllConversationFragment();
ConversationFragment conversationFragment = new ConversationFragment();
return conversationFragment;
case 3:
// CuddllNotificationsFragment tab4 = new CuddllNotificationsFragment();
Tab4ContainerFragment notificationPageFragment = new Tab4ContainerFragment();
return notificationPageFragment;
case 4:
MyProfileFragment tab5 = new MyProfileFragment();
return tab5;
default:
return null;
}
}
#Override
public int getCount() {
return mNumOfTabs;
}
}
public void onTabSelected(TabLayout.Tab tab) {
tab.getIcon().setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN);
if(tab.getPosition()==0)
{
viewpager.setCurrentItem(0);
}else if(tab.getPosition()==1)
{
viewpager.setCurrentItem(1);
}else if(tab.getPosition()==2)
{
viewpager.setCurrentItem(2);
}else if(tab.getPosition()==3)
{
viewpager.setCurrentItem(3);
}
}

Tabs with fragment in activity

I have 2 tabs and 2 fragments.I did all for this guide
http://www.truiton.com/2015/06/android-tabs-example-fragments-viewpager/
but now I see tabs but dont see my fragments.What is the problem?
ViewPagerAdapter:
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
public ViewPagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
#Override
public android.support.v4.app.Fragment getItem(int position) {
switch (position) {
case 0:
Direct tab1 = new Direct();
return tab1;
case 1:
AdventuresFragment tab2 = new AdventuresFragment();
return tab2;
default:
return null;
}
}
#Override
public int getCount() {
return mNumOfTabs;
}
}
Activity with using this adapter:
public class TabsActivity extends Activity {
Fragment directFragmentTab = new Direct();
Fragment adventuresFragmentTab = new AdventuresFragment();
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabs_activity);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
}
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 2"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
final PagerAdapter adapter = new PagerAdapter
() {
#Override
public int getCount() {
return 0;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return false;
}
};
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(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) {
}
});
}
}
In your activity you are using the PagerAdapter class while your fragment handling class is named ViewPagerAdapter. You want to use the ViewPagerAdapter class instead.

properly remove fragments from FragmentStatePagerAdapter

hey guys i implemented a tab layout(an action bar with tabs) , basically it is made of using pagerAdapter and FragmentStatePagerAdapter below is my code :
public class PagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
public PagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
TabFragment1 tab1 = new TabFragment1();
return tab1;
case 1:
TabFragment2 tab2 = new TabFragment2();
return tab2;
case 2:
TabFragment3 tab3 = new TabFragment3();
return tab3;
case 3:
TabFragment4 tab4 = new TabFragment4();
return tab4;
case 4:
TabFragment5 tab5 = new TabFragment5();
return tab5;
default:
return null;
}
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub
super.destroyItem(container, position, object);
FragmentManager manager = ((Fragment) object).getFragmentManager();
FragmentTransaction trans = manager.beginTransaction();
trans.remove((Fragment) object);
trans.commit();
}
#Override
public int getCount() {
return mNumOfTabs;
}}
my mainActivty:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Tab1"));
tabLayout.addTab(tabLayout.newTab().setText("Tab2"));
tabLayout.addTab(tabLayout.newTab().setText("Tab3"));
tabLayout.addTab(tabLayout.newTab().setText("Tab4"));
tabLayout.addTab(tabLayout.newTab().setText("Tab5"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
final PagerAdapter adapter = new PagerAdapter
(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(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) {
}
});
now i want to remove the tabs(fragments) and for that am just removing them from here
tabLayout.addTab(tabLayout.newTab().setText("Tab1"));
tabLayout.addTab(tabLayout.newTab().setText("Tab2"));
tabLayout.addTab(tabLayout.newTab().setText("Tab3"));
tabLayout.addTab(tabLayout.newTab().setText("Tab4"));
if (condition == true){ // do nothing which means don't add the fragment }
else {
tabLayout.addTab(tabLayout.newTab().setText("Tab5")); }
and is working but not as it supposed to , even after removing the the tab from here tabLayout.addTab(tabLayout.newTab().setText("Tab5")); the fragments are still there and actively performing their task , i want to shut them completely , am new in android so have no idea about this that its a good approach or not guys is it enough or not please let me know any guidance will be so helpful for me

Categories

Resources