I am working on viewPager with fragment and it was working all things perfect before I have start implementing runtime permission for different module.
Here is my viewPager Adapter.
public class DashboardAdapter extends FragmentStatePagerAdapter {
public DashboardAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return FunkyInboxFragment.newInstance(position);
case 1:
return FunkyTakePhotoFragment.newInstance(position);
case 2:
return FunkyAddFriendsFragmnet.newInstance(position);
default:
return FunkyTakePhotoFragment.newInstance(position);
}
}
#Override
public int getCount() {
return 3;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
Fragment fragment = (Fragment) super.instantiateItem(container, position);
return fragment;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
}
}
Here different fragments required different permissions so I have made a common activity class for it. It's working in the same fragment but after asking for a permission when I swipe the viewPager the fragment after next fragment and a fragment before previous fragment get blank. i have made all possible change in adapter but did not get a proper outcome.
Please can anyone help with it!
#Override
public int getCount() {
return 2;
}
//try this may be it work
package com.example.madhavgor.actionbardemolast;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
private int[] tabIcons = {
R.mipmap.ic_launcher,R.mipmap.ic_launcher_bluetooth
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//toolbar = (Toolbar) findViewById(R.id.toolbar);
//setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager) findViewById(R.id.viewPager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();
}
private void setupTabIcons() {
tabLayout.getTabAt(0).setIcon(tabIcons[0]);
tabLayout.getTabAt(1).setIcon(tabIcons[1]);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new OneFragment(), "ONE");
adapter.addFragment(new SecondFragment(), "BLUETOOTH");
adapter.addFragment(new ThirdFragment(), "THREE");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
//refer this code ....may be it will help you..
Related
package com.beacons.railwayaid.raid;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.customlbs.library.IndoorsFactory;
import com.customlbs.surface.library.IndoorsSurfaceFactory;
import com.customlbs.surface.library.IndoorsSurfaceFragment;
public class Navigation extends Fragment{
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.navigation_layout, container, false);
IndoorsFactory.Builder indoorsBuilder = new IndoorsFactory.Builder();
IndoorsSurfaceFactory.Builder surfaceBuilder = new IndoorsSurfaceFactory.Builder();
indoorsBuilder.setContext(getContext());
indoorsBuilder.setApiKey("69af5e85-007a-4df5-8715-d1c2c22e206f");
indoorsBuilder.setBuildingId((long) 979231679);
surfaceBuilder.setIndoorsBuilder(indoorsBuilder);
IndoorsSurfaceFragment indoorsFragment = surfaceBuilder.build();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(android.R.id.tabcontent, indoorsFragment, "indoors");
transaction.commit();
return v;
}
}
I am new at android app development and I can't figure out why the fragment is viewed under all tabs. Previously when it was only textview, the app showed specific text under each tab and the app worked but adding this new code somehow causes a fragment to be viewed under all tabs.
Edit:
Adapter Code:
package com.beacons.railwayaid.raid;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import java.util.List;
public class PagerAdapter extends FragmentPagerAdapter {
List<Fragment> listFragments;
public PagerAdapter(FragmentManager fm, List<Fragment> listFragments){
super(fm);
this.listFragments = listFragments;
}
#Override
public Fragment getItem(int position) {
return listFragments.get(position);
}
#Override
public int getCount() {
return listFragments.size();
}
}
You need to use an Adpater to switch between Fragments.
Also in your Adapter you need to provide switch case to choose appropriate fragments based on selected Tabs.
Have a look at this.
https://developer.android.com/reference/android/support/v4/app/FragmentPagerAdapter.html
Because you set that only fragment, you have to use switch case or if condition and take Id of the each tab and set at the perticular tab.
Just like this:
private void setupViewPager(ViewPager viewPager) {
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
ViewPagerAdapter adapter = new ViewPagerAdapter(fragmentManager);
if (role == 1) {
adapter.addFragment(new MyDetailsB(), "My Details");
adapter.addFragment(new Biography(), "Biography");
viewPager.setAdapter(adapter);
}
}
if (st == 1) {
adapter.addFragment(new MyDetailsB(), "My Details");
adapter.addFragment(new Biography(), "Biography");
viewPager.setAdapter(adapter);
}
}
private static class ViewPagerAdapter extends FragmentStatePagerAdapter {
private final List<android.support.v4.app.Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
private ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public android.support.v4.app.Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
private void addFragment(android.support.v4.app.Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
In my program, I have 2 implemented tabs using fragments. I've placed an EditText inside Tab2. I want to write all my main code for Tab2 inside onCreateView() of fragment Tab2, so that every time I swipe over to Tab2, this same code is executed. To test whether onCreateView() was being called every time I swiped to Tab2, I wrote the following statement in onCreateView() of Tab2:
text.setText("default");
Now when I run the app and swipe to Tab2- I see 'default' written. Now if I change this text manually, then swipe over to Tab1 and come back to Tab2- I still see this same text written. This obviously means that onCreateView() was not called again when I swiped to Tab2 for the 2nd time? How do I fix this?
Tab2.java
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Tab2 extends Fragment
{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
setRetainInstance(false);
final View rootView;
rootView= inflater.inflate(R.layout.fragment_tab2, container, false);
text.setText("default");
return rootView;
}
}
MainActivity.java
package com.example.nirvan.diary30;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v7.widget.Toolbar;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity
{
public static int itemPosition;
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
viewPager.setOffscreenPageLimit(0);
}
private void setupViewPager(ViewPager viewPager)
{
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new Tab1(), "ONE");
adapter.addFragment(new Tab2(), "TWO");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter
{
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
If you are using ViewPager
ViewPager viewPager;
Try to use setOnPageChangeListenerin MainActivity
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
if(mFragmentList.size() > position && mFragmentList.get(position) instanceof Tab2)
{
((Tab2)mFragmentList.get(position)).changeText("changed text")
}
// you can use interface insted
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
in Tab2
public void changeText(String string){
text.setText(string);
}
Update MainActivity.java
public class MainActivity extends AppCompatActivity
{
public static int itemPosition;
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
ViewPagerAdapter adapter;
private final List<Fragment> mFragmentLists = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
viewPager.setOffscreenPageLimit(0);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
if(mFragmentList.size() > position && mFragmentList.get(position) instanceof Tab2)
{
((Tab2)mFragmentList.get(position)).changeText("changed text")
}
// you can use interface insted
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
}
private void setupViewPager(ViewPager viewPager)
{
adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new Tab1(), "ONE");
adapter.addFragment(new Tab2(), "TWO");
viewPager.setAdapter(adapter);
mFragmentLists = adapter.getFragements();
}
class ViewPagerAdapter extends FragmentPagerAdapter
{
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public List<Fragment> getFragements()
{
return mFragmentList;
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
I want to set image/icon in Tabsin the following code:
in my application i set Tabs in Bottom and now i want to set image in the space of the text.. i tried tutorials also but not getting output as i wanted to
Here Tab.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#color/white"/>
<android.support.design.widget.TabLayout
android:id="#+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/orange"
/>
</LinearLayout>
Now Tab.Java
package com.example.sachin.navigationwithswipe;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TabHost;
public class TabFragment extends Fragment {
public static TabLayout tabLayout;
public static ViewPager viewPager;
public static int int_items = 3 ;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View x = inflater.inflate(R.layout.tab_layout,null);
tabLayout = (TabLayout) x.findViewById(R.id.sliding_tabs);
viewPager = (ViewPager) x.findViewById(R.id.viewpager);
viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
tabLayout.post(new Runnable() {
#Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
}
});
return x;
}
class MyAdapter extends FragmentPagerAdapter{
public MyAdapter(FragmentManager fm) {
super(fm);
}
/**
* Return fragment with respect to Position .
*/
#Override
public Fragment getItem(int position)
{
switch (position){
case 0 : return new PrimaryFragment();
case 1 : return new SocialFragment();
case 2 : return new UpdatesFragment();
}
return null;
}
#Override
public int getCount() {
return int_items;
}
/**
* This method returns the title of the tab according to the position.
*/
#Override
public CharSequence getPageTitle(int position) {
switch (position){
case 0 :
return "Primary";
case 1 :
return "Social";
case 2 :
return "Updates";
}
return null;
}
}
}
i want to set Image in the Tabs using following code.
Use this to set icon in tab
tabLayout.setupWithViewPager(viewPager);
//You tab icons
private int[] icons = {
R.drawable.one,
R.drawable.two,
R.drawable.three
};
for (int i = 0; i < tabLayout.getTabCount(); i++) {
tabLayout.getTabAt(i).setIcon(icons[i]);
}
Also If you want only Icon then on Adapter pass pageTitle null
#Override
public CharSequence getPageTitle(int position) {
// return null to display only the icon
return null;
}
Use this for Selector. This Might Work
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/icon_on" android:state_selected="true"/>
<item android:drawable="#drawable/icon_off"/> <!-- default -->
</selector>
Set icon like this way..
mTabLayout.addTab(mTabLayout.newTab().setIcon(R.drawable.yourIcon));
To set both icon and text:
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
private int[] tabIcons = {
R.drawable.ic_tab_favourite,
R.drawable.ic_tab_call,
R.drawable.ic_tab_contacts
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();
}
private void setupTabIcons() {
tabLayout.getTabAt(0).setIcon(tabIcons[0]);
tabLayout.getTabAt(1).setIcon(tabIcons[1]);
tabLayout.getTabAt(2).setIcon(tabIcons[2]);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new OneFragment(), "ONE");
adapter.addFrag(new TwoFragment(), "TWO");
adapter.addFrag(new ThreeFragment(), "THREE");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
Setting only icon to tab is same as setting text and icon except the method getPageTitle() in ViewPagerAdapter class returns null instead of tab label.
Modify MainActivity.java and modify the getPageTitle() method as :
#Override
public CharSequence getPageTitle(int position) {
// return null to display only the icon
return null;
}
I have an activity which uses view pager to switch between fragments. I have three fragments. While switching between these fragments i have to pass a string value to the fragment class to identify which fragment is selected.Because here I am using same fragment class for all the three fragments.
Here is my main activity.
public class mainActivity extends AppCompatActivity {
public String kind, value;
private TabLayout tabLayout;
private ViewPager viewPager;
public FloatingActionButton upload, send;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
upload = (FloatingActionButton) findViewById(R.id.upload_fab);
send = (FloatingActionButton) findViewById(R.id.send_message);
uploadFab.hide();
Intent mIntent = getIntent();
kind = mIntent.getStringExtra("KIND");
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
Student student =new Student();
switch (position) {
case 0:
upload.hide();
value = "r";
break;
case 1:
upload.hide();
value = "e";
break;
case 2:
upload.show();
value = "u";
break;
default:
upload.hide();
val = "r";
break;
}
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new RemainderFragment(), "REMAINDER");
adapter.addFragment(new RemainderFragment(), "EMAIL");
adapter.addFragment(new RemainderFragment(), "UPLOAD");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
here is my fragment class
public class RemainderFragment extends Fragment {
public RemainderEmailFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.raminder_fragment, container, false);
EditText subject = (EditText) rootView.findViewById(R.id.subject);
EditText des = (EditText) rootView.findViewById(R.id.description);
// String value = mainActivity.value
//button to send data
button = activity.send;
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//getting details
SendData();
}
});
return rootView;
}
public void SendData() {
//functon
}
I have tried intents and shared preferences to send data. but nothing worked. I have to get the String value in my fragment class. please help.
Please check the below code for sample required changes, I have modified your file and added some code to support your requirement.
Your Activity file
package com.adobe.reader.test;
import java.util.ArrayList;
import java.util.List;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import com.adobe.reader.R;
public class mainActivity extends AppCompatActivity
{
public String kind, value;
private TabLayout tabLayout;
private ViewPager viewPager;
public FloatingActionButton upload, send;
// Adding adapter
private ViewPagerAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
upload = (FloatingActionButton) findViewById(R.id.upload_fab);
send = (FloatingActionButton) findViewById(R.id.send_message);
uploadFab.hide();
Intent mIntent = getIntent();
kind = mIntent.getStringExtra("KIND");
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
// Let's say we need to call some method from fragment in the activity
Fragment fragment = getCurrentFragment();
if (Fragment instanceof RemainderFragment)
{
RemainderFragment remainderFragment = (RemainderFragment) fragment;
remainderFragment.setString("Testing string");
}
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
Fragment fragment = getCurrentFragment();
if (Fragment instanceof RemainderFragment)
{
RemainderFragment remainderFragment = (RemainderFragment) fragment;
}
Student student =new Student();
switch (position) {
case 0:
upload.hide();
value = "r";
break;
case 1:
upload.hide();
value = "e";
break;
case 2:
upload.show();
value = "u";
break;
default:
upload.hide();
val = "r";
break;
}
remainderFragment.setString(value);
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
}
/**
* This method will give you current fragment used in the viewpager.
* #return
*/
public Fragment getCurrentFragment()
{
Fragment fragment = null;
if (viewPager != null)
{
int currentFragmentIndex = viewPager.getCurrentItem();
if (adapter != null)
{
fragment = adapter.getItem(currentFragmentIndex);
}
}
return fragment;
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new RemainderFragment(), "REMAINDER");
adapter.addFragment(new RemainderFragment(), "EMAIL");
adapter.addFragment(new RemainderFragment(), "UPLOAD");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
Your Fragment file
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import com.adobe.reader.R;
public class RemainderFragment extends Fragment
{
public String testingString;
public RemainderEmailFragment() {
// Required empty public constructor
}
// Sample method to be called from activity
public void setString(String testStr)
{
testingString = testStr;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.raminder_fragment, container, false);
EditText subject = (EditText) rootView.findViewById(R.id.subject);
EditText des = (EditText) rootView.findViewById(R.id.description);
// String value = mainActivity.value
//button to send data
button = activity.send;
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//getting details
SendData();
}
});
return rootView;
}
public void SendData() {
//functon
}
Is there a way to dynamically hide and show a tab in android.support.design.widget.TabLayout
For example, click a button/view somewhere else in activity which hides or shows the tab in position 3 or shows/hides tab of a specific id.
I tried removing the tab tabLayout.removeTabAt(3); and add it back in but get an exception when swiping tabs as swipe still tries to go to the tab that has been removed
12-08 12:30:42.904 29052-29052/com.example.p1v1 E/MessageQueue-JNIļ¹ java.lang.IndexOutOfBoundsException: Invalid index 3, size is 3
I have the following code:
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
...
...
public class BaseTabActivity extends AppCompatActivity {
private TabLayout tabLayout;
private ViewPager viewPager;
private int[] tabIcons = {
R.drawable.ic_w,
R.drawable.ic_x,
R.drawable.ic_y,
R.drawable.z
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_base_tab);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();
...
...
}
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]);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new FeedFragment(), "W");
adapter.addFragment(new FeedFragment(), "X");
adapter.addFragment(new FeedFragment(), "Y");
adapter.addFragment(new FeedFragment(), "Z");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
//return mFragmentTitleList.get(position); //replace below with this if you want tab text and not just icons
// return null to display only the icon
return null;
}
}
}